aboutsummaryrefslogtreecommitdiffstats
path: root/lib/af83/decorator.rb
blob: 79694ee59fde4f68d9bcc2831831ec5f544d96e0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
class AF83::Decorator < ModelDecorator
  include AF83::EnhancedDecorator
  extend AF83::EnhancedDecorator::ClassMethods

  def self.decorates klass
    instance_decorator.decorates klass
  end

  def self.instance_decorator
    @instance_decorator ||= Class.new(AF83::Decorator::InstanceDecorator)
  end

  def self.with_instance_decorator
    yield instance_decorator
  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
  end

  class ActionLinks
    attr_reader :options

    def initialize opts
      @options = opts.deep_dup
    end

    def for_group group
      returning_a_copy do
        @options[:groups] = [group] if group.present?
      end
    end

    def for_groups groups
      returning_a_copy do
        @options[:groups] = groups if groups.present?
      end
    end

    def primary
      for_group :primary
    end

    def secondary
      for_group :secondary
    end

    def resolve
      out = @options[:links].map{|l| l.bind_to_context(@options[:context])}.select{|l| l.enabled?}
      if @options[:groups].present?
        out = out.select do |l|
          @options[:groups].any? do |g|
            l.in_group_for_action?(@options[:action], g)
          end
        end
      end
      out
    end

    def grouped_by *groups
      add_footer = groups.include?(:footer)
      groups -= [:footer]
      out = HashWithIndifferentAccess[*groups.map{|g| [g, []]}.flatten(1)]
      out[:other] = []
      if add_footer
        out[:footer] = []
        groups << :footer
      end

      each do |l|
        found = false
        groups.each do |g|
          if l.in_group_for_action?(@options[:action], g)
            out[g] << l
            found = true
            next
          end
        end
        out[:other] << l unless found
      end
      out
    end

    alias_method :to_ary, :resolve

    %w(each map size first last any?).each do |meth|
      define_method meth do |*args, &block|
        resolve.send meth, *args, &block
      end
    end

    private
    def returning_a_copy &block
      out = ActionLinks.new options
      out.instance_eval &block
      out
    end
  end

  class Link
    REQUIRED_ATTRIBUTES = %i(href content)

    attr_reader :context

    def initialize options={}
      @options = options
    end

    def bind_to_context context
      @context = context
      self
    end

    def method *args
      link_method *args
    end

    def class *args
      link_class *args
    end

    def method_missing name, *args, &block
      if block_given?
        @options[name] = block
      elsif args.size == 0
        out = @options[name]
        out = context.instance_exec(&out) if out.is_a?(Proc)
        out
      else
        @options[name] = args.first
      end
    end

    def options
      @options.symbolize_keys
    end

    def complete?
      @missing_attributes = REQUIRED_ATTRIBUTES.select{|a| !@options[a].present?}
      @missing_attributes.empty?
    end

    def enabled_actions
      @options[:_actions].map(&:to_s) || []
    end

    def for_action? action
      enabled_actions.empty? || enabled_actions.include?(action.to_s)
    end

    def actions_for_group group
      val = @options[:_groups][group]
      val.is_a?(Array) ? val.map(&:to_s) : val
    end

    def in_group_for_action? action, group
      vals = actions_for_group(group)
      if vals.is_a?(Array)
        return vals.include?(action.to_s)
      elsif vals.is_a?(String) || vals.is_a?(Symbol)
        vals.to_s == action.to_s
      else
        !!vals
      end
    end

    def enabled?
      enabled = false
      if @options[:_if].nil?
        enabled = true
      elsif @options[:_if].is_a?(Proc)
        enabled = context.instance_exec(&@options[:_if])
      else
        enabled = !!@options[:_if]
      end

      enabled = enabled && check_policy(@options[:_policy]) if @options[:_policy].present?

      enabled
    end

    def check_policy(policy)
      @context.check_policy policy
    end

    def errors
      "Missing attributes: #{@missing_attributes.to_sentence}"
    end

    def html_options
      out = {}
      options.each do |k, v|
        out[k] = v unless k == :content || k == :href || k.to_s =~ /^_/
      end
      out[:class] = out.delete(:link_class)
      out
    end

    def to_html
      if block_given?
        link = Link.new(@options)
        yield link
        return link.bind_to_context(context).to_html
      end
      context.h.link_to content, href, html_options
    end
  end

  class IncompleteLinkDefinition < RuntimeError
  end

  class InstanceDecorator < Draper::Decorator
    include AF83::EnhancedDecorator
    extend AF83::EnhancedDecorator::ClassMethods
  end
end