Module: LatoSpaces::Associable

Extended by:
ActiveSupport::Concern
Defined in:
app/models/concerns/lato_spaces/associable.rb

Instance Method Summary collapse

Instance Method Details

#add_to_lato_spaces_group(group_id) ⇒ Object

Operations



33
34
35
36
37
38
39
40
41
# File 'app/models/concerns/lato_spaces/associable.rb', line 33

def add_to_lato_spaces_group(group_id)
  association = lato_spaces_associations.create(lato_spaces_group_id: group_id)
  unless association.valid?
    errors.add(:base, association.errors.full_messages.join(', '))
    return false
  end

  true
end

#lato_spaces_groupObject

Helpers



22
23
24
# File 'app/models/concerns/lato_spaces/associable.rb', line 22

def lato_spaces_group
  @lato_spaces_group ||= lato_spaces_groups.first
end

#lato_spaces_group_idObject



26
27
28
# File 'app/models/concerns/lato_spaces/associable.rb', line 26

def lato_spaces_group_id
  @lato_spaces_group_id ||= lato_spaces_group.try(:id)
end

#remove_from_lato_spaces_group(group_id) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
# File 'app/models/concerns/lato_spaces/associable.rb', line 43

def remove_from_lato_spaces_group(group_id)
  association = lato_spaces_associations.find_by(lato_spaces_group_id: group_id)
  return true unless association

  unless association.destroy
    errors.add(:base, association.errors.full_messages.join(', '))
    return false
  end

  true
end

#switch_lato_spaces_group(group_id) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
# File 'app/models/concerns/lato_spaces/associable.rb', line 55

def switch_lato_spaces_group(group_id)
  association = lato_spaces_associations.first
  return add_to_lato_spaces_group(group_id) unless association

  unless association.update(lato_spaces_group_id: group_id)
    errors.add(:base, association.errors.full_messages.join(', '))
    return false
  end

  true
end