Sha256: c5c7301dfb2b385e74d5e1859d6cdfb3742e996f1571cf727f8f94e1acd10a5a
Contents?: true
Size: 1.93 KB
Versions: 2
Compression:
Stored size: 1.93 KB
Contents
# frozen_string_literal: true module Sail # Profile # # The profile model contains the # definitions for keeping a collection # of settings' values saved. It allows # switching between different collections. class Profile < ApplicationRecord has_many :entries, dependent: :destroy has_many :settings, through: :entries validates :name, presence: true, uniqueness: { case_sensitive: false } # create_or_update_self # # Creates or updates a profile with name # +name+ saving the values of all settings # in the database. 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| Sail::Entry.where( setting: setting, profile: profile ).first_or_create!( setting: setting, value: setting.value, profile: profile ) end handle_profile_activation(name) [profile, new_record] end # switch # # Switch to a different setting profile. Set the value # of every setting to what was previously saved. 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 # handle_profile_activation # # Set other profiles to active false and # set the selected profile to active true. def self.handle_profile_activation(name) select(:id).where(active: true).update_all(active: false) select(:id, :name).where(name: name).update_all(active: true) end private_class_method :handle_profile_activation # dirty? # # A profile is considered dirty if it is active # but setting values have been changed and do # not match the entries definitions. def dirty? @dirty ||= entries.any?(&:dirty?) end end end
Version data entries
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
sail-3.0.1 | app/models/sail/profile.rb |
sail-3.0.0 | app/models/sail/profile.rb |