Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Gemified

  • Loading branch information...
commit 15375e255d1737e09a4939727a4c7636ea5a64c5 1 parent 7a50aff
@ledermann authored
View
4 .gitignore
@@ -0,0 +1,4 @@
+*.gem
+.bundle
+Gemfile.lock
+pkg/*
View
4 Gemfile
@@ -0,0 +1,4 @@
+source "http://rubygems.org"
+
+# Specify your gem's dependencies in rails-settings.gemspec
+gemspec
View
17 README.md
@@ -1,8 +1,8 @@
-# Settings Plugin for Rails
+# Settings Gem/Plugin for Rails
[![Build Status](https://secure.travis-ci.org/ledermann/rails-settings.png)](http://travis-ci.org/ledermann/rails-settings)
-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.
+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.
## Requirements
@@ -12,7 +12,16 @@ ActiveRecord 2.3.x, 3.0.x or 3.1.x
Tested with Ruby 1.8.7, 1.9.2, 1.9.3 and RBX2.0
-## Setup
+## Installation
+
+Include the gem in your Gemfile
+
+ gem 'ledermann-rails-settings', :require => 'rails-settings'
+
+or install as a plugin:
+
+ ./script/plugin install git://github.com/ledermann/rails-settings.git
+
You have to create the table used by the Settings model by using this migration:
@@ -34,7 +43,7 @@ You have to create the table used by the Settings model by using this migration:
end
end
-Now put update your database with:
+Now update your database with:
rake db:migrate
View
4 Rakefile
@@ -6,4 +6,6 @@ Rake::TestTask.new do |t|
t.verbose = true
end
-task :default => :test
+task :default => :test
+
+require 'bundler/gem_tasks'
View
33 init.rb
@@ -1,32 +1 @@
-require 'settings'
-
-ActiveRecord::Base.class_eval do
- def self.has_settings
- class_eval do
- def settings
- ScopedSettings.for_target(self)
- end
-
- after_destroy { |user| user.settings.target_scoped.delete_all }
-
- scope_method = ActiveRecord::VERSION::MAJOR < 3 ? :named_scope : :scope
-
- send scope_method, :with_settings, :joins => "JOIN settings ON (settings.target_id = #{self.table_name}.#{self.primary_key} AND
- settings.target_type = '#{self.base_class.name}')",
- :select => "DISTINCT #{self.table_name}.*"
-
- send scope_method, :with_settings_for, lambda { |var| { :joins => "JOIN settings ON (settings.target_id = #{self.table_name}.#{self.primary_key} AND
- settings.target_type = '#{self.base_class.name}') AND
- settings.var = '#{var}'" } }
-
- send scope_method, :without_settings, :joins => "LEFT JOIN settings ON (settings.target_id = #{self.table_name}.#{self.primary_key} AND
- settings.target_type = '#{self.base_class.name}')",
- :conditions => 'settings.id IS NULL'
-
- send scope_method, :without_settings_for, lambda { |var| { :joins => "LEFT JOIN settings ON (settings.target_id = #{self.table_name}.#{self.primary_key} AND
- settings.target_type = '#{self.base_class.name}') AND
- settings.var = '#{var}'",
- :conditions => 'settings.id IS NULL' } }
- end
- end
-end
+require 'rails-settings'
View
4 lib/rails-settings.rb
@@ -0,0 +1,4 @@
+require 'rails-settings/version'
+require 'rails-settings/active_record'
+require 'rails-settings/settings'
+require 'rails-settings/scoped_settings'
View
30 lib/rails-settings/active_record.rb
@@ -0,0 +1,30 @@
+ActiveRecord::Base.class_eval do
+ def self.has_settings
+ class_eval do
+ def settings
+ ScopedSettings.for_target(self)
+ end
+
+ after_destroy { |user| user.settings.target_scoped.delete_all }
+
+ scope_method = ActiveRecord::VERSION::MAJOR < 3 ? :named_scope : :scope
+
+ send scope_method, :with_settings, :joins => "JOIN settings ON (settings.target_id = #{self.table_name}.#{self.primary_key} AND
+ settings.target_type = '#{self.base_class.name}')",
+ :select => "DISTINCT #{self.table_name}.*"
+
+ send scope_method, :with_settings_for, lambda { |var| { :joins => "JOIN settings ON (settings.target_id = #{self.table_name}.#{self.primary_key} AND
+ settings.target_type = '#{self.base_class.name}') AND
+ settings.var = '#{var}'" } }
+
+ send scope_method, :without_settings, :joins => "LEFT JOIN settings ON (settings.target_id = #{self.table_name}.#{self.primary_key} AND
+ settings.target_type = '#{self.base_class.name}')",
+ :conditions => 'settings.id IS NULL'
+
+ send scope_method, :without_settings_for, lambda { |var| { :joins => "LEFT JOIN settings ON (settings.target_id = #{self.table_name}.#{self.primary_key} AND
+ settings.target_type = '#{self.base_class.name}') AND
+ settings.var = '#{var}'",
+ :conditions => 'settings.id IS NULL' } }
+ end
+ end
+end
View
14 lib/rails-settings/scoped_settings.rb
@@ -0,0 +1,14 @@
+class ScopedSettings < Settings
+ def self.for_target(target)
+ @target = target
+ self
+ end
+
+ def self.target_id
+ @target.id
+ end
+
+ def self.target_type
+ @target.class.base_class.to_s
+ end
+end
View
245 lib/settings.rb → lib/rails-settings/settings.rb
@@ -1,131 +1,116 @@
-class Settings < ActiveRecord::Base
- class SettingNotFound < RuntimeError; end
-
- cattr_accessor :defaults
- @@defaults = {}.with_indifferent_access
-
- # Support old plugin
- if defined?(SettingsDefaults::DEFAULTS)
- @@defaults = SettingsDefaults::DEFAULTS.with_indifferent_access
- end
-
- #get or set a variable with the variable as the called method
- def self.method_missing(method, *args)
- if self.respond_to?(method)
- super
- else
- method_name = method.to_s
-
- #set a value for a variable
- if method_name =~ /=$/
- var_name = method_name.gsub('=', '')
- value = args.first
- self[var_name] = value
-
- #retrieve a value
- else
- self[method_name]
-
- end
- end
- end
-
- #destroy the specified settings record
- def self.destroy(var_name)
- var_name = var_name.to_s
- begin
- target(var_name).destroy
- true
- rescue NoMethodError
- raise SettingNotFound, "Setting variable \"#{var_name}\" not found"
- end
- end
-
- #retrieve all settings as a hash (optionally starting with a given namespace)
- def self.all(starting_with=nil)
- options = starting_with ? { :conditions => "var LIKE '#{starting_with}%'"} : {}
- vars = target_scoped.find(:all, {:select => 'var, value'}.merge(options))
-
- result = {}
- vars.each do |record|
- result[record.var] = record.value
- end
- result.with_indifferent_access
- end
-
- #get a setting value by [] notation
- def self.[](var_name)
- if var = target(var_name)
- var.value
- else
- @@defaults[var_name.to_s]
- end
- end
-
- #set a setting value by [] notation
- def self.[]=(var_name, value)
- record = target_scoped.find_or_initialize_by_var(var_name.to_s)
- record.value = value
- record.save!
-
- value
- end
-
- def self.merge!(var_name, hash_value)
- raise ArgumentError unless hash_value.is_a?(Hash)
-
- old_value = self[var_name] || {}
- raise TypeError, "Existing value is not a hash, can't merge!" unless old_value.is_a?(Hash)
-
- new_value = old_value.merge(hash_value)
- self[var_name] = new_value if new_value != old_value
-
- new_value
- end
-
- def self.target(var_name)
- target_scoped.find_by_var(var_name.to_s)
- end
-
- #get the value field, YAML decoded
- def value
- YAML::load(self[:value])
- end
-
- #set the value field, YAML encoded
- def value=(new_value)
- self[:value] = new_value.to_yaml
- end
-
- def self.target_scoped
- Settings.scoped_by_target_type_and_target_id(target_type, target_id)
- end
-
- #Deprecated!
- def self.reload # :nodoc:
- self
- end
-
- def self.target_id
- nil
- end
-
- def self.target_type
- nil
- end
-end
-
-class ScopedSettings < Settings
- def self.for_target(target)
- @target = target
- self
- end
-
- def self.target_id
- @target.id
- end
-
- def self.target_type
- @target.class.base_class.to_s
- end
+class Settings < ActiveRecord::Base
+ class SettingNotFound < RuntimeError; end
+
+ cattr_accessor :defaults
+ @@defaults = {}.with_indifferent_access
+
+ # Support old plugin
+ if defined?(SettingsDefaults::DEFAULTS)
+ @@defaults = SettingsDefaults::DEFAULTS.with_indifferent_access
+ end
+
+ #get or set a variable with the variable as the called method
+ def self.method_missing(method, *args)
+ if self.respond_to?(method)
+ super
+ else
+ method_name = method.to_s
+
+ #set a value for a variable
+ if method_name =~ /=$/
+ var_name = method_name.gsub('=', '')
+ value = args.first
+ self[var_name] = value
+
+ #retrieve a value
+ else
+ self[method_name]
+
+ end
+ end
+ end
+
+ #destroy the specified settings record
+ def self.destroy(var_name)
+ var_name = var_name.to_s
+ begin
+ target(var_name).destroy
+ true
+ rescue NoMethodError
+ raise SettingNotFound, "Setting variable \"#{var_name}\" not found"
+ end
+ end
+
+ #retrieve all settings as a hash (optionally starting with a given namespace)
+ def self.all(starting_with=nil)
+ options = starting_with ? { :conditions => "var LIKE '#{starting_with}%'"} : {}
+ vars = target_scoped.find(:all, {:select => 'var, value'}.merge(options))
+
+ result = {}
+ vars.each do |record|
+ result[record.var] = record.value
+ end
+ result.with_indifferent_access
+ end
+
+ #get a setting value by [] notation
+ def self.[](var_name)
+ if var = target(var_name)
+ var.value
+ else
+ @@defaults[var_name.to_s]
+ end
+ end
+
+ #set a setting value by [] notation
+ def self.[]=(var_name, value)
+ record = target_scoped.find_or_initialize_by_var(var_name.to_s)
+ record.value = value
+ record.save!
+
+ value
+ end
+
+ def self.merge!(var_name, hash_value)
+ raise ArgumentError unless hash_value.is_a?(Hash)
+
+ old_value = self[var_name] || {}
+ raise TypeError, "Existing value is not a hash, can't merge!" unless old_value.is_a?(Hash)
+
+ new_value = old_value.merge(hash_value)
+ self[var_name] = new_value if new_value != old_value
+
+ new_value
+ end
+
+ def self.target(var_name)
+ target_scoped.find_by_var(var_name.to_s)
+ end
+
+ #get the value field, YAML decoded
+ def value
+ YAML::load(self[:value])
+ end
+
+ #set the value field, YAML encoded
+ def value=(new_value)
+ self[:value] = new_value.to_yaml
+ end
+
+ def self.target_scoped
+ Settings.scoped_by_target_type_and_target_id(target_type, target_id)
+ end
+
+ #Deprecated!
+ def self.reload # :nodoc:
+ self
+ end
+
+ def self.target_id
+ nil
+ end
+
+ def self.target_type
+ nil
+ end
end
View
3  lib/rails-settings/version.rb
@@ -0,0 +1,3 @@
+module RailsSettings
+ VERSION = '1.0.0'
+end
View
24 rails-settings.gemspec
@@ -0,0 +1,24 @@
+# -*- encoding: utf-8 -*-
+$:.push File.expand_path('../lib', __FILE__)
+require 'rails-settings/version'
+
+Gem::Specification.new do |s|
+ s.name = 'ledermann-rails-settings'
+ s.version = RailsSettings::VERSION
+ s.authors = ['Georg Ledermann']
+ s.email = ['mail@georg-ledermann.de']
+ s.homepage = 'https://github.com/ledermann/rails-settings'
+ s.summary = %q{Settings management for ActiveRecord objects}
+ s.description = %q{Ruby Gem 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.}
+
+ s.rubyforge_project = 'ledermann-rails-settings'
+
+ s.files = `git ls-files`.split("\n")
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
+ s.require_paths = ['lib']
+
+ # specify any dependencies here; for example:
+ # s.add_development_dependency "rspec"
+ # s.add_runtime_dependency "rest-client"
+end
Please sign in to comment.
Something went wrong with that request. Please try again.