Module: ObjectAttorney::Record

Defined in:
lib/object_attorney/record.rb

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#call_save_or_destroy(object, save_method) ⇒ Object



38
39
40
41
42
43
44
45
# File 'lib/object_attorney/record.rb', line 38

def call_save_or_destroy(object, save_method)
  if object == self
    represented_object.present? ? represented_object.send(save_method) : true
  else
    save_method = :destroy if Helpers.marked_for_destruction?(object)
    object.send(save_method)
  end
end

#clear_nested_imposed_errorsObject



47
48
49
50
51
52
53
# File 'lib/object_attorney/record.rb', line 47

def clear_nested_imposed_errors
  nested_objects.each do |reflection, nested_object|
    nested_object.clear_imposed_errors

    nested_object.clear_nested_imposed_errors if nested_object.respond_to?(:clear_nested_imposed_errors)
  end
end

#destroyObject



33
34
35
36
# File 'lib/object_attorney/record.rb', line 33

def destroy
  return true if represented_object.blank?
  represented_object.destroy
end

#idObject



5
6
7
# File 'lib/object_attorney/record.rb', line 5

def id
  represented_object.try(:id)
end

#new_record?Boolean

Returns:

  • (Boolean)


9
10
11
# File 'lib/object_attorney/record.rb', line 9

def new_record?
  Helpers.try_or_return(represented_object, :new_record?, true)
end

#persisted?Boolean

Returns:

  • (Boolean)


13
14
15
# File 'lib/object_attorney/record.rb', line 13

def persisted?
  Helpers.try_or_return(represented_object, :persisted?, false)
end

#populate_nested_imposed_errorsObject



55
56
57
58
59
60
61
62
63
# File 'lib/object_attorney/record.rb', line 55

def populate_nested_imposed_errors
  nested_objects.each do |reflection, nested_object|
    next if Helpers.marked_for_destruction?(nested_object)
    
    nested_object.populate_imposed_errors
    
    nested_object.populate_nested_imposed_errors if nested_object.respond_to?(:populate_nested_imposed_errors)
  end
end

#saveObject



17
18
19
# File 'lib/object_attorney/record.rb', line 17

def save
  save_or_! { submit }
end

#save!(save_method = :save!) ⇒ Object



21
22
23
# File 'lib/object_attorney/record.rb', line 21

def save!(save_method = :save!)
  save_or_! { submit! }
end

#submitObject



25
26
27
# File 'lib/object_attorney/record.rb', line 25

def submit
  submit_or_!(:save)
end

#submit!Object



29
30
31
# File 'lib/object_attorney/record.rb', line 29

def submit!
  submit_or_!(:save!)
end