Module: Sequel::Plugins::ActiveModel::InstanceMethods

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

Constant Summary collapse

DEFAULT_TO_PARAM_JOINER =

The default string to join composite primary keys with in to_param.

'-'.freeze

Instance Method Summary collapse

Instance Method Details

#after_destroyObject

Record that an object was destroyed, for later use by destroyed?



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

def after_destroy
  super
  @destroyed = true
end

#before_createObject

Mark current instance as destroyed if the transaction in which this instance is created is rolled back.



51
52
53
54
# File 'lib/sequel/plugins/active_model.rb', line 51

def before_create
  db.after_rollback{@destroyed = true}
  super
end

#model_nameObject

Return ::ActiveModel::Name instance for the class.



57
58
59
# File 'lib/sequel/plugins/active_model.rb', line 57

def model_name
  model.model_name
end

#persisted?Boolean

False if the object is new? or has been destroyed, true otherwise.

Returns:

  • (Boolean)


62
63
64
# File 'lib/sequel/plugins/active_model.rb', line 62

def persisted?
  !new? && @destroyed != true
end

#to_keyObject

An array of primary key values, or nil if the object is not persisted.



67
68
69
70
71
72
73
# File 'lib/sequel/plugins/active_model.rb', line 67

def to_key
  if primary_key.is_a?(Symbol)
    [pk] if pk
  else
    pk if pk.all?
  end
end

#to_modelObject

With the ActiveModel plugin, Sequel model objects are already compliant, so this returns self.



77
78
79
# File 'lib/sequel/plugins/active_model.rb', line 77

def to_model
  self
end

#to_paramObject

An string representing the object’s primary key. For composite primary keys, joins them with to_param_joiner.



83
84
85
86
87
# File 'lib/sequel/plugins/active_model.rb', line 83

def to_param
  if persisted? and k = to_key
    k.join(to_param_joiner)
  end
end

#to_partial_pathObject

Returns a string identifying the path associated with the object.



90
91
92
# File 'lib/sequel/plugins/active_model.rb', line 90

def to_partial_path
  model._to_partial_path
end