Module: ActiveRecord::Fusion::ClassMethods

Defined in:
lib/active_record/fusion.rb

Instance Method Summary collapse

Instance Method Details

#define_attribute_methodsObject



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/active_record/fusion.rb', line 49

def define_attribute_methods
  @attribute_methods_mutex.synchronize do
    return if attribute_methods_generated?

    fusion_associations.each_value do |assoc|
      assoc.each_column do |name, column, serialized|
        columns_hash[name] = column
        serialized_attributes[name] = serialized if serialized
        define_fusion_attribute_methods(assoc.name, name)
      end
    end
  end

  super
end

#define_fusion_attribute_methods(assoc_name, attr_name) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/active_record/fusion.rb', line 65

def define_fusion_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

#fuse(*assocs) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/active_record/fusion.rb', line 29

def fuse(*assocs)
  context = scoped
  
  if context.select_values.empty?
    context = context.select(arel_table[Arel::star])
  end
  
  assocs = if assocs.any?
    fusion_associations.values_at(*assocs)
  else
    fusion_associations.values
  end
  
  assocs.each do |assoc|
    context = context.select(assoc.selects).joins(assoc.joins)
  end
  
  context
end

#fusion(name, options = nil) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/active_record/fusion.rb', line 13

def fusion(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, "Only belongs_to and has_one associations can be fused"
  end

  assoc = Association.new(reflection, options)

  self.fusion_associations = fusion_associations.merge(name => assoc)
end