Class: Sail::Profile

Inherits:
ApplicationRecord show all
Defined in:
app/models/sail/profile.rb

Overview

Profile

The profile model contains the definitions for keeping a collection of settings’ values saved. It allows switching between different collections.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.create_or_update_self(name) ⇒ Object

create_or_update_self

Creates or updates a profile with name name saving the values of all settings in the database.



21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'app/models/sail/profile.rb', line 21

def self.create_or_update_self(name)
  profile = where(name: name).first_or_initialize
  new_record = profile.new_record?

  Sail::Setting.select(:id, :value).each do |setting|
    entry = setting.entries.where(profile: profile).first_or_initialize(profile: profile, setting: setting)
    entry.value = setting.value
    entry.save!
  end

  profile.save!
  handle_profile_activation(name)
  [profile, new_record]
end

.currentObject

current

Returns the currently activated profile



62
63
64
# File 'app/models/sail/profile.rb', line 62

def self.current
  find_by(active: true)
end

.switch(name) ⇒ Object

switch

Switch to a different setting profile. Set the value of every setting to what was previously saved.



40
41
42
43
44
45
46
# File 'app/models/sail/profile.rb', line 40

def self.switch(name)
  Sail::Entry.by_profile_name(name).each do |entry|
    Sail::Setting.set(entry.name, entry.value)
  end

  handle_profile_activation(name)
end

Instance Method Details

#dirty?Boolean

dirty?

A profile is considered dirty if it is active but setting values have been changed and do not match the entries definitions.

Returns:

  • (Boolean)


73
74
75
# File 'app/models/sail/profile.rb', line 73

def dirty?
  @dirty ||= entries.any?(&:dirty?)
end