diff options
| author | Zog | 2018-01-15 18:22:53 +0100 |
|---|---|---|
| committer | Zog | 2018-01-25 17:17:58 +0100 |
| commit | 936650bb189104bf9795e0a7e9b5b8044e59a652 (patch) | |
| tree | ebca427ef1127cde9d62dd902e521c2e9bec7507 /lib | |
| parent | aefa3a507239b57ebdfe9110f3c8c789da6bce26 (diff) | |
| download | chouette-core-936650bb189104bf9795e0a7e9b5b8044e59a652.tar.bz2 | |
Refs #5586 @2h; Refactor the whole thing
We now have a ModelDecorator and an "instance" decorator, all in the
same file, with the same API.
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/af83/decorator.rb | 88 | ||||
| -rw-r--r-- | lib/af83/enhanced_decorator.rb | 80 |
2 files changed, 99 insertions, 69 deletions
diff --git a/lib/af83/decorator.rb b/lib/af83/decorator.rb index 6c7dff59d..79694ee59 100644 --- a/lib/af83/decorator.rb +++ b/lib/af83/decorator.rb @@ -1,80 +1,25 @@ -class AF83::Decorator < Draper::Decorator - def self.action_link args={} - args[:if] = @_condition if args[:if].nil? +class AF83::Decorator < ModelDecorator + include AF83::EnhancedDecorator + extend AF83::EnhancedDecorator::ClassMethods - options, link_options = parse_options args - - link = Link.new(link_options) - yield link if block_given? - raise IncompleteLinkDefinition.new(link.errors) unless link.complete? - - weight = options[:weight] || 1 - @_action_links ||= [] - @_action_links[weight] ||= [] - @_action_links[weight] << link - end - - def self.with_condition condition, &block - @_condition = condition - instance_eval &block - @_condition = nil - end - - def self.action_links action - (@_action_links || []).flatten.compact.select{|l| l.for_action?(action)} - end - - def action_links action=:index, opts={} - links = ActionLinks.new links: self.class.action_links(action), context: self, action: action - group = opts[:group] - links = links.for_group opts[:group] - links + def self.decorates klass + instance_decorator.decorates klass end - def primary_links action=:index - action_links(action, group: :primary) + def self.instance_decorator + @instance_decorator ||= Class.new(AF83::Decorator::InstanceDecorator) end - def secondary_links action=:index - action_links(action, group: :secondary) + def self.with_instance_decorator + yield instance_decorator end - def check_policy policy - _object = policy.to_s == "create" ? object.class : object - method = "#{policy}?" - h.policy(_object).send(method) - end - - private - def self.parse_options args - options = {} - %i(weight primary secondary footer on action actions policy if groups group).each do |k| - options[k] = args.delete(k) if args.has_key?(k) - end - link_options = args.dup - - actions = options.delete :actions - actions ||= options.delete :on - actions ||= [options.delete(:action)] - actions = [actions] unless actions.is_a?(Array) - link_options[:_actions] = actions.compact - - link_options[:_groups] = options.delete(:groups) - link_options[:_groups] ||= {} - if single_group = options.delete(:group) - if(single_group.is_a?(Symbol) || single_group.is_a?(String)) - link_options[:_groups][single_group] = true - else - link_options[:_groups].update single_group - 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) end - link_options[:_groups][:primary] ||= options.delete :primary - link_options[:_groups][:secondary] ||= options.delete :secondary - link_options[:_groups][:footer] ||= options.delete :footer - - link_options[:_if] = options.delete(:if) - link_options[:_policy] = options.delete(:policy) - [options, link_options] end class ActionLinks @@ -267,4 +212,9 @@ class AF83::Decorator < Draper::Decorator class IncompleteLinkDefinition < RuntimeError end + + class InstanceDecorator < Draper::Decorator + include AF83::EnhancedDecorator + extend AF83::EnhancedDecorator::ClassMethods + end end diff --git a/lib/af83/enhanced_decorator.rb b/lib/af83/enhanced_decorator.rb new file mode 100644 index 000000000..41cc17f85 --- /dev/null +++ b/lib/af83/enhanced_decorator.rb @@ -0,0 +1,80 @@ +module AF83::EnhancedDecorator + module ClassMethods + def action_link args={} + args[:if] = @_condition if args[:if].nil? + + options, link_options = parse_options args + + link = AF83::Decorator::Link.new(link_options) + yield link if block_given? + raise AF83::Decorator::IncompleteLinkDefinition.new(link.errors) unless link.complete? + + weight = options[:weight] || 1 + @_action_links ||= [] + @_action_links[weight] ||= [] + @_action_links[weight] << link + end + + def with_condition condition, &block + @_condition = condition + instance_eval &block + @_condition = nil + end + + def action_links action + (@_action_links || []).flatten.compact.select{|l| l.for_action?(action)} + end + + def parse_options args + options = {} + %i(weight primary secondary footer on action actions policy if groups group).each do |k| + options[k] = args.delete(k) if args.has_key?(k) + end + link_options = args.dup + + actions = options.delete :actions + actions ||= options.delete :on + actions ||= [options.delete(:action)] + actions = [actions] unless actions.is_a?(Array) + link_options[:_actions] = actions.compact + + link_options[:_groups] = options.delete(:groups) + link_options[:_groups] ||= {} + if single_group = options.delete(:group) + if(single_group.is_a?(Symbol) || single_group.is_a?(String)) + link_options[:_groups][single_group] = true + else + link_options[:_groups].update single_group + end + end + link_options[:_groups][:primary] ||= options.delete :primary + link_options[:_groups][:secondary] ||= options.delete :secondary + link_options[:_groups][:footer] ||= options.delete :footer + + link_options[:_if] = options.delete(:if) + link_options[:_policy] = options.delete(:policy) + [options, link_options] + end + end + + def action_links action=:index, opts={} + links = AF83::Decorator::ActionLinks.new links: self.class.action_links(action), context: self, action: action + group = opts[:group] + links = links.for_group opts[:group] + links + end + + def primary_links action=:index + action_links(action, group: :primary) + end + + def secondary_links action=:index + action_links(action, group: :secondary) + end + + def check_policy policy + _object = policy.to_s == "create" ? object.klass : object + method = "#{policy}?" + h.policy(_object).send(method) + end +end |
