Module: Movescount::Concern::Move

Defined in:
lib/movescount/concern/move.rb

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



4
5
6
# File 'lib/movescount/concern/move.rb', line 4

def self.included(base)
  base.extend ClassMethods
end

Instance Method Details

#movescount_moveObject

Get the movescount move



9
10
11
12
# File 'lib/movescount/concern/move.rb', line 9

def movescount_move
  return unless self[movescount_options[:move_id_column]] && movescount_member
  @movescount_move ||= Movescount::Move.new(movescount_member, 'MoveID' => self[movescount_options[:move_id_column]])
end

#movescount_saveObject

Save the points to the database



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/movescount/concern/move.rb', line 15

def movescount_save
  self.class.transaction do
    # First delete all point
    public_send(movescount_options[:points_relation]).public_send(movescount_options[:points_clear_method])
    # Then for each point create a new point
    movescount_move.points.each do |point|
      attributes = {}
      movescount_options[:point_attributes].each do |attribute, target|
        attributes[target] = point.public_send(attribute) if target
      end
      unless attributes.empty?
        if persisted?
          public_send(movescount_options[:points_relation]).create attributes
        else
          public_send(movescount_options[:points_relation]).build attributes
        end
      end
    end
  end
end