Module: ROM::Rails::ControllerExtension::ClassExtensions

Defined in:
lib/rom/rails/controller_extension.rb

Instance Method Summary collapse

Instance Method Details

#relation(path, options) ⇒ Object



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
# File 'lib/rom/rails/controller_extension.rb', line 15

def relation(path, options)
  root, method = path.split('.').map(&:to_sym)

  name = options.fetch(:as) { root }
  requires = Array(options.fetch(:requires) { [] })

  before_filter(options.except(:as, :requires)) do
    args = params.values_at(*requires)

    if requires.any? && args.none?
      raise RelationParamsMissingError
    else
      relation =
        if args.any?
          rom.read(root).send(method, *args)
        else
          rom.read(root).send(method)
        end

      instance_variable_set("@#{name}", relation.to_a)
    end
  end

  unless respond_to?(name)
    attr_reader name
    helper_method name
  end
end