Module: Mochigome::ModelExtensions::ClassMethods

Defined in:
lib/model_extensions.rb

Instance Method Summary collapse

Instance Method Details

#acts_as_mochigome_focus {|settings| ... } ⇒ Object

Yields:

  • (settings)


32
33
34
35
36
37
38
39
40
# File 'lib/model_extensions.rb', line 32

def acts_as_mochigome_focus
  if self.try(:mochigome_focus_settings).try(:model) == self
    raise Mochigome::ModelSetupError.new("Already acts_as_mochigome_focus for #{self.name}")
  end
  settings = ReportFocusSettings.new(self)
  yield settings if block_given?
  write_inheritable_attribute :mochigome_focus_settings, settings
  send(:include, InstanceMethods)
end

#acts_as_mochigome_focus?Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/model_extensions.rb', line 42

def acts_as_mochigome_focus?
  !!mochigome_focus_settings
end

#arel_primary_keyObject



28
29
30
# File 'lib/model_extensions.rb', line 28

def arel_primary_key
  arel_table[primary_key]
end

#arel_tableObject

TODO: Use this instead of calling Table.new all over the place



24
25
26
# File 'lib/model_extensions.rb', line 24

def arel_table
  Arel::Table.new(table_name)
end

#assoc_condition(name) ⇒ Object

Raises:



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/model_extensions.rb', line 62

def assoc_condition(name)
  # TODO: Deal with polymorphic assocs.
  model = self
  assoc = reflect_on_association(name)
  raise AssociationError.new("No such assoc #{name}") unless assoc
  table = Arel::Table.new(table_name)
  ftable = Arel::Table.new(assoc.klass.table_name)

  # FIXME: This acts as though arel methods are non-destructive,
  # but they are, right? Except, I can't remove the rel
  # assignment from relation_over_path...
  cond = nil
  if assoc.belongs_to?
    cond = table[assoc.association_foreign_key].eq(
      ftable[assoc.klass.primary_key]
    )
  else
    cond = table[primary_key].eq(ftable[assoc.primary_key_name])
  end

  if assoc.options[:as]
    # FIXME Can we really assume that this is the polymorphic type field?
    cond = cond.and(ftable["#{assoc.options[:as]}_type"].eq(model.name))
  end

  # TODO: Apply association conditions.

  return cond
end

#has_mochigome_aggregations(name = :default) {|settings| ... } ⇒ Object

TODO: Split out aggregation stuff into its own module

Yields:

  • (settings)


48
49
50
51
52
53
54
55
56
# File 'lib/model_extensions.rb', line 48

def has_mochigome_aggregations(name = :default)
  name = name.to_sym
  if mochigome_aggregation_settings_sets[name].try(:model) == self
    raise Mochigome::ModelSetupError.new("Can't overwrite aggregation settings for #{self.name}")
  end
  settings = AggregationSettings.new(self)
  yield settings if block_given?
  mochigome_aggregation_settings_sets[name] = settings
end

#mochigome_aggregation_settings(name = :default) ⇒ Object



58
59
60
# File 'lib/model_extensions.rb', line 58

def mochigome_aggregation_settings(name = :default)
  mochigome_aggregation_settings_sets[name]
end

#real_model?Boolean

Returns:

  • (Boolean)


15
16
17
# File 'lib/model_extensions.rb', line 15

def real_model?
  true
end

#to_real_modelObject



19
20
21
# File 'lib/model_extensions.rb', line 19

def to_real_model
  self
end