lib/livery/presenter.rb in livery-0.1.0 vs lib/livery/presenter.rb in livery-0.2.0
- old
+ new
@@ -23,28 +23,31 @@
def method_added(sym)
raise "can't override resource on a subclass of Presenter" if sym == :resource
end
def presenter_association(*names, source: nil, namespace: nil)
+ raise ArgumentError, '`source` must be a block' unless source.nil? || source.is_a?(Proc)
+
names.each do |name|
instance_variable_name = "@#{name}".to_sym
define_method name do
return instance_variable_get(instance_variable_name) if instance_variable_defined?(instance_variable_name)
- association_resource = source.present? ? @resource.instance_exec(&source) : @resource.public_send(name)
+ association_resource = source.nil? ? @resource.public_send(name) : @resource.instance_exec(&source)
association_presenter = Presenter.to_presenter(association_resource, namespace: namespace)
instance_variable_set(instance_variable_name, association_presenter)
association_presenter
end
end
end
def presenterize(klass, namespace: nil)
- [namespace.presence, "#{klass}Presenter"].compact.join('::').constantize
+ presenter_klass_string = [namespace, "#{klass}Presenter"].compact.join('::')
+ ActiveSupport::Inflector.constantize(presenter_klass_string)
end
def resource(sym)
define_method sym do
@resource
@@ -70,14 +73,14 @@
end
end
end
def t(*args)
- I18n.t(self.class.name.underscore + args.shift, *args)
+ I18n.t(ActiveSupport::Inflector.underscore(self.class.name) + args.shift, *args)
end
def t!(*args)
- I18n.t!(self.class.name.underscore + args.shift, *args)
+ I18n.t!(ActiveSupport::Inflector.underscore(self.class.name) + args.shift, *args)
end
def to_model
raise 'Presenter objects should not be used for forms. Call .resource on this Presenter'
end