Module: FlatMap::Mapper::Targeting

Extended by:
ActiveSupport::Concern
Included in:
FlatMap::Mapper
Defined in:
lib/flat_map/mapper/targeting.rb

Overview

This module provides some integration between mapper and its target, which is usually an ActiveRecord model, as well as some integration between mapper and Rails forms.

Defined Under Namespace

Modules: ClassMethods Classes: NoTargetError

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.



98
99
100
101
102
103
104
105
106
# File 'lib/flat_map/mapper/targeting.rb', line 98

def apply(params)
  write(params)
  res = if valid?
    ActiveRecord::Base.transaction do
      save
    end
  end
  !!res
end

#idFixnum?

Delegate #id to target, if possible.



126
127
128
# File 'lib/flat_map/mapper/targeting.rb', line 126

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

#model_nameString

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



83
84
85
# File 'lib/flat_map/mapper/targeting.rb', line 83

def model_name
  'mapper'
end

#persisted?Boolean

Delegate persistence to target.



119
120
121
# File 'lib/flat_map/mapper/targeting.rb', line 119

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

#save_targetBoolean

Save target



111
112
113
114
# File 'lib/flat_map/mapper/targeting.rb', line 111

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.



89
90
91
# File 'lib/flat_map/mapper/targeting.rb', line 89

def to_key
  target.to_key
end