has_settings 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
data/.gitignore ADDED
@@ -0,0 +1,22 @@
1
+ ## MAC OS
2
+ .DS_Store
3
+
4
+ ## TEXTMATE
5
+ *.tmproj
6
+ tmtags
7
+
8
+ ## EMACS
9
+ *~
10
+ \#*
11
+ .\#*
12
+
13
+ ## VIM
14
+ *.swp
15
+
16
+ ## PROJECT::GENERAL
17
+ coverage
18
+ rdoc
19
+ pkg
20
+
21
+ ## PROJECT::SPECIFIC
22
+ test/log/*
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Morten Primdahl
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,17 @@
1
+ = has_settings
2
+
3
+ Description goes here.
4
+
5
+ == Note on Patches/Pull Requests
6
+
7
+ * Fork the project.
8
+ * Make your feature addition or bug fix.
9
+ * Add tests for it. This is important so I don't break it in a
10
+ future version unintentionally.
11
+ * Commit, do not mess with rakefile, version, or history.
12
+ (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
13
+ * Send me a pull request. Bonus points for topic branches.
14
+
15
+ == Copyright
16
+
17
+ Copyright (c) 2010 Morten Primdahl. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,53 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "has_settings"
8
+ gem.summary = %Q{Per row settings for ActiveRecord}
9
+ gem.description = %Q{This gem is an ActiveRecord extension which provides a convenient interface for managing per row settings}
10
+ gem.email = "morten@zendesk.com"
11
+ gem.homepage = "http://github.com/morten/has_settings"
12
+ gem.authors = ["Morten Primdahl"]
13
+ gem.add_development_dependency "thoughtbot-shoulda", ">= 0"
14
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
15
+ end
16
+ Jeweler::GemcutterTasks.new
17
+ rescue LoadError
18
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
19
+ end
20
+
21
+ require 'rake/testtask'
22
+ Rake::TestTask.new(:test) do |test|
23
+ test.libs << 'lib' << 'test'
24
+ test.pattern = 'test/**/test_*.rb'
25
+ test.verbose = true
26
+ end
27
+
28
+ begin
29
+ require 'rcov/rcovtask'
30
+ Rcov::RcovTask.new do |test|
31
+ test.libs << 'test'
32
+ test.pattern = 'test/**/test_*.rb'
33
+ test.verbose = true
34
+ end
35
+ rescue LoadError
36
+ task :rcov do
37
+ abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
38
+ end
39
+ end
40
+
41
+ task :test => :check_dependencies
42
+
43
+ task :default => :test
44
+
45
+ require 'rake/rdoctask'
46
+ Rake::RDocTask.new do |rdoc|
47
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
48
+
49
+ rdoc.rdoc_dir = 'rdoc'
50
+ rdoc.title = "has_settings #{version}"
51
+ rdoc.rdoc_files.include('README*')
52
+ rdoc.rdoc_files.include('lib/**/*.rb')
53
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.1
@@ -0,0 +1,62 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{has_settings}
8
+ s.version = "0.0.1"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Morten Primdahl"]
12
+ s.date = %q{2010-11-04}
13
+ s.description = %q{This gem is an ActiveRecord extension which provides a convenient interface for managing per row settings}
14
+ s.email = %q{morten@zendesk.com}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE",
17
+ "README.rdoc"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ ".gitignore",
22
+ "LICENSE",
23
+ "README.rdoc",
24
+ "Rakefile",
25
+ "VERSION",
26
+ "has_settings.gemspec",
27
+ "lib/has_settings.rb",
28
+ "lib/has_settings/active_record_extension.rb",
29
+ "lib/has_settings/has_settings_helper.rb",
30
+ "lib/has_settings/setting.rb",
31
+ "test/database.yml",
32
+ "test/fixtures/account_settings.yml",
33
+ "test/fixtures/accounts.yml",
34
+ "test/helper.rb",
35
+ "test/schema.rb",
36
+ "test/test_has_settings.rb"
37
+ ]
38
+ s.homepage = %q{http://github.com/morten/has_settings}
39
+ s.rdoc_options = ["--charset=UTF-8"]
40
+ s.require_paths = ["lib"]
41
+ s.rubygems_version = %q{1.3.7}
42
+ s.summary = %q{Per row settings for ActiveRecord}
43
+ s.test_files = [
44
+ "test/helper.rb",
45
+ "test/schema.rb",
46
+ "test/test_has_settings.rb"
47
+ ]
48
+
49
+ if s.respond_to? :specification_version then
50
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
51
+ s.specification_version = 3
52
+
53
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
54
+ s.add_development_dependency(%q<thoughtbot-shoulda>, [">= 0"])
55
+ else
56
+ s.add_dependency(%q<thoughtbot-shoulda>, [">= 0"])
57
+ end
58
+ else
59
+ s.add_dependency(%q<thoughtbot-shoulda>, [">= 0"])
60
+ end
61
+ end
62
+
@@ -0,0 +1,14 @@
1
+ require 'has_settings/setting'
2
+ require 'has_settings/active_record_extension'
3
+ require 'has_settings/has_settings_helper'
4
+
5
+ module HasSettings
6
+ def self.ensure_settings_class(owner_class)
7
+ const_name = "#{owner_class.name}Settings".to_sym
8
+ unless Object.const_defined?(const_name)
9
+ settings_class = Object.const_set(const_name, Class.new(HasSettings::Setting))
10
+ settings_class.owner_class = owner_class
11
+ end
12
+ Object.const_get(const_name)
13
+ end
14
+ end
@@ -0,0 +1,88 @@
1
+ module HasSettings
2
+ module ActiveRecordExtension
3
+ module ClassMethods
4
+ def has_settings(&block)
5
+ settings_class = HasSettings.ensure_settings_class(self)
6
+ settings_class.instance_eval(&block)
7
+
8
+ has_many :settings, :class_name => settings_class.name, :dependent => :destroy do
9
+ moo = settings_class
10
+ def build(*setting_names)
11
+ setting_names.each do |setting_name|
12
+ self << settings_class.new(:name => setting_name)
13
+ end
14
+ end
15
+
16
+ # Define the settings query methods, e.g. +account.settings.wiffle?+
17
+ settings_class.list.each do |key|
18
+ # The query method, does this account have a given setting? account.settings.wiffle?
19
+ define_method "#{key}?" do
20
+ any? { |setting| setting.name.to_sym == key }
21
+ end
22
+
23
+ # The finder method which returns the setting if present, otherwise a new instance, allows
24
+ # non-destructive create and delete operations:
25
+ #
26
+ # account.settings.wiffle.destroy
27
+ # account.settings.wiffle.create
28
+ #
29
+ define_method "#{key}" do
30
+ instance = detect { |setting| setting.name.to_sym == key }
31
+ instance ||= settings_class.new(@owner.class.name.underscore.to_sym => @owner, :name => key.to_s)
32
+ end
33
+ end
34
+ end
35
+
36
+ include HasSettings::ActiveRecordExtension::InstanceMethods
37
+ alias_method_chain :update_attributes, :settings
38
+ alias_method_chain :update_attributes!, :settings
39
+
40
+ end
41
+ end
42
+
43
+ module InstanceMethods
44
+ # Allows you to check for multiple features like account.settings?(:suggestions, :suggestions_on_web)
45
+ def settings?(*setting_names)
46
+ setting_names.all? { |setting_name| settings.send("#{setting_name}?") }
47
+ end
48
+
49
+ def update_attributes_with_settings(attributes)
50
+ update_settings_attributes(attributes)
51
+ update_attributes_without_settings(attributes)
52
+ end
53
+
54
+ def update_attributes_with_settings!(attributes)
55
+ update_settings_attributes(attributes)
56
+ update_attributes_without_settings!(attributes)
57
+ end
58
+
59
+ private
60
+
61
+ def update_settings_attributes(attributes)
62
+ if attributes && settings_attributes = attributes.delete(:settings)
63
+ settings_attributes.each do |setting_name, value|
64
+ setting = settings.send(setting_name)
65
+ if setting.protected?
66
+ logger.warn("Someone tried to mass update the protected #{setting_name} setting")
67
+ else
68
+ if value == '1' || value == true
69
+ setting.create
70
+ else
71
+ setting.destroy
72
+ end
73
+ end
74
+ end
75
+ settings.reset
76
+ end
77
+ end
78
+ end
79
+
80
+ def self.included(receiver)
81
+ receiver.extend(ClassMethods)
82
+ end
83
+ end
84
+ end
85
+
86
+ ActiveRecord::Base.class_eval do
87
+ include HasSettings::ActiveRecordExtension
88
+ end
@@ -0,0 +1,18 @@
1
+ module ActionView
2
+ module Helpers
3
+ def setting_check_box(model_name, method, options = {}, checked_value = "1", unchecked_value = "0")
4
+ the_model = @template.instance_variable_get("@#{model_name}")
5
+ throw "setting_check_box only work on models with settings" unless the_model.respond_to?(:settings)
6
+ options[:checked] = the_model.settings.send("#{method}?")
7
+ options[:id] ||= "#{model_name}_settings_#{method}"
8
+ options[:name] = "#{model_name}[settings][#{method}]"
9
+ @template.check_box(model_name, "settings_#{method}", options, checked_value, unchecked_value)
10
+ end
11
+
12
+ class FormBuilder
13
+ def setting_check_box(method, options = {}, checked_value = "1", unchecked_value = "0")
14
+ @template.setting_check_box(@object_name, method, objectify_options(options), checked_value, unchecked_value)
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,71 @@
1
+ module HasSettings
2
+ class Setting < ActiveRecord::Base
3
+ abstract_class = true
4
+
5
+ after_create :reset_owner_association
6
+ after_destroy :reset_owner_association
7
+
8
+ def create
9
+ if new_record?
10
+ super
11
+ end
12
+ self
13
+ end
14
+
15
+ def self.setting(key, options = nil)
16
+ @settings ||= {}
17
+ @settings[key] = options && options[:protected]
18
+ end
19
+
20
+ def self.list
21
+ @settings.keys
22
+ end
23
+
24
+ def self.table_name
25
+ name.tableize
26
+ end
27
+
28
+ def self.protected?(key)
29
+ @settings[key]
30
+ end
31
+
32
+ def protected?
33
+ self.class.protected?(self.name.to_sym)
34
+ end
35
+
36
+ private
37
+
38
+ def owner_class_instance
39
+ send(self.class.owner_class_sym)
40
+ end
41
+
42
+ def update_owner_timestamp
43
+ owner_class_instance.update_attribute(:updated_at, Time.now) if owner_class_instance && !owner_class_instance.new_record?
44
+ end
45
+
46
+ def reset_owner_association
47
+ owner_class_instance.settings.reload
48
+ end
49
+
50
+ def self.owner_class=(owner_class)
51
+ @owner_class_sym = owner_class.name.underscore.to_sym
52
+ belongs_to owner_class_sym
53
+ validates_presence_of owner_class_sym
54
+ validates_uniqueness_of :type, :scope => owner_class_key_sym
55
+
56
+ if owner_class.table_exists? && owner_class.column_names.include?('updated_at')
57
+ before_create :update_owner_timestamp
58
+ before_destroy :update_owner_timestamp
59
+ end
60
+ end
61
+
62
+ def self.owner_class_sym
63
+ @owner_class_sym
64
+ end
65
+
66
+ def self.owner_class_key_sym
67
+ "#{owner_class_sym}_id".to_sym
68
+ end
69
+ end
70
+
71
+ end
data/test/database.yml ADDED
@@ -0,0 +1,7 @@
1
+ test:
2
+ adapter: mysql
3
+ encoding: utf8
4
+ database: has_settings_test
5
+ username: root
6
+ password:
7
+ socket: /tmp/mysql.sock
@@ -0,0 +1,7 @@
1
+ account1_archive:
2
+ account_id: 1
3
+ name: archive
4
+
5
+ account1_ssl:
6
+ account_id: 1
7
+ name: ssl
@@ -0,0 +1,3 @@
1
+ account1:
2
+ name: My Account
3
+ id: 1
data/test/helper.rb ADDED
@@ -0,0 +1,33 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'active_support'
4
+ require 'active_record'
5
+ require 'active_record/fixtures'
6
+ require 'shoulda'
7
+
8
+ ActiveRecord::Base.configurations = YAML::load(IO.read(File.dirname(__FILE__) + '/database.yml'))
9
+ ActiveRecord::Base.establish_connection('test')
10
+ ActiveRecord::Base.logger = Logger.new(File.dirname(__FILE__) + "/log/test.log")
11
+
12
+ load(File.dirname(__FILE__) + "/schema.rb")
13
+
14
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
15
+ require 'has_settings'
16
+
17
+ class ActiveSupport::TestCase
18
+ include ActiveRecord::TestFixtures
19
+
20
+ def create_fixtures(*table_names)
21
+ if block_given?
22
+ Fixtures.create_fixtures(Test::Unit::TestCase.fixture_path, table_names) { yield }
23
+ else
24
+ Fixtures.create_fixtures(Test::Unit::TestCase.fixture_path, table_names)
25
+ end
26
+ end
27
+
28
+ self.use_transactional_fixtures = true
29
+ self.use_instantiated_fixtures = false
30
+ end
31
+
32
+ ActiveSupport::TestCase.fixture_path = File.dirname(__FILE__) + "/fixtures/"
33
+ $LOAD_PATH.unshift(ActiveSupport::TestCase.fixture_path)
data/test/schema.rb ADDED
@@ -0,0 +1,16 @@
1
+ ActiveRecord::Schema.define(:version => 1) do
2
+
3
+ create_table "account_settings", :force => true do |t|
4
+ t.string "name"
5
+ t.integer "account_id"
6
+ t.datetime "created_at"
7
+ t.datetime "updated_at"
8
+ end
9
+
10
+ create_table "accounts", :force => true do |t|
11
+ t.string "name"
12
+ t.datetime "created_at"
13
+ t.datetime "updated_at"
14
+ end
15
+
16
+ end
@@ -0,0 +1,73 @@
1
+ require 'helper'
2
+
3
+ class Account < ActiveRecord::Base
4
+ has_settings do
5
+ setting :archive_reports
6
+ setting :ssl, :protected => true
7
+ setting :archive
8
+ setting :reports
9
+ end
10
+ end
11
+
12
+ class TestHasSettings < ActiveSupport::TestCase
13
+ context "settings" do
14
+ fixtures :accounts, :account_settings
15
+
16
+ should "be accessable from the owner association" do
17
+ a = Account.create(:name => 'name')
18
+ assert a.settings.empty?
19
+ assert !a.settings.archive?
20
+ assert a.settings.archive.create
21
+ assert a.settings.size == 1
22
+ assert a.settings.archive?
23
+ assert a.settings.archive.id == a.settings.archive.create.id
24
+ end
25
+
26
+ should "be destroyable via the association" do
27
+ a = accounts(:account1)
28
+ assert a.settings.archive?
29
+ assert a.settings.archive.destroy
30
+ a.settings.reload
31
+ assert !a.settings.archive?
32
+ end
33
+
34
+ should "have convenience methods for checking presence" do
35
+ a = accounts(:account1)
36
+ assert a.settings.archive?
37
+ assert a.settings.ssl?
38
+ assert !a.settings.reports?
39
+ end
40
+
41
+ should "support mass updates" do
42
+ a = Account.create(:name => 'name')
43
+ assert a.settings.empty?
44
+ assert !a.settings.archive?
45
+ assert !a.settings.reports?
46
+
47
+ a.update_attributes(:settings => { :archive => '1', :reports => '1' })
48
+ assert a.settings.archive?
49
+ assert a.settings.reports?
50
+
51
+ a.update_attributes(:settings => { :archive => '0', :reports => '0' })
52
+ assert !a.settings.archive?
53
+ assert !a.settings.reports?
54
+ end
55
+
56
+ should "support protecting certain settings from mass updates" do
57
+ a = Account.create(:name => 'name')
58
+ assert a.settings.empty?
59
+ assert !a.settings.archive?
60
+ assert !a.settings.ssl?
61
+
62
+ a.update_attributes(:settings => {:archive => '1', :ssl => '1'})
63
+ assert a.settings.archive?
64
+ assert !a.settings.ssl?
65
+
66
+ assert a.settings.ssl.create
67
+ assert a.settings.ssl?
68
+ a.update_attributes(:settings => {:archive => '0', :ssl => '0'})
69
+ assert !a.settings.archive?
70
+ assert a.settings.ssl?
71
+ end
72
+ end
73
+ end
metadata ADDED
@@ -0,0 +1,99 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: has_settings
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - Morten Primdahl
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-11-04 00:00:00 -07:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: thoughtbot-shoulda
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 3
30
+ segments:
31
+ - 0
32
+ version: "0"
33
+ type: :development
34
+ version_requirements: *id001
35
+ description: This gem is an ActiveRecord extension which provides a convenient interface for managing per row settings
36
+ email: morten@zendesk.com
37
+ executables: []
38
+
39
+ extensions: []
40
+
41
+ extra_rdoc_files:
42
+ - LICENSE
43
+ - README.rdoc
44
+ files:
45
+ - .document
46
+ - .gitignore
47
+ - LICENSE
48
+ - README.rdoc
49
+ - Rakefile
50
+ - VERSION
51
+ - has_settings.gemspec
52
+ - lib/has_settings.rb
53
+ - lib/has_settings/active_record_extension.rb
54
+ - lib/has_settings/has_settings_helper.rb
55
+ - lib/has_settings/setting.rb
56
+ - test/database.yml
57
+ - test/fixtures/account_settings.yml
58
+ - test/fixtures/accounts.yml
59
+ - test/helper.rb
60
+ - test/schema.rb
61
+ - test/test_has_settings.rb
62
+ has_rdoc: true
63
+ homepage: http://github.com/morten/has_settings
64
+ licenses: []
65
+
66
+ post_install_message:
67
+ rdoc_options:
68
+ - --charset=UTF-8
69
+ require_paths:
70
+ - lib
71
+ required_ruby_version: !ruby/object:Gem::Requirement
72
+ none: false
73
+ requirements:
74
+ - - ">="
75
+ - !ruby/object:Gem::Version
76
+ hash: 3
77
+ segments:
78
+ - 0
79
+ version: "0"
80
+ required_rubygems_version: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ">="
84
+ - !ruby/object:Gem::Version
85
+ hash: 3
86
+ segments:
87
+ - 0
88
+ version: "0"
89
+ requirements: []
90
+
91
+ rubyforge_project:
92
+ rubygems_version: 1.3.7
93
+ signing_key:
94
+ specification_version: 3
95
+ summary: Per row settings for ActiveRecord
96
+ test_files:
97
+ - test/helper.rb
98
+ - test/schema.rb
99
+ - test/test_has_settings.rb