Class: Effective::Merge

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Model
Defined in:
app/models/effective/merge.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#sourceObject

Returns the value of attribute source.



5
6
7
# File 'app/models/effective/merge.rb', line 5

def source
  @source
end

#source_idObject

Returns the value of attribute source_id.



5
6
7
# File 'app/models/effective/merge.rb', line 5

def source_id
  @source_id
end

#targetObject

Returns the value of attribute target.



5
6
7
# File 'app/models/effective/merge.rb', line 5

def target
  @target
end

#target_idObject

Returns the value of attribute target_id.



5
6
7
# File 'app/models/effective/merge.rb', line 5

def target_id
  @target_id
end

#typeObject

Returns the value of attribute type.



5
6
7
# File 'app/models/effective/merge.rb', line 5

def type
  @type
end

Instance Method Details

#collectionObject



36
37
38
# File 'app/models/effective/merge.rb', line 36

def collection
  @collection ||= (klass.respond_to?(:effective_mergery_collection) ? klass.effective_mergery_collection : klass.all)
end

#form_collectionObject



40
41
42
# File 'app/models/effective/merge.rb', line 40

def form_collection
  @form_collection ||= (klass.respond_to?(:effective_mergery_form_collection) ? klass.effective_mergery_form_collection : collection)
end

#klassObject



44
45
46
# File 'app/models/effective/merge.rb', line 44

def klass
  @klass ||= type.safe_constantize
end

#save(validate: true) ⇒ Object



26
27
28
29
# File 'app/models/effective/merge.rb', line 26

def save(validate: true)
  return false unless valid?
  (merge!(validate: validate) rescue false)
end

#save!(validate: true) ⇒ Object



31
32
33
34
# File 'app/models/effective/merge.rb', line 31

def save!(validate: true)
  raise 'is invalid' unless valid?
  merge!(validate: validate)
end

#to_sObject



21
22
23
24
# File 'app/models/effective/merge.rb', line 21

def to_s
  return 'New Merge' unless type
  type.downcase
end

#validate_klass!Object

This is called on Admin::Merges#new



49
50
51
52
53
54
55
# File 'app/models/effective/merge.rb', line 49

def validate_klass!
  raise "type can't be blank" unless type.present?
  raise 'type must be a mergable type' unless EffectiveMergery.mergables.include?(type)
  raise "invalid ActiveRecord klass" unless klass
  raise "invalid ActiveRecord collection" unless collection.kind_of?(ActiveRecord::Relation)
  true
end