Module: FlatMap::ModelMapper::Persistence

Extended by:
ActiveSupport::Concern
Included in:
FlatMap::ModelMapper
Defined in:
lib/flat_map/model_mapper/persistence.rb

Overview

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

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#apply(params) ⇒ Boolean

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

Parameters:

  • params (Hash)

Returns:

  • (Boolean)


76
77
78
79
80
81
82
83
84
# 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

#idFixnum?

Delegate #id to target, if possible.

Returns:

  • (Fixnum, nil)


104
105
106
# File 'lib/flat_map/model_mapper/persistence.rb', line 104

def id
  target.id if target.respond_to?(:id)
end

#model_nameString

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

Returns:

  • (String)


61
62
63
# File 'lib/flat_map/model_mapper/persistence.rb', line 61

def model_name
  'mapper'
end

#persisted?Boolean

Delegate persistence to target.

Returns:

  • (Boolean)


97
98
99
# File 'lib/flat_map/model_mapper/persistence.rb', line 97

def persisted?
  target.respond_to?(:persisted?) ? target.persisted? : false
end

#save_targetBoolean

Save target

Returns:

  • (Boolean)


89
90
91
92
# 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_keyString

Delegate to the target’s #to_key method.

Returns:

  • (String)


67
68
69
# File 'lib/flat_map/model_mapper/persistence.rb', line 67

def to_key
  target.to_key
end