Module: ActiveShotgun::Model::Write

Defined in:
lib/active_shotgun/model/write.rb

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#mass_assign(assigned_attributes) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/active_shotgun/model/write.rb', line 25

def mass_assign(assigned_attributes)
  sym_assigned_attributes = assigned_attributes.transform_keys(&:to_sym)
  sym_assigned_attributes.
    slice(*self.class.shotgun_writable_fetched_attributes).
    each do |k, v|
      public_send("#{k}=", v)
    end

  sym_assigned_attributes.
    slice(*self.class::BELONG_ASSOC.map{ |assoc| "#{assoc}_id".to_sym }).
    each do |k, v|
      public_send("#{k}=", v, sym_assigned_attributes["#{k.to_s.gsub(/_id$/, '')}_type".to_sym])
    end

  sym_assigned_attributes.
    slice(*self.class::BELONG_ASSOC.map(&:to_sym)).
    each do |k, v|
      public_send("#{k}=", v)
    end
end

#persisted?Boolean

Returns:

  • (Boolean)


6
7
8
# File 'lib/active_shotgun/model/write.rb', line 6

def persisted?
  !!id
end

#saveObject Also known as: save!



10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/active_shotgun/model/write.rb', line 10

def save
  return false unless changed?

  sg_result =
    if persisted?
      shotgun_client.update(id, changes_with_relations)
    else
      shotgun_client.create(changes_with_relations)
    end
  override_attributes!(sg_result)
  changes_applied
  true
end

#update(updated_attributes) ⇒ Object



46
47
48
49
# File 'lib/active_shotgun/model/write.rb', line 46

def update(updated_attributes)
  mass_assign(updated_attributes)
  save
end

#update!(updated_attributes) ⇒ Object



51
52
53
54
# File 'lib/active_shotgun/model/write.rb', line 51

def update!(updated_attributes)
  mass_assign(updated_attributes)
  save!
end