Module: Stonean::ClassyInheritance::ClassMethods

Defined in:
lib/classy-inheritance.rb

Instance Method Summary collapse

Instance Method Details

#can_be(model_sym, options = {}) ⇒ Object



149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/classy-inheritance.rb', line 149

def can_be(model_sym, options = {})
  unless options[:as]
    raise ArgumentError, ":as attribute required when calling can_be"
  end

  klass = model_sym.to_s.classify

  define_method "is_a_#{model_sym}?" do
    eval("self.#{options[:as]}_type == '#{klass}'")
  end

  find_with_method = "find_with_#{self.name.underscore}"

  define_method "as_a_#{model_sym}" do
    eval("#{klass}.send(:#{find_with_method},self.#{options[:as]}_id)")
  end
end

#depends_on(model_sym, options = {}) ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/classy-inheritance.rb', line 112

def depends_on(model_sym, options = {}) 
  define_relationship(model_sym,options)

  # Optional presence of handling
  if options.has_key?(:validates_presence_if) && options[:validates_presence_if] != true
    if [Symbol, String, Proc].include?(options[:validates_presence_if].class)
      validates_presence_of model_sym, :if => options[:validates_presence_if]
    end
  else
    validates_presence_of model_sym
  end

  if options.has_key?(:validates_associated_if) && options[:validates_associated_if] != true
    if [Symbol, String, Proc].include?(options[:validates_associated_if].class)
      validates_associated_dependent model_sym, options, :if => options[:validates_associated_if]
    end
  else
    validates_associated_dependent model_sym, options 
  end

  # Before save functionality to create/update the requisite object
  define_save_method(model_sym, options[:as])

  # Adds a find_with_<model_sym> class method
  define_find_with_method(model_sym)

  if options[:as]
    define_can_be_method_on_requisite_class(options[:class_name] || model_sym.to_s.classify, options[:as])
  end

  options[:attrs].each{|attr| define_accessors(model_sym, attr, options)}
end

#has_dependency(model_sym, options = {}) ⇒ Object



145
146
147
# File 'lib/classy-inheritance.rb', line 145

def has_dependency(model_sym, options = {})
  depends_on(model_sym, options.update(:has_dependency => true))
end

#has_one(association_id, options = {}) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/classy-inheritance.rb', line 80

def has_one(association_id, options = {})
  if options[:through]
    reflection = create_has_one_through_reflection(association_id, options)
    association_accessor_methods(reflection, ActiveRecord::Associations::HasOneThroughAssociation)
  else
    reflection = create_has_one_reflection(association_id, options)

    ivar = "@#{reflection.name}"

    method_name = "has_one_after_save_for_#{reflection.name}".to_sym
    define_method(method_name) do
      association = instance_variable_get(ivar) if instance_variable_defined?(ivar)
    
      primary_key = reflection.options[:primary_key] || :id
      if !association.nil? && (new_record? || association.new_record? || association[reflection.primary_key_name] != send(primary_key))
        association[reflection.primary_key_name] = send(primary_key)
        association.save(true)
      end
    end
    after_save method_name

    add_single_associated_validation_callbacks(reflection.name) if options[:validate] == true
    association_accessor_methods(reflection, ActiveRecord::Associations::HasOneAssociation)
    association_constructor_method(:build,  reflection, ActiveRecord::Associations::HasOneAssociation)
    association_constructor_method(:create, reflection, ActiveRecord::Associations::HasOneAssociation)

    configure_dependency_for_has_one(reflection)
  end
end