aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/dynamic_matchers.rb
blob: f3f143940fd1162656dfaa3a2fb95a9a0d039527 (plain) (blame)
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
module ActiveRecord
  module DynamicMatchers
    def respond_to?(name, include_private = false)
      match = Method.match(self, name)
      match && match.valid? || super
    end

    private

    # Enables dynamic finders like <tt>User.find_by_user_name(user_name)</tt> and
    # <tt>User.scoped_by_user_name(user_name). Refer to Dynamic attribute-based finders
    # section at the top of this file for more detailed information.
    #
    # It's even possible to use all the additional parameters to +find+. For example, the
    # full interface for +find_all_by_amount+ is actually <tt>find_all_by_amount(amount, options)</tt>.
    #
    # Each dynamic finder using <tt>scoped_by_*</tt> is also defined in the class after it
    # is first invoked, so that future attempts to use it do not run through method_missing.
    def method_missing(name, *arguments, &block)
      match = Method.match(self, name)

      if match && match.valid?
        match.define
        send(name, *arguments, &block)
      else
        super
      end
    end

    class Method
      def self.match(model, name)
        klass = klasses.find { |k| name =~ k.pattern }
        klass.new(model, name) if klass
      end

      def self.klasses
        [
          FindBy, FindAllBy, FindLastBy, FindByBang, ScopedBy,
          FindOrInitializeBy, FindOrCreateBy, FindOrCreateByBang
        ]
      end

      def self.pattern
        /^#{prefix}_([_a-zA-Z]\w*)#{suffix}$/
      end

      def self.prefix
        raise NotImplementedError
      end

      def self.suffix
        ''
      end

      attr_reader :model, :name, :attribute_names

      def initialize(model, name)
        @model           = model
        @name            = name.to_s
        @attribute_names = @name.match(self.class.pattern)[1].split('_and_')
      end

      def expand_attribute_names_for_aggregates
        attribute_names.map do |attribute_name|
          if aggregation = model.reflect_on_aggregation(attribute_name.to_sym)
            model.send(:aggregate_mapping, aggregation).map do |field_attr, _|
              field_attr.to_sym
            end
          else
            attribute_name.to_sym
          end
        end.flatten
      end

      def valid?
        (expand_attribute_names_for_aggregates - model.column_methods_hash.keys).empty?
      end

      def define
        model.class_eval <<-CODE, __FILE__, __LINE__ + 1
          def self.#{name}(#{signature})
            #{body}
          end
        CODE
      end

      def body
        raise NotImplementedError
      end
    end

    class Finder < Method
      def body
        <<-CODE
          result = #{result}
          result && block_given? ? yield(result) : result
        CODE
      end

      def result
        "scoped.apply_finder_options(options).#{finder}(#{attributes_hash})"
      end

      def signature
        attribute_names.join(', ') + ", options = {}"
      end

      def attributes_hash
        "{" + attribute_names.map { |name| ":#{name} => #{name}" }.join(',') + "}"
      end

      def finder
        raise NotImplementedError
      end
    end

    class FindBy < Finder
      def self.prefix
        "find_by"
      end

      def finder
        "find_by"
      end
    end

    class FindByBang < Finder
      def self.prefix
        "find_by"
      end

      def self.suffix
        "!"
      end

      def finder
        "find_by!"
      end
    end

    class FindAllBy < Finder
      def self.prefix
        "find_all_by"
      end

      def finder
        "where"
      end

      def result
        "#{super}.to_a"
      end
    end

    class FindLastBy < Finder
      def self.prefix
        "find_last_by"
      end

      def finder
        "where"
      end

      def result
        "#{super}.last"
      end
    end

    class ScopedBy < Finder
      def self.prefix
        "scoped_by"
      end

      def body
        "where(#{attributes_hash})"
      end
    end

    class Instantiator < Method
      # This is nasty, but it doesn't matter because it will be deprecated.
      def self.dispatch(klass, attribute_names, instantiator, args, block)
        if args.length == 1 && args.first.is_a?(Hash)
          attributes = args.first.stringify_keys
          conditions = attributes.slice(*attribute_names)
          rest       = [attributes.except(*attribute_names)]
        else
          raise ArgumentError, "too few arguments" unless args.length >= attribute_names.length

          conditions = Hash[attribute_names.map.with_index { |n, i| [n, args[i]] }]
          rest       = args.drop(attribute_names.length)
        end

        klass.where(conditions).first ||
          klass.create_with(conditions).send(instantiator, *rest, &block)
      end

      def signature
        "*args, &block"
      end

      def body
        "#{self.class}.dispatch(self, #{attribute_names.inspect}, #{instantiator.inspect}, args, block)"
      end

      def instantiator
        raise NotImplementedError
      end
    end

    class FindOrInitializeBy < Instantiator
      def self.prefix
        "find_or_initialize_by"
      end

      def instantiator
        "new"
      end
    end

    class FindOrCreateBy < Instantiator
      def self.prefix
        "find_or_create_by"
      end

      def instantiator
        "create"
      end
    end

    class FindOrCreateByBang < Instantiator
      def self.prefix
        "find_or_create_by"
      end

      def self.suffix
        "!"
      end

      def instantiator
        "create!"
      end
    end

    def aggregate_mapping(reflection)
      mapping = reflection.options[:mapping] || [reflection.name, reflection.name]
      mapping.first.is_a?(Array) ? mapping : [mapping]
    end
  end
end