Module: FriendlyAttributes::InstanceMethods

Defined in:
lib/friendly_attributes/instance_methods.rb

Instance Method Summary collapse

Instance Method Details

#all_friendly_instancesArray<FriendlyAttributes::Base>

List of all the FriendlyAttributes::Base instances associated with the model. Forces loading if the details have not been loaded yet.

Returns:



110
111
112
113
114
# File 'lib/friendly_attributes/instance_methods.rb', line 110

def all_friendly_instances
  friendly_attributes_configuration.friendly_models.map do |friendly_model|
    send(DetailsDelegator.friendly_model_reader(friendly_model))
  end
end

#changed?true, false

Returns if the record has been changed and should be saved, taking into account any FriendlyAttributes.

Overloads ActiveRecord::Base#changed?

Returns:

  • (true, false)


131
132
133
# File 'lib/friendly_attributes/instance_methods.rb', line 131

def changed?
  super || present_friendly_instances.any?(&:changed?)
end

#destroy_friendly_modelstrue, false

Destroys all FriendlyAttributes associated with the model. Forces loading and sends :destroy to all associated Friendly models.

Returns:

  • (true, false)

    result of attempting to destroy the associated FriendlyAttributes



39
40
41
# File 'lib/friendly_attributes/instance_methods.rb', line 39

def destroy_friendly_models
  all_friendly_instances.map(&:destroy).all?
end

#find_or_build_and_memoize_details(friendly_model) ⇒ Object

Finds or builds the Friendly instance associated through friendly_model. Result is memoized in an instance variable.

Parameters:

  • friendly_model (Class)

    FriendlyAttributes::Base subclass

See Also:



75
76
77
78
79
80
81
82
83
84
# File 'lib/friendly_attributes/instance_methods.rb', line 75

def find_or_build_and_memoize_details(friendly_model)
  friendly_model_ivar = DetailsDelegator.friendly_model_ivar(friendly_model)
  
  val = instance_variable_get(friendly_model_ivar)
  return val if val.present?
  
  instance_variable_set(friendly_model_ivar,
    friendly_model.
    find_or_build_by_active_record_id(id, friendly_details_build_options(friendly_model)))
end

#friendly_details_build_options(friendly_model = nil) ⇒ Hash

Hook provided in order to customize the defaults for building Friendly model instances associated with a certain model. Redefine the method on the FriendlyAttributes::Base subclass to customize.

Defaults to an empty Hash.

Examples:

We want to specify build options for the UserDetails instances, but not for UserSecondDetails

class User < ActiveRecord::Base
  include FriendlyAttributes

  friendly_details(UserDetails, Integer => :shoe_size)
  friendly_details(UserSecondDetails, Integer => :second_int)

  def friendly_details_build_options(friendly_model)
    if UserDetails == friendly_model
      { :shoe_size => 42 }
    else
      {}
    end
  end
end

Parameters:

  • friendly_model (Class) (defaults to: nil)

    FriendlyAttributes::Base subclass for which the build options should be returned

Returns:

  • (Hash)

    default attributes to be used when building the associated friendly_model



66
67
68
# File 'lib/friendly_attributes/instance_methods.rb', line 66

def friendly_details_build_options(friendly_model = nil)
  {}
end

#friendly_instance_for_attribute(attr) ⇒ Class

Returns the Friendly instance corresponding to the specified attribute

Parameters:

  • attr (Symbol, String)

    name of the attribute

Returns:

  • (Class)

    FriendyAttributes::Base instance



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

def friendly_instance_for_attribute(attr)
  klass = friendly_attributes_configuration.model_for_attribute(attr)
  send DetailsDelegator.friendly_model_reader(klass)
end

#friendly_instance_presence(friendly_model) ⇒ FriendlyAttributes::Base?

Returns the associated FriendlyAttributes instance, if it has been loaded. If not loaded, returns nil.

Parameters:

  • friendly_model (Class, Symbol, String)

    Class or name of the FriendlyAttributes model

Returns:



100
101
102
103
104
# File 'lib/friendly_attributes/instance_methods.rb', line 100

def friendly_instance_presence(friendly_model)
  friendly_instance_present?(friendly_model) ?
    send(DetailsDelegator.friendly_model_reader(friendly_model)) :
    nil
end

#friendly_instance_present?(friendly_model) ⇒ true, false

Returns true if the FriendlyAttributes specified instance is loaded.

Parameters:

  • friendly_model (Class, Symbol, String)

    Class or name of the FriendlyAttributes model

Returns:

  • (true, false)

    is the FriendlyAttributes instance loaded



90
91
92
93
94
# File 'lib/friendly_attributes/instance_methods.rb', line 90

def friendly_instance_present?(friendly_model)
  friendly_model_ivar = DetailsDelegator.friendly_model_ivar(friendly_model)
  val = instance_variable_get(friendly_model_ivar)
  val.present?
end

#present_friendly_instancesArray<FriendlyAttributes::Base>

List of FriendlyAttributes::Base instances that have been loaded. Does not force loading of details not loaded yet.

Returns:



120
121
122
123
124
# File 'lib/friendly_attributes/instance_methods.rb', line 120

def present_friendly_instances
  friendly_attributes_configuration.friendly_models.map do |friendly_model|
    friendly_instance_presence(friendly_model)
  end.compact
end

#read_friendly_attribute(attr) ⇒ Object

Read the value of a Friendly attribute

Parameters:

  • attr (Symbol, String)

    name of the attribute to read

Returns:

  • (Object)

    value of the read attribute



7
8
9
# File 'lib/friendly_attributes/instance_methods.rb', line 7

def read_friendly_attribute(attr)
  friendly_instance_for_attribute(attr).send(attr)
end

#update_friendly_modelsObject

Update all associated Friendly instances, if they have been changed. If assigning attributes resulted in new instances being built, they will be created.



30
31
32
33
34
# File 'lib/friendly_attributes/instance_methods.rb', line 30

def update_friendly_models
  present_friendly_instances.each do |details|
    details.update_if_changed_with_model(id)
  end
end

#write_friendly_attribute(attr, value) ⇒ Object

Write the value of a Friendly attribute

Parameters:

  • attr (Symbol, String)

    name of the attribute to set

  • value (Object)

    value to set the attribute to



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

def write_friendly_attribute(attr, value)
  friendly_instance_for_attribute(attr).send(:"#{attr}=", value)
end