Module: Associates::ClassMethods

Defined in:
lib/associates.rb

Instance Method Summary collapse

Instance Method Details

#associate(model, options = {}) ⇒ Object

Defines an associated model

Examples:

class User
  include Associates

  associate :user, only: :username
end

Parameters:

  • model (Symbol, Class)
  • options (Hash) (defaults to: {})

Options Hash (options):

  • :only (Symbol, Array)

    Only generate methods for the given attributes

  • :except (Symbol, Array)

    Generate all the model’s methods except for the given attributes

  • :depends_on (Symbol)

    Specify one or more associate name on which the current associate model depends to be valid. Allow to automatically setup ‘belongs_to` associations between models

  • :class_name (String, Class)

    Specify the class name of the associate. Use it only if that name can’t be inferred from the associate’s name

  • :delegate (Boolean) — default: true

    Wether or not to delegate the associate’s attributes getter and setters methods to the associate instance



50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/associates.rb', line 50

def associate(model, options = {})
  options = {
    delegate: true
  }.merge(options)

  associate = build_associate(model, options)
  self.associates << associate

  define_associate_delegation(associate) if options[:delegate]
  define_associate_instance_setter_method(associate)
  define_associate_instance_getter_method(associate)
end