Module: Merge::Base::ClassMethods
- Defined in:
- lib/merge/base.rb
Instance Method Summary collapse
- #default_selects ⇒ Object
- #define_attribute_methods ⇒ Object
- #define_merge_attribute_methods(assoc_name, attr_name) ⇒ Object
- #merge(name, options = nil) ⇒ Object
Instance Method Details
#default_selects ⇒ Object
34 35 36 37 38 39 40 |
# File 'lib/merge/base.rb', line 34 def default_selects selects = [ arel_table[Arel::star] ] merges.each_value do |assoc| selects.concat(assoc.selects) end selects end |
#define_attribute_methods ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/merge/base.rb', line 42 def define_attribute_methods @attribute_methods_mutex.synchronize do return if attribute_methods_generated? merges.each_value do |assoc| assoc.each_column do |name, column, serialized| columns_hash[name] = column serialized_attributes[name] = serialized if serialized define_merge_attribute_methods(assoc.name, name) end end end super end |
#define_merge_attribute_methods(assoc_name, attr_name) ⇒ Object
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/merge/base.rb', line 56 def define_merge_attribute_methods(assoc_name, attr_name) generated_attribute_methods.module_eval <<-RUBY, __FILE__, __LINE__ + 1 def __temp__ if @attributes.has_key?("#{attr_name}") (v=@attributes["#{attr_name}"]) && #{attribute_cast_code(attr_name)} else #{assoc_name}.#{attr_name} end end alias_method :"#{attr_name}", :__temp__ undef_method :__temp__ def __temp__ !!send(:"#{attr_name}") end alias_method '#{attr_name}?', :__temp__ undef_method :__temp__ RUBY end |
#merge(name, options = nil) ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/merge/base.rb', line 13 def merge(name, = nil) reflection = reflect_on_association(name) if reflection.nil? raise ArgumentError, "#{self.name} has no association #{name.inspect}" end if reflection.collection? raise ArgumentError, "Merge only supports belongs_to and has_one associations" end assoc = Association.new(reflection, ) self.merges = merges.merge(name => assoc) default_scope do dep = ActiveRecord::Associations::JoinDependency.new(self, name, []) joins(dep.join_associations.each{ |a| a.join_type = Arel::OuterJoin }) end end |