module FlatMap::ModelMapper::Persistence

This module enhances and modifies original FlatMap::OpenMapper::Persistance functionality for ActiveRecord models as targets.

Public Instance Methods

apply(params) click to toggle source

Write a passed set of params. Then try to save the model if self passes validation. Saving is performed in a transaction.

@param [Hash] params @return [Boolean]

# File lib/flat_map/model_mapper/persistence.rb, line 76
def apply(params)
  write(params)
  res = if valid?
    ActiveRecord::Base.transaction do
      save
    end
  end
  !!res
end
id() click to toggle source

Delegate id to target, if possible.

@return [Fixnum, nil]

# File lib/flat_map/model_mapper/persistence.rb, line 104
def id
  target.id if target.respond_to?(:id)
end
model_name() click to toggle source

Return a ‘mapper’ string as a model_name. Used by Rails FormBuilder.

@return [String]

# File lib/flat_map/model_mapper/persistence.rb, line 61
def model_name
  'mapper'
end
persisted?() click to toggle source

Delegate persistence to target.

@return [Boolean]

# File lib/flat_map/model_mapper/persistence.rb, line 97
def persisted?
  target.respond_to?(:persisted?) ? target.persisted? : false
end
save_target() click to toggle source

Save target

@return [Boolean]

# File lib/flat_map/model_mapper/persistence.rb, line 89
def save_target
  return true if owned?
  target.respond_to?(:save) ? target.save(:validate => false) : true
end
to_key() click to toggle source

Delegate to the target’s to_key method. @return [String]

# File lib/flat_map/model_mapper/persistence.rb, line 67
def to_key
  target.to_key
end