Module: Pivotable::Model::ClassMethods

Defined in:
lib/pivotable/model.rb

Instance Method Summary collapse

Instance Method Details

#_pivotableObject


47
48
49
# File 'lib/pivotable/model.rb', line 47

def _pivotable
  @_pivotable ||= {}
end

#pivot(*args) ⇒ Object

Delegator to Relation#pivot


43
44
45
# File 'lib/pivotable/model.rb', line 43

def pivot(*args)
  scoped.pivot(*args)
end

#pivotable(*tokens, &block) ⇒ Object

Pivotable definition for a model. Example:

class Stat < ActiveRecord::Base

  # Add your rotations
  pivotable :overall do
    sum :visits
  end

  # Rotations can in herit parent definitions
  pivotable :daily => :overall do
    by :day
  end

  # Add "namespaced" rotations, e.g. for admins
  pivotable "admin:daily" => :daily do
    maximum :bounce_rate
  end

  # Can be expressed as
  pivotable :admin, :extended => [:admin, :daily] do
    sum :bot_impressions
  end

end

Raises:

  • (ArgmentError)

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

def pivotable(*tokens, &block)
  name, parent = tokens.extract_options!.to_a.first
  name, parent = Pivotable.name(tokens, name), Pivotable.name(parent)
  raise ArgmentError, "A name must be provided" unless name

  _pivotable[name] = Pivotable::Rotation.new(self, name, parent, &block) if block
  _pivotable[name]
end