Module: DoorMat::AttrSymmetricStore::ClassMethods

Defined in:
lib/door_mat/attr_symmetric_store.rb

Instance Method Summary collapse

Instance Method Details

#attr_symmetric_store(*args, **options) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/door_mat/attr_symmetric_store.rb', line 61

def attr_symmetric_store(*args, **options)
  return unless self.table_exists?

  actor_column = options.fetch(:actor_column, :actor).to_s
  unless self.attribute_names.include? "#{actor_column}_id"
    raise ActiveRecord::ActiveRecordError, "attr_symmetric_store records must belong to a DoorMat::Actor but could not find the actor column. Pass the actor_column: :actor_column_name option to specify it."
  end

  args.each do |arg|
    column_type = (self.columns_hash[arg.to_s].cast_type.respond_to?(:type) && self.columns_hash[arg.to_s].cast_type.type) || self.columns_hash[arg.to_s].cast_type.to_s
    if [:text, :string].include? column_type
      after_find DoorMat::AttrSymmetricStore::AttrSymmetricStoreWrapper.new(arg.to_s, actor_column)
      around_save DoorMat::AttrSymmetricStore::AttrSymmetricStoreWrapper.new(arg.to_s, actor_column)
    else
      raise ActiveRecord::ActiveRecordError, "attr_symmetric_store only support text and string column types."
    end
  end
end