diff options
| author | Zog | 2018-01-31 11:39:39 +0100 |
|---|---|---|
| committer | Zog | 2018-01-31 11:39:39 +0100 |
| commit | fe7915ffac359db41c7737a3847f31a728f502e6 (patch) | |
| tree | e733b44018321cc8eec28b147813cb279db40b7a /lib | |
| parent | 4023ea52097a47458ac2fcad36d343aba0c8e68b (diff) | |
| download | chouette-core-fe7915ffac359db41c7737a3847f31a728f502e6.tar.bz2 | |
Refs #5683 @2H; Fix specs and refactor action_links
Note: Did not fix the missing workgroup in the calendar mailer
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/af83/decorator.rb | 63 | ||||
| -rw-r--r-- | lib/af83/decorator/enhanced_decorator.rb | 22 | ||||
| -rw-r--r-- | lib/af83/decorator/link.rb | 3 |
3 files changed, 56 insertions, 32 deletions
diff --git a/lib/af83/decorator.rb b/lib/af83/decorator.rb index f990555fe..71cf1170d 100644 --- a/lib/af83/decorator.rb +++ b/lib/af83/decorator.rb @@ -2,40 +2,49 @@ class AF83::Decorator < ModelDecorator include AF83::Decorator::EnhancedDecorator extend AF83::Decorator::EnhancedDecorator::ClassMethods - def self.decorates klass - instance_decorator.decorates klass - end + class << self + def decorates klass + instance_decorator.decorates klass + end - def self.instance_decorator - @instance_decorator ||= begin - klass = Class.new(AF83::Decorator::InstanceDecorator) - klass.delegate_all - klass + def instance_decorator + @instance_decorator ||= begin + klass = Class.new(AF83::Decorator::InstanceDecorator) + klass.delegate_all + klass + end end - end - def self.with_instance_decorator - @_with_instance_decorator = true - yield instance_decorator - @_with_instance_decorator = false - end + def with_instance_decorator + @_with_instance_decorator = true + yield instance_decorator + @_with_instance_decorator = false + end + + def decorate object, options = {} + if object.is_a?(ActiveRecord::Base) + return instance_decorator.decorate object, options + else + self.new object, options.update(with: instance_decorator) + end + end - def self.decorate object, options = {} - if object.is_a?(ActiveRecord::Base) - return instance_decorator.decorate object, options - else - self.new object, options.update(with: instance_decorator) + def define_instance_method method_name, &block + instance_decorator.send(:define_method, method_name, &block) end - end - def self.define_instance_method method_name, &block - instance_decorator.send(:define_method, method_name, &block) - end + # Defines a class method on the decorated object's class. These + # can be called like `object.class.my_method`. + def define_instance_class_method method_name, &block + instance_decorator.send(:define_singleton_method, method_name, &block) + end + + def set_scope_with_instance_decorator value=nil, &block + set_scope_without_instance_decorator value, &block + instance_decorator.set_scope value, &block + end - # Defines a class method on the decorated object's class. These - # can be called like `object.class.my_method`. - def self.define_instance_class_method method_name, &block - instance_decorator.send(:define_singleton_method, method_name, &block) + alias_method_chain :set_scope, :instance_decorator end class ActionLinks diff --git a/lib/af83/decorator/enhanced_decorator.rb b/lib/af83/decorator/enhanced_decorator.rb index 904d1b2da..fff8bb8b3 100644 --- a/lib/af83/decorator/enhanced_decorator.rb +++ b/lib/af83/decorator/enhanced_decorator.rb @@ -25,7 +25,7 @@ module AF83::Decorator::EnhancedDecorator policy: :create, before_block: -> (l){ l.content { h.t('actions.add') } - l.href { [:new, object.klass.name.underscore.singularize] } + l.href { [:new, scope, object.klass.model_name.singular] } } } action_link opts.update(args), &block @@ -37,7 +37,7 @@ module AF83::Decorator::EnhancedDecorator primary: :index, before_block: -> (l){ l.content { h.t('actions.show') } - l.href { [object] } + l.href { [scope, object] } } } action_link opts.update(args), &block @@ -49,7 +49,7 @@ module AF83::Decorator::EnhancedDecorator policy: :edit, before_block: -> (l){ l.content { h.t('actions.edit') } - l.href { [:edit, object] } + l.href { [:edit, scope, object] } } } action_link opts.update(args), &block @@ -62,7 +62,7 @@ module AF83::Decorator::EnhancedDecorator secondary: :show, before_block: -> (l){ l.content { h.destroy_link_content } - l.href { [object] } + l.href { [scope, object] } l.method :delete l.data {{ confirm: h.t('actions.destroy_confirm') }} } @@ -70,6 +70,14 @@ module AF83::Decorator::EnhancedDecorator action_link opts.update(args), &block end + def set_scope value=nil, &block + @scope = value || block + end + + def scope + @scope + end + def t key eval "-> (l){ h.t('#{key}') }" end @@ -142,4 +150,10 @@ module AF83::Decorator::EnhancedDecorator def check_feature feature h.has_feature? feature end + + def scope + scope = self.class.scope + scope = instance_exec &scope if scope.is_a? Proc + scope + end end diff --git a/lib/af83/decorator/link.rb b/lib/af83/decorator/link.rb index 7d2896e6a..de7106740 100644 --- a/lib/af83/decorator/link.rb +++ b/lib/af83/decorator/link.rb @@ -30,7 +30,8 @@ class AF83::Decorator::Link @options[name] = block elsif args.size == 0 out = @options[name] - out = context.instance_exec(self, &out) if out.is_a?(Proc) + out = context.instance_exec(self, &out) if out.is_a?(Proc) + out = out.flatten.compact if name.to_s == "href" && out.is_a?(Array) out else # we can use l.foo("bar") or l.foo = "bar" |
