Skip to content

Commit 15375e2

Browse files
committed
Gemified
1 parent 7a50aff commit 15375e2

File tree

11 files changed

+215
-167
lines changed

11 files changed

+215
-167
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
*.gem
2+
.bundle
3+
Gemfile.lock
4+
pkg/*

Gemfile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
source "http://rubygems.org"
2+
3+
# Specify your gem's dependencies in rails-settings.gemspec
4+
gemspec

README.md

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
# Settings Plugin for Rails
1+
# Settings Gem/Plugin for Rails
22

33
[![Build Status](https://secure.travis-ci.org/ledermann/rails-settings.png)](http://travis-ci.org/ledermann/rails-settings)
44

5-
Settings is a plugin that makes managing a table of key/value pairs easy. Think of it like a Hash stored in you database, that uses simple ActiveRecord like methods for manipulation. Keep track of any setting that you don't want to hard code into your rails app. You can store any kind of object: Strings, numbers, arrays, or any object which can be noted as YAML.
5+
Settings is a gem/plugin that makes managing a table of key/value pairs easy. Think of it like a Hash stored in you database, that uses simple ActiveRecord like methods for manipulation. Keep track of any setting that you don't want to hard code into your rails app. You can store any kind of object: Strings, numbers, arrays, or any object which can be noted as YAML.
66

77

88
## Requirements
@@ -12,7 +12,16 @@ ActiveRecord 2.3.x, 3.0.x or 3.1.x
1212
Tested with Ruby 1.8.7, 1.9.2, 1.9.3 and RBX2.0
1313

1414

15-
## Setup
15+
## Installation
16+
17+
Include the gem in your Gemfile
18+
19+
gem 'ledermann-rails-settings', :require => 'rails-settings'
20+
21+
or install as a plugin:
22+
23+
./script/plugin install git://github.com/ledermann/rails-settings.git
24+
1625

1726
You have to create the table used by the Settings model by using this migration:
1827

@@ -34,7 +43,7 @@ You have to create the table used by the Settings model by using this migration:
3443
end
3544
end
3645

37-
Now put update your database with:
46+
Now update your database with:
3847

3948
rake db:migrate
4049

Rakefile

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,6 @@ Rake::TestTask.new do |t|
66
t.verbose = true
77
end
88

9-
task :default => :test
9+
task :default => :test
10+
11+
require 'bundler/gem_tasks'

init.rb

Lines changed: 1 addition & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1 @@
1-
require 'settings'
2-
3-
ActiveRecord::Base.class_eval do
4-
def self.has_settings
5-
class_eval do
6-
def settings
7-
ScopedSettings.for_target(self)
8-
end
9-
10-
after_destroy { |user| user.settings.target_scoped.delete_all }
11-
12-
scope_method = ActiveRecord::VERSION::MAJOR < 3 ? :named_scope : :scope
13-
14-
send scope_method, :with_settings, :joins => "JOIN settings ON (settings.target_id = #{self.table_name}.#{self.primary_key} AND
15-
settings.target_type = '#{self.base_class.name}')",
16-
:select => "DISTINCT #{self.table_name}.*"
17-
18-
send scope_method, :with_settings_for, lambda { |var| { :joins => "JOIN settings ON (settings.target_id = #{self.table_name}.#{self.primary_key} AND
19-
settings.target_type = '#{self.base_class.name}') AND
20-
settings.var = '#{var}'" } }
21-
22-
send scope_method, :without_settings, :joins => "LEFT JOIN settings ON (settings.target_id = #{self.table_name}.#{self.primary_key} AND
23-
settings.target_type = '#{self.base_class.name}')",
24-
:conditions => 'settings.id IS NULL'
25-
26-
send scope_method, :without_settings_for, lambda { |var| { :joins => "LEFT JOIN settings ON (settings.target_id = #{self.table_name}.#{self.primary_key} AND
27-
settings.target_type = '#{self.base_class.name}') AND
28-
settings.var = '#{var}'",
29-
:conditions => 'settings.id IS NULL' } }
30-
end
31-
end
32-
end
1+
require 'rails-settings'

lib/rails-settings.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
require 'rails-settings/version'
2+
require 'rails-settings/active_record'
3+
require 'rails-settings/settings'
4+
require 'rails-settings/scoped_settings'

lib/rails-settings/active_record.rb

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
ActiveRecord::Base.class_eval do
2+
def self.has_settings
3+
class_eval do
4+
def settings
5+
ScopedSettings.for_target(self)
6+
end
7+
8+
after_destroy { |user| user.settings.target_scoped.delete_all }
9+
10+
scope_method = ActiveRecord::VERSION::MAJOR < 3 ? :named_scope : :scope
11+
12+
send scope_method, :with_settings, :joins => "JOIN settings ON (settings.target_id = #{self.table_name}.#{self.primary_key} AND
13+
settings.target_type = '#{self.base_class.name}')",
14+
:select => "DISTINCT #{self.table_name}.*"
15+
16+
send scope_method, :with_settings_for, lambda { |var| { :joins => "JOIN settings ON (settings.target_id = #{self.table_name}.#{self.primary_key} AND
17+
settings.target_type = '#{self.base_class.name}') AND
18+
settings.var = '#{var}'" } }
19+
20+
send scope_method, :without_settings, :joins => "LEFT JOIN settings ON (settings.target_id = #{self.table_name}.#{self.primary_key} AND
21+
settings.target_type = '#{self.base_class.name}')",
22+
:conditions => 'settings.id IS NULL'
23+
24+
send scope_method, :without_settings_for, lambda { |var| { :joins => "LEFT JOIN settings ON (settings.target_id = #{self.table_name}.#{self.primary_key} AND
25+
settings.target_type = '#{self.base_class.name}') AND
26+
settings.var = '#{var}'",
27+
:conditions => 'settings.id IS NULL' } }
28+
end
29+
end
30+
end

lib/rails-settings/scoped_settings.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class ScopedSettings < Settings
2+
def self.for_target(target)
3+
@target = target
4+
self
5+
end
6+
7+
def self.target_id
8+
@target.id
9+
end
10+
11+
def self.target_type
12+
@target.class.base_class.to_s
13+
end
14+
end
Lines changed: 115 additions & 130 deletions
Original file line numberDiff line numberDiff line change
@@ -1,131 +1,116 @@
1-
class Settings < ActiveRecord::Base
2-
class SettingNotFound < RuntimeError; end
3-
4-
cattr_accessor :defaults
5-
@@defaults = {}.with_indifferent_access
6-
7-
# Support old plugin
8-
if defined?(SettingsDefaults::DEFAULTS)
9-
@@defaults = SettingsDefaults::DEFAULTS.with_indifferent_access
10-
end
11-
12-
#get or set a variable with the variable as the called method
13-
def self.method_missing(method, *args)
14-
if self.respond_to?(method)
15-
super
16-
else
17-
method_name = method.to_s
18-
19-
#set a value for a variable
20-
if method_name =~ /=$/
21-
var_name = method_name.gsub('=', '')
22-
value = args.first
23-
self[var_name] = value
24-
25-
#retrieve a value
26-
else
27-
self[method_name]
28-
29-
end
30-
end
31-
end
32-
33-
#destroy the specified settings record
34-
def self.destroy(var_name)
35-
var_name = var_name.to_s
36-
begin
37-
target(var_name).destroy
38-
true
39-
rescue NoMethodError
40-
raise SettingNotFound, "Setting variable \"#{var_name}\" not found"
41-
end
42-
end
43-
44-
#retrieve all settings as a hash (optionally starting with a given namespace)
45-
def self.all(starting_with=nil)
46-
options = starting_with ? { :conditions => "var LIKE '#{starting_with}%'"} : {}
47-
vars = target_scoped.find(:all, {:select => 'var, value'}.merge(options))
48-
49-
result = {}
50-
vars.each do |record|
51-
result[record.var] = record.value
52-
end
53-
result.with_indifferent_access
54-
end
55-
56-
#get a setting value by [] notation
57-
def self.[](var_name)
58-
if var = target(var_name)
59-
var.value
60-
else
61-
@@defaults[var_name.to_s]
62-
end
63-
end
64-
65-
#set a setting value by [] notation
66-
def self.[]=(var_name, value)
67-
record = target_scoped.find_or_initialize_by_var(var_name.to_s)
68-
record.value = value
69-
record.save!
70-
71-
value
72-
end
73-
74-
def self.merge!(var_name, hash_value)
75-
raise ArgumentError unless hash_value.is_a?(Hash)
76-
77-
old_value = self[var_name] || {}
78-
raise TypeError, "Existing value is not a hash, can't merge!" unless old_value.is_a?(Hash)
79-
80-
new_value = old_value.merge(hash_value)
81-
self[var_name] = new_value if new_value != old_value
82-
83-
new_value
84-
end
85-
86-
def self.target(var_name)
87-
target_scoped.find_by_var(var_name.to_s)
88-
end
89-
90-
#get the value field, YAML decoded
91-
def value
92-
YAML::load(self[:value])
93-
end
94-
95-
#set the value field, YAML encoded
96-
def value=(new_value)
97-
self[:value] = new_value.to_yaml
98-
end
99-
100-
def self.target_scoped
101-
Settings.scoped_by_target_type_and_target_id(target_type, target_id)
102-
end
103-
104-
#Deprecated!
105-
def self.reload # :nodoc:
106-
self
107-
end
108-
109-
def self.target_id
110-
nil
111-
end
112-
113-
def self.target_type
114-
nil
115-
end
116-
end
117-
118-
class ScopedSettings < Settings
119-
def self.for_target(target)
120-
@target = target
121-
self
122-
end
123-
124-
def self.target_id
125-
@target.id
126-
end
127-
128-
def self.target_type
129-
@target.class.base_class.to_s
130-
end
1+
class Settings < ActiveRecord::Base
2+
class SettingNotFound < RuntimeError; end
3+
4+
cattr_accessor :defaults
5+
@@defaults = {}.with_indifferent_access
6+
7+
# Support old plugin
8+
if defined?(SettingsDefaults::DEFAULTS)
9+
@@defaults = SettingsDefaults::DEFAULTS.with_indifferent_access
10+
end
11+
12+
#get or set a variable with the variable as the called method
13+
def self.method_missing(method, *args)
14+
if self.respond_to?(method)
15+
super
16+
else
17+
method_name = method.to_s
18+
19+
#set a value for a variable
20+
if method_name =~ /=$/
21+
var_name = method_name.gsub('=', '')
22+
value = args.first
23+
self[var_name] = value
24+
25+
#retrieve a value
26+
else
27+
self[method_name]
28+
29+
end
30+
end
31+
end
32+
33+
#destroy the specified settings record
34+
def self.destroy(var_name)
35+
var_name = var_name.to_s
36+
begin
37+
target(var_name).destroy
38+
true
39+
rescue NoMethodError
40+
raise SettingNotFound, "Setting variable \"#{var_name}\" not found"
41+
end
42+
end
43+
44+
#retrieve all settings as a hash (optionally starting with a given namespace)
45+
def self.all(starting_with=nil)
46+
options = starting_with ? { :conditions => "var LIKE '#{starting_with}%'"} : {}
47+
vars = target_scoped.find(:all, {:select => 'var, value'}.merge(options))
48+
49+
result = {}
50+
vars.each do |record|
51+
result[record.var] = record.value
52+
end
53+
result.with_indifferent_access
54+
end
55+
56+
#get a setting value by [] notation
57+
def self.[](var_name)
58+
if var = target(var_name)
59+
var.value
60+
else
61+
@@defaults[var_name.to_s]
62+
end
63+
end
64+
65+
#set a setting value by [] notation
66+
def self.[]=(var_name, value)
67+
record = target_scoped.find_or_initialize_by_var(var_name.to_s)
68+
record.value = value
69+
record.save!
70+
71+
value
72+
end
73+
74+
def self.merge!(var_name, hash_value)
75+
raise ArgumentError unless hash_value.is_a?(Hash)
76+
77+
old_value = self[var_name] || {}
78+
raise TypeError, "Existing value is not a hash, can't merge!" unless old_value.is_a?(Hash)
79+
80+
new_value = old_value.merge(hash_value)
81+
self[var_name] = new_value if new_value != old_value
82+
83+
new_value
84+
end
85+
86+
def self.target(var_name)
87+
target_scoped.find_by_var(var_name.to_s)
88+
end
89+
90+
#get the value field, YAML decoded
91+
def value
92+
YAML::load(self[:value])
93+
end
94+
95+
#set the value field, YAML encoded
96+
def value=(new_value)
97+
self[:value] = new_value.to_yaml
98+
end
99+
100+
def self.target_scoped
101+
Settings.scoped_by_target_type_and_target_id(target_type, target_id)
102+
end
103+
104+
#Deprecated!
105+
def self.reload # :nodoc:
106+
self
107+
end
108+
109+
def self.target_id
110+
nil
111+
end
112+
113+
def self.target_type
114+
nil
115+
end
131116
end

lib/rails-settings/version.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module RailsSettings
2+
VERSION = '1.0.0'
3+
end

0 commit comments

Comments
 (0)