Simplify Settings API

This commit is contained in:
Josep Jaume Rey Peroy
2015-12-10 11:26:42 +01:00
parent 89ece16fe8
commit 5de3d700e4
16 changed files with 64 additions and 46 deletions

View File

@@ -53,7 +53,7 @@ class Users::ConfirmationsController < Devise::ConfirmationsController
private
def set_official_position
resource.add_official_position! (Setting.value_for 'official_level_1_name'), 1
resource.add_official_position! (Setting['official_level_1_name']), 1
end
end

View File

@@ -7,7 +7,7 @@ module AdminHelper
def official_level_options
options = [["",0]]
(1..5).each do |i|
options << [[t("admin.officials.level_#{i}"), Setting.value_for("official_level_#{i}_name")].compact.join(': '), i]
options << [[t("admin.officials.level_#{i}"), Setting["official_level_#{i}_name"]].compact.join(': '), i]
end
options
end

View File

@@ -75,7 +75,7 @@ class Debate < ActiveRecord::Base
end
def editable?
total_votes <= Setting.value_for('max_votes_for_debate_edit').to_i
total_votes <= Setting['max_votes_for_debate_edit'].to_i
end
def editable_by?(user)
@@ -93,8 +93,8 @@ class Debate < ActiveRecord::Base
return false unless user
total_votes <= 100 ||
!user.unverified? ||
Setting.value_for('max_ratio_anon_votes_on_debates').to_i == 100 ||
anonymous_votes_ratio < Setting.value_for('max_ratio_anon_votes_on_debates').to_i ||
Setting['max_ratio_anon_votes_on_debates'].to_i == 100 ||
anonymous_votes_ratio < Setting['max_ratio_anon_votes_on_debates'].to_i ||
user.voted_for?(self)
end

View File

@@ -80,7 +80,7 @@ class Proposal < ActiveRecord::Base
end
def editable?
total_votes <= Setting.value_for("max_votes_for_proposal_edit").to_i
total_votes <= Setting["max_votes_for_proposal_edit"].to_i
end
def editable_by?(user)
@@ -98,7 +98,7 @@ class Proposal < ActiveRecord::Base
end
def code
"#{Setting.value_for("proposal_code_prefix")}-#{created_at.strftime('%Y-%m')}-#{id}"
"#{Setting["proposal_code_prefix"]}-#{created_at.strftime('%Y-%m')}-#{id}"
end
def after_commented
@@ -129,7 +129,7 @@ class Proposal < ActiveRecord::Base
end
def self.votes_needed_for_success
Setting.value_for('votes_for_proposal_success').to_i
Setting['votes_for_proposal_success'].to_i
end
protected

View File

@@ -3,7 +3,16 @@ class Setting < ActiveRecord::Base
default_scope { order(id: :asc) }
def self.value_for(key)
where(key: key).pluck(:value).first
class << self
def [](key)
where(key: key).pluck(:value).first
end
def []=(key, value)
setting = where(key: key).first || new(key: key)
setting.value = value
setting.save!
value
end
end
end
end

View File

@@ -202,7 +202,7 @@ class User < ActiveRecord::Base
end
def has_official_email?
domain = Setting.value_for 'email_domain_for_officials'
domain = Setting['email_domain_for_officials']
!email.blank? && ( (email.end_with? "@#{domain}") || (email.end_with? ".#{domain}") )
end