Class: FriendlyAttributes::DetailsDelegator

Inherits:
Object
  • Object
show all
Defined in:
lib/friendly_attributes/details_delegator.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(friendly_model, ar_model, attributes, options = {}) ⇒ DetailsDelegator

Initialize new DetailsDelegator instance.

Parameters:

  • friendly_model (Class)

    FriendlyAttributes model, that inherits from FriendlyModel::Base

  • ar_model (Class)

    ActiveRecord model, host for the FriendlyAttributes model

  • attributes (Hash)
  • options (Hash) (defaults to: {})

Options Hash (options):

  • :active_record_key (Symbol) — default: :active_record_id

    name of the ‘foreign key’ in which the FriendlyModel::Base instance keeps a reference to the ActiveRecord model



45
46
47
48
49
50
51
52
53
# File 'lib/friendly_attributes/details_delegator.rb', line 45

def initialize(friendly_model, ar_model, attributes, options={})
  @active_record_model  = ar_model
  @friendly_model       = friendly_model
  @attributes           = attributes
  @delegated_attributes = {}
  
  setup_friendly_model(options.delete(:active_record_key) || :active_record_id)
  setup_active_record_model
end

Instance Attribute Details

#active_record_modelObject (readonly)



6
7
8
# File 'lib/friendly_attributes/details_delegator.rb', line 6

def active_record_model
  @active_record_model
end

#attributesObject (readonly)



6
7
8
# File 'lib/friendly_attributes/details_delegator.rb', line 6

def attributes
  @attributes
end

#delegated_attributesObject (readonly)



6
7
8
# File 'lib/friendly_attributes/details_delegator.rb', line 6

def delegated_attributes
  @delegated_attributes
end

#friendly_modelObject (readonly)



6
7
8
# File 'lib/friendly_attributes/details_delegator.rb', line 6

def friendly_model
  @friendly_model
end

Class Method Details

.friendly_model_ivar(name_or_klass) ⇒ Symbol

Instance variable name for the FriendlyAttributes::Base or ‘friendly_model_name` passed to it.

Parameters:

  • name_or_klass (Symbol, String, Class)

    Class or name for which to generate the ivar name

Returns:

  • (Symbol)

    ivar name



23
24
25
26
# File 'lib/friendly_attributes/details_delegator.rb', line 23

def friendly_model_ivar(name_or_klass)
  name_or_klass = friendly_model_name(name_or_klass) if name_or_klass.is_a?(Class)
  :"@#{name_or_klass}_ivar"
end

.friendly_model_name(klass) ⇒ Symbol

Method name for the FriendlyAttributes::Base class passed to it.

Parameters:

  • klass (Class)

    class we want to get the name for

Returns:

  • (Symbol)

    underscored name of the class.



15
16
17
# File 'lib/friendly_attributes/details_delegator.rb', line 15

def friendly_model_name(klass)
  klass.name.underscore.to_sym
end

.friendly_model_reader(name_or_klass) ⇒ Symbol

Reader method name for a certain FriendlyAttributes::Base instance associated with the model.

Parameters:

  • name_or_klass (Symbol, String, Class)

    Class or name for which to generate the reader method name

Returns:

  • (Symbol)

    reader method name



32
33
34
35
# File 'lib/friendly_attributes/details_delegator.rb', line 32

def friendly_model_reader(name_or_klass)
  name_or_klass = friendly_model_name(name_or_klass) if name_or_klass.is_a?(Class)
  :"load_#{name_or_klass}"
end

Instance Method Details

#delegated_attribute(name, klass) ⇒ Object



55
56
57
58
59
60
61
62
63
# File 'lib/friendly_attributes/details_delegator.rb', line 55

def delegated_attribute(name, klass)
  delegated_attributes[name] = klass
  
  attribute name, klass
  delegated_method(:"#{name}")
  delegated_method(:"#{name}=")
  
  active_record_model.friendly_attributes_configuration.add_attribute(name, friendly_model)
end

#delegated_method(name) ⇒ Object



65
66
67
68
69
70
71
# File 'lib/friendly_attributes/details_delegator.rb', line 65

def delegated_method(name)
  friendly_model = self.friendly_model
  
  active_record_model.class_eval do
    delegate :"#{name}", :to => FriendlyAttributes::DetailsDelegator.friendly_model_reader(friendly_model)
  end
end

#setup_delegated_attributesObject



73
74
75
76
77
78
79
80
81
# File 'lib/friendly_attributes/details_delegator.rb', line 73

def setup_delegated_attributes
  attributes.each do |key, value|
    if Array === value
      value.each { |v| delegated_attribute v, key }
    else
      delegated_attribute value, key
    end
  end
end