Module: Sequel::Plugins::Touch::ClassMethods

Defined in:
lib/sequel/lib/sequel/plugins/touch.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#touch_columnObject

The column to modify when touching a model instance, as a symbol. Also used as the default column when touching associations, if the associations don't specify a column.


39
40
41
# File 'lib/sequel/lib/sequel/plugins/touch.rb', line 39

def touch_column
  @touch_column
end

#touched_associationsObject (readonly)

A hash specifying the associations to touch when instances are updated or destroyed. Keys are association dataset method name symbols and values are column name symbols.


44
45
46
# File 'lib/sequel/lib/sequel/plugins/touch.rb', line 44

def touched_associations
  @touched_associations
end

Instance Method Details

#inherited(subclass) ⇒ Object

Set the touch_column for the subclass to be the same as the current class. Also, create a copy of the touched_associations in the subclass.


48
49
50
51
52
# File 'lib/sequel/lib/sequel/plugins/touch.rb', line 48

def inherited(subclass)
  super
  subclass.touch_column = touch_column
  subclass.instance_variable_set(:@touched_associations, touched_associations.dup)
end

#touch_associations(*associations) ⇒ Object

Add additional associations to be touched. See the :association option of the Sequel::Plugin::Touch.configure method for the format of the associations arguments.


57
58
59
60
61
62
63
64
65
# File 'lib/sequel/lib/sequel/plugins/touch.rb', line 57

def touch_associations(*associations)
  associations.flatten.each do |a|
    a = {a=>touch_column} if a.is_a?(Symbol)
    a.each do |k,v|
      raise(Error, "invalid association: #{k}") unless r = association_reflection(k)
      touched_associations[r.dataset_method] = v
    end
  end
end