Module: InheritedResources::Helpers::Accessors::ClassMethods

Defined in:
lib/inherited_resources/helpers/accessors.rb

Instance Method Summary collapse

Instance Method Details

#begin_of_association_chain(symbol) ⇒ Object



9
10
11
12
# File 'lib/inherited_resources/helpers/accessors.rb', line 9

def begin_of_association_chain(symbol)
  resources_configuration[:self][:begin_of_association_chain] = symbol
  define_method(:_begin_of_association_chain) { send(symbol) }
end

#define_resource_accessor(name, config, target) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/inherited_resources/helpers/accessors.rb', line 29

def define_resource_accessor(name, config, target)
  target ||= config[:parent_class] || resource_class
  param  = config[:param] || :id

  if name == resources_configuration[:self][:instance_name]
    define_method(name) { resource }
  elsif target.is_a?(Symbol)
    define_method(name) { send(target).send(config[:collection_name], params[param]).first }
  else
    define_method(name) { target.send(config[:finder] || :find, params[param]) }
  end
end

#define_resource_accessors(controller) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/inherited_resources/helpers/accessors.rb', line 14

def define_resource_accessors(controller)
  symbols = parents_symbols + [:self]
  targets = [resources_configuration[:self][:begin_of_association_chain]].compact

  symbols.uniq.inject(targets) do |parents, symbol|
    config = resources_configuration[symbol]
    name   = symbol == :self ? config[:instance_name] : symbol

    define_resource_accessor(name, config, parents.last)
    helper_method(name)
    parents << name
  end
  @resource_accessors_defined = true
end

#resource_accessor?(name) ⇒ Boolean

Returns:

  • (Boolean)


46
47
48
# File 'lib/inherited_resources/helpers/accessors.rb', line 46

def resource_accessor?(name)
  resource_accessors.include?(name.to_sym)
end

#resource_accessorsObject



50
51
52
# File 'lib/inherited_resources/helpers/accessors.rb', line 50

def resource_accessors
  @resource_accessors ||= resources_configuration.except(:polymorphic).values.map { |c| c[:instance_name] }
end

#resource_accessors_defined?Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/inherited_resources/helpers/accessors.rb', line 42

def resource_accessors_defined?
  !!@resource_accessors_defined
end