Module: Merge::Base::ClassMethods

Defined in:
lib/merge/base.rb

Instance Method Summary collapse

Instance Method Details

#default_selectsObject



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_methodsObject



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 "    def __temp__\n      if @attributes.has_key?(\"\#{attr_name}\")\n        (v=@attributes[\"\#{attr_name}\"]) && \#{attribute_cast_code(attr_name)}\n      else\n        \#{assoc_name}.\#{attr_name}\n      end\n    end\n    alias_method :\"\#{attr_name}\", :__temp__\n    undef_method :__temp__\n\n    def __temp__\n      !!send(:\"\#{attr_name}\")\n    end\n\n    alias_method '\#{attr_name}?', :__temp__\n    undef_method :__temp__\n  RUBY\nend\n", __FILE__, __LINE__ + 1

#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, options = 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, options)

  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