attribute_enums 0.1.8 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (66) hide show
  1. data/.gitignore +1 -3
  2. data/.rspec +1 -0
  3. data/CHANGELOG +4 -0
  4. data/Gemfile +1 -1
  5. data/README.markdown +61 -59
  6. data/attribute_enums.gemspec +5 -16
  7. data/lib/attribute_enums.rb +0 -4
  8. data/lib/attribute_enums/active_record.rb +122 -0
  9. data/lib/attribute_enums/common.rb +168 -0
  10. data/lib/attribute_enums/railtie.rb +2 -2
  11. data/lib/attribute_enums/version.rb +1 -1
  12. data/spec/common_and_active_record/ask_methods_options_spec.rb +58 -0
  13. data/spec/common_and_active_record/attribute_name_spec.rb +7 -0
  14. data/spec/common_and_active_record/boolean_options_spec.rb +43 -0
  15. data/spec/common_and_active_record/default_options_spec.rb +77 -0
  16. data/spec/common_and_active_record/i18n_options_spec.rb +116 -0
  17. data/spec/common_and_active_record/in_options_spec.rb +38 -0
  18. data/spec/common_and_active_record/readme_user_example_spec.rb +87 -0
  19. data/spec/common_and_active_record/scopeds_options_spec.rb +114 -0
  20. data/spec/common_and_active_record/validate_options_spec.rb +82 -0
  21. data/spec/spec_helper.rb +5 -12
  22. data/spec/support/locales/person_en.yml +14 -0
  23. data/spec/support/locales/person_zh-CN.yml +14 -0
  24. data/spec/support/locales/user_en.yml +7 -15
  25. data/spec/support/models/person.rb +23 -0
  26. metadata +34 -229
  27. data/Guardfile +0 -22
  28. data/lib/attribute_enums/active_record_extension.rb +0 -88
  29. data/spec/models/active_record/user_spec.rb +0 -67
  30. data/spec/rails3_0_app/.gitignore +0 -4
  31. data/spec/rails3_0_app/.rspec +0 -1
  32. data/spec/rails3_0_app/Gemfile +0 -13
  33. data/spec/rails3_0_app/Rakefile +0 -7
  34. data/spec/rails3_0_app/app/controllers/application_controller.rb +0 -3
  35. data/spec/rails3_0_app/app/helpers/application_helper.rb +0 -2
  36. data/spec/rails3_0_app/app/models/user.rb +0 -2
  37. data/spec/rails3_0_app/app/views/layouts/application.html.erb +0 -14
  38. data/spec/rails3_0_app/config.ru +0 -4
  39. data/spec/rails3_0_app/config/application.rb +0 -44
  40. data/spec/rails3_0_app/config/boot.rb +0 -6
  41. data/spec/rails3_0_app/config/database.yml +0 -19
  42. data/spec/rails3_0_app/config/environment.rb +0 -6
  43. data/spec/rails3_0_app/config/environments/development.rb +0 -26
  44. data/spec/rails3_0_app/config/environments/production.rb +0 -49
  45. data/spec/rails3_0_app/config/environments/test.rb +0 -35
  46. data/spec/rails3_0_app/config/initializers/backtrace_silencers.rb +0 -7
  47. data/spec/rails3_0_app/config/initializers/inflections.rb +0 -10
  48. data/spec/rails3_0_app/config/initializers/mime_types.rb +0 -5
  49. data/spec/rails3_0_app/config/initializers/secret_token.rb +0 -7
  50. data/spec/rails3_0_app/config/initializers/session_store.rb +0 -8
  51. data/spec/rails3_0_app/config/locales/en.yml +0 -5
  52. data/spec/rails3_0_app/config/routes.rb +0 -58
  53. data/spec/rails3_0_app/db/migrate/20111129011508_create_users.rb +0 -14
  54. data/spec/rails3_0_app/db/schema.rb +0 -23
  55. data/spec/rails3_0_app/db/seeds.rb +0 -7
  56. data/spec/rails3_0_app/lib/tasks/.gitkeep +0 -0
  57. data/spec/rails3_0_app/public/404.html +0 -26
  58. data/spec/rails3_0_app/public/422.html +0 -26
  59. data/spec/rails3_0_app/public/500.html +0 -26
  60. data/spec/rails3_0_app/public/favicon.ico +0 -0
  61. data/spec/rails3_0_app/public/images/rails.png +0 -0
  62. data/spec/rails3_0_app/public/index.html +0 -239
  63. data/spec/rails3_0_app/public/robots.txt +0 -5
  64. data/spec/rails3_0_app/public/stylesheets/.gitkeep +0 -0
  65. data/spec/rails3_0_app/script/rails +0 -6
  66. data/spec/rails3_0_app/vendor/plugins/.gitkeep +0 -0
data/Guardfile DELETED
@@ -1,22 +0,0 @@
1
- # A sample Guardfile
2
- # More info at https://github.com/guard/guard#readme
3
-
4
- guard 'rspec', :version => 2 do
5
- watch(%r{^spec/.+_spec\.rb$})
6
- watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
7
- watch('spec/spec_helper.rb') { "spec" }
8
-
9
- # Rails example
10
- watch(%r{^spec/.+_spec\.rb$})
11
- watch(%r{^app/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
12
- watch(%r{^app/(.*)(\.erb|\.haml)$}) { |m| "spec/#{m[1]}#{m[2]}_spec.rb" }
13
- watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
14
- watch(%r{^app/controllers/(.+)_(controller)\.rb$}) { |m| ["spec/routing/#{m[1]}_routing_spec.rb", "spec/#{m[2]}s/#{m[1]}_#{m[2]}_spec.rb", "spec/acceptance/#{m[1]}_spec.rb"] }
15
- watch(%r{^spec/support/(.+)\.rb$}) { "spec" }
16
- watch('spec/spec_helper.rb') { "spec" }
17
- watch('config/routes.rb') { "spec/routing" }
18
- watch('app/controllers/application_controller.rb') { "spec/controllers" }
19
- # Capybara request specs
20
- watch(%r{^app/views/(.+)/.*\.(erb|haml)$}) { |m| "spec/requests/#{m[1]}_spec.rb" }
21
- end
22
-
@@ -1,88 +0,0 @@
1
- module AttributeEnums
2
- module ActiveRecordExtension
3
-
4
- extend ActiveSupport::Concern
5
-
6
- module ClassMethods
7
- def attribute_enums(attribute_name, *options, &block)
8
- attribute_name = attribute_name.to_s
9
- options = options.extract_options!
10
- within = options.delete(:in).to_a || options.delete(:within).to_a || []
11
- scopes = options.delete(:scopes) || true
12
- allow_blank = options.delete(:allow_blank) || false
13
- use_validate = options.delete(:validate) || true
14
- methods = options.delete(:methods) || true
15
- i18n = options.delete(:i18n) || true
16
- booleans = options.delete(:booleans) || false
17
-
18
- default_field_value = options.delete(:default)
19
- default_field_value = true if default_field_value.nil? and booleans
20
- raise "if booleans, default must be in true/false" if booleans and ![true, false].index(default_field_value)
21
-
22
- default = (default_field_value.nil? ? false : true)
23
-
24
- return if !self.table_exists? or !self.respond_to?(:column_names)
25
- raise "attribute #{ attribute_name } does not exist." unless column_names.index(attribute_name)
26
- raise "in/within and booleans has one and only one exist." if (!within.blank? and !booleans.blank?) or (within.blank? and booleans.blank?)
27
-
28
-
29
- if booleans
30
- within = [true, false]
31
- else
32
- within.keep_if{|x| x.respond_to?("to_sym")}.collect!(&:to_s)
33
- end
34
-
35
- attribute_values = Hash[ within.map {|x| [ x, i18n ? I18n.translate(:"#{self.i18n_scope}.enums.#{self.model_name.underscore}.#{ attribute_name }.#{ x }") : x ]} ]
36
- eval(%Q`class_attribute :#{ attribute_name}_values`)
37
- eval(%Q`self.#{ attribute_name}_values = #{ attribute_values }`)
38
-
39
- if scopes
40
- if booleans
41
- scope attribute_name, where(attribute_name => true)
42
- eval(%Q`scope :not_#{ attribute_name }, where(:#{ attribute_name } => false)`)
43
- else
44
- within.all? do |value|
45
- scope value, where(attribute_name => value)
46
- end
47
- end
48
- end
49
-
50
-
51
- if use_validate
52
- if booleans
53
- validates attribute_name, :inclusion => { :in => [true, false] }
54
- else
55
- unless allow_blank
56
- validates attribute_name, :presence => true
57
- end
58
- validates attribute_name, :inclusion => { :in => within + within.collect{|x| x.to_sym} },
59
- :if => eval(%Q`Proc.new { |record| record.#{ attribute_name }? }`)
60
- end
61
- end
62
-
63
- if methods
64
- eval(%Q`def get_#{ attribute_name }_values; self.#{ attribute_name}_values.map {|k,v| [v,k]}; end`)
65
- if booleans
66
- class_eval(%Q`def #{ attribute_name }_text; self.class.#{ attribute_name}_values[read_attribute("#{ attribute_name }")]; end`)
67
- else
68
- class_eval(%Q`def #{ attribute_name }_text; self.class.#{ attribute_name}_values[read_attribute("#{ attribute_name }").to_s]; end`)
69
- end
70
- unless booleans
71
- within.each {|name| class_eval(%Q`def #{ name }?; read_attribute("#{ attribute_name }").to_s == "#{ name }"; end`) }
72
- end
73
- end
74
-
75
- if default
76
- if booleans
77
- class_eval(%Q`def set_default_for_attr_#{ attribute_name }; self.#{ attribute_name } = #{ default_field_value } if read_attribute("#{ attribute_name }").nil? ; end`)
78
- else
79
- class_eval(%Q`def set_default_for_attr_#{ attribute_name }; self.#{ attribute_name } ||= "#{ default_field_value.to_s }"; end`)
80
- end
81
- eval(%Q`before_validation :set_default_for_attr_#{ attribute_name }`)
82
- end
83
- end
84
- end
85
- end
86
-
87
- end
88
-
@@ -1,67 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe User do
4
- context "test :in" do
5
- before {
6
- User.attribute_enums :gender, :in => [:female, :male]
7
- }
8
- it { User.male.new.gender.should == "male" }
9
- it { User.female.new.gender.should == "female" }
10
- it { User.get_gender_values.should == [["Girl", "female"], ["Boy", "male"]] }
11
- it { User.gender_values.should == {"female"=>"Girl", "male"=>"Boy"} }
12
-
13
- let(:user) { User.create(:gender => :male) }
14
- it { user.gender_text.should == "Boy" }
15
- it { user.male?.should be_true }
16
- it { user.female?.should be_false }
17
-
18
- #:in default == nil
19
- it { User.create.gender.should be_nil }
20
- end
21
-
22
- context "test :in and :default" do
23
- before {
24
- User.attribute_enums :gender, :in => [:female, :male], :default => :female
25
- }
26
- let(:user) { User.create() }
27
- it { user.gender.should_not be_nil }
28
- it { user.gender.should == "female" }
29
- end
30
-
31
-
32
- context "test :booleans" do
33
- before {
34
- User.attribute_enums :enable, :booleans => true
35
- }
36
- it { User.enable.new.enable.should == true }
37
- it { User.not_enable.new.enable.should == false }
38
- it { User.get_enable_values.should == [["Yes", true], ["No", false]] }
39
- it { User.enable_values.should == {true=>"Yes", false=>"No"} }
40
-
41
- let(:user) { User.create(:enable => true) }
42
- it { user.enable.should be_true }
43
- it { user.enable_text.should == "Yes" }
44
-
45
- #:booleans default == true
46
- it { User.create.enable.should be_true }
47
- end
48
-
49
- context "test :booleans and :default => false" do
50
- before {
51
- User.attribute_enums :enable, :booleans => true, :default => false
52
- }
53
- let(:user) { User.create() }
54
- it { user.enable.should be_false }
55
- it { user.enable.should_not be_nil }
56
- end
57
-
58
- context "test :booleans and :default => true" do
59
- before {
60
- User.attribute_enums :enable, :booleans => true, :default => true
61
- }
62
- let(:user) { User.create() }
63
- it { user.enable.should be_true }
64
- it { user.enable.should_not be_nil }
65
- end
66
- end
67
-
@@ -1,4 +0,0 @@
1
- .bundle
2
- db/*.sqlite3
3
- log/*.log
4
- tmp/
@@ -1 +0,0 @@
1
- --colour
@@ -1,13 +0,0 @@
1
- source 'http://rubygems.org'
2
-
3
- gem 'rails', '3.0.11'
4
- gem 'sqlite3-ruby', :require => 'sqlite3'
5
-
6
- gem "attribute_enums", :path => '../../'
7
-
8
- gem 'pry'
9
-
10
- group :development do
11
- gem 'pry-padrino'
12
- end
13
-
@@ -1,7 +0,0 @@
1
- # Add your own tasks in files placed in lib/tasks ending in .rake,
2
- # for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.
3
-
4
- require File.expand_path('../config/application', __FILE__)
5
- require 'rake'
6
-
7
- Rails30App::Application.load_tasks
@@ -1,3 +0,0 @@
1
- class ApplicationController < ActionController::Base
2
- protect_from_forgery
3
- end
@@ -1,2 +0,0 @@
1
- module ApplicationHelper
2
- end
@@ -1,2 +0,0 @@
1
- class User < ActiveRecord::Base
2
- end
@@ -1,14 +0,0 @@
1
- <!DOCTYPE html>
2
- <html>
3
- <head>
4
- <title>Rails30App</title>
5
- <%= stylesheet_link_tag :all %>
6
- <%= javascript_include_tag :defaults %>
7
- <%= csrf_meta_tag %>
8
- </head>
9
- <body>
10
-
11
- <%= yield %>
12
-
13
- </body>
14
- </html>
@@ -1,4 +0,0 @@
1
- # This file is used by Rack-based servers to start the application.
2
-
3
- require ::File.expand_path('../config/environment', __FILE__)
4
- run Rails30App::Application
@@ -1,44 +0,0 @@
1
- require File.expand_path('../boot', __FILE__)
2
-
3
- require 'rails/all'
4
-
5
- # If you have a Gemfile, require the gems listed there, including any gems
6
- # you've limited to :test, :development, or :production.
7
- Bundler.require(:default, Rails.env) if defined?(Bundler)
8
-
9
- module Rails30App
10
- class Application < Rails::Application
11
- # Settings in config/environments/* take precedence over those specified here.
12
- # Application configuration should go into files in config/initializers
13
- # -- all .rb files in that directory are automatically loaded.
14
-
15
- # Custom directories with classes and modules you want to be autoloadable.
16
- # config.autoload_paths += %W(#{config.root}/extras)
17
-
18
- # Only load the plugins named here, in the order given (default is alphabetical).
19
- # :all can be used as a placeholder for all plugins not explicitly named.
20
- # config.plugins = [ :exception_notification, :ssl_requirement, :all ]
21
-
22
- # Activate observers that should always be running.
23
- # config.active_record.observers = :cacher, :garbage_collector, :forum_observer
24
-
25
- # Set Time.zone default to the specified zone and make Active Record auto-convert to this zone.
26
- # Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC.
27
- # config.time_zone = 'Central Time (US & Canada)'
28
-
29
- # The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
30
- # config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]
31
- # config.i18n.default_locale = :de
32
-
33
- # JavaScript files you want as :defaults (application.js is always included).
34
- # config.action_view.javascript_expansions[:defaults] = %w(jquery rails)
35
- config.action_view.javascript_expansions[:defaults] = %w()
36
-
37
- # Configure the default encoding used in templates for Ruby 1.9.
38
- config.encoding = "utf-8"
39
-
40
- # Configure sensitive parameters which will be filtered from the log file.
41
- config.filter_parameters += [:password]
42
- end
43
- end
44
-
@@ -1,6 +0,0 @@
1
- require 'rubygems'
2
-
3
- # Set up gems listed in the Gemfile.
4
- ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../Gemfile', __FILE__)
5
-
6
- require 'bundler/setup' if File.exists?(ENV['BUNDLE_GEMFILE'])
@@ -1,19 +0,0 @@
1
- development:
2
- adapter: sqlite3
3
- database: db/development.sqlite3
4
- pool: 5
5
- timeout: 5000
6
-
7
- test:
8
- adapter: sqlite3
9
- database: ":memory:"
10
-
11
- production:
12
- adapter: sqlite3
13
- database: ":memory:"
14
-
15
- #in_memory:
16
- # adapter: sqlite3
17
- # database: ":memory:"
18
- # verbosity: quiet
19
-
@@ -1,6 +0,0 @@
1
- # Load the rails application
2
- require File.expand_path('../application', __FILE__)
3
-
4
- # Initialize the rails application
5
- Rails30App::Application.initialize!
6
-
@@ -1,26 +0,0 @@
1
- Rails30App::Application.configure do
2
- # Settings specified here will take precedence over those in config/application.rb
3
-
4
- # In the development environment your application's code is reloaded on
5
- # every request. This slows down response time but is perfect for development
6
- # since you don't have to restart the webserver when you make code changes.
7
- config.cache_classes = false
8
-
9
- # Log error messages when you accidentally call methods on nil.
10
- config.whiny_nils = true
11
-
12
- # Show full error reports and disable caching
13
- config.consider_all_requests_local = true
14
- config.action_view.debug_rjs = true
15
- config.action_controller.perform_caching = false
16
-
17
- # Don't care if the mailer can't send
18
- config.action_mailer.raise_delivery_errors = false
19
-
20
- # Print deprecation notices to the Rails logger
21
- config.active_support.deprecation = :log
22
-
23
- # Only use best-standards-support built into browsers
24
- config.action_dispatch.best_standards_support = :builtin
25
- end
26
-
@@ -1,49 +0,0 @@
1
- Rails30App::Application.configure do
2
- # Settings specified here will take precedence over those in config/application.rb
3
-
4
- # The production environment is meant for finished, "live" apps.
5
- # Code is not reloaded between requests
6
- config.cache_classes = true
7
-
8
- # Full error reports are disabled and caching is turned on
9
- config.consider_all_requests_local = false
10
- config.action_controller.perform_caching = true
11
-
12
- # Specifies the header that your server uses for sending files
13
- config.action_dispatch.x_sendfile_header = "X-Sendfile"
14
-
15
- # For nginx:
16
- # config.action_dispatch.x_sendfile_header = 'X-Accel-Redirect'
17
-
18
- # If you have no front-end server that supports something like X-Sendfile,
19
- # just comment this out and Rails will serve the files
20
-
21
- # See everything in the log (default is :info)
22
- # config.log_level = :debug
23
-
24
- # Use a different logger for distributed setups
25
- # config.logger = SyslogLogger.new
26
-
27
- # Use a different cache store in production
28
- # config.cache_store = :mem_cache_store
29
-
30
- # Disable Rails's static asset server
31
- # In production, Apache or nginx will already do this
32
- config.serve_static_assets = false
33
-
34
- # Enable serving of images, stylesheets, and javascripts from an asset server
35
- # config.action_controller.asset_host = "http://assets.example.com"
36
-
37
- # Disable delivery errors, bad email addresses will be ignored
38
- # config.action_mailer.raise_delivery_errors = false
39
-
40
- # Enable threaded mode
41
- # config.threadsafe!
42
-
43
- # Enable locale fallbacks for I18n (makes lookups for any locale fall back to
44
- # the I18n.default_locale when a translation can not be found)
45
- config.i18n.fallbacks = true
46
-
47
- # Send deprecation notices to registered listeners
48
- config.active_support.deprecation = :notify
49
- end
@@ -1,35 +0,0 @@
1
- Rails30App::Application.configure do
2
- # Settings specified here will take precedence over those in config/application.rb
3
-
4
- # The test environment is used exclusively to run your application's
5
- # test suite. You never need to work with it otherwise. Remember that
6
- # your test database is "scratch space" for the test suite and is wiped
7
- # and recreated between test runs. Don't rely on the data there!
8
- config.cache_classes = true
9
-
10
- # Log error messages when you accidentally call methods on nil.
11
- config.whiny_nils = true
12
-
13
- # Show full error reports and disable caching
14
- config.consider_all_requests_local = true
15
- config.action_controller.perform_caching = false
16
-
17
- # Raise exceptions instead of rendering exception templates
18
- config.action_dispatch.show_exceptions = false
19
-
20
- # Disable request forgery protection in test environment
21
- config.action_controller.allow_forgery_protection = false
22
-
23
- # Tell Action Mailer not to deliver emails to the real world.
24
- # The :test delivery method accumulates sent emails in the
25
- # ActionMailer::Base.deliveries array.
26
- config.action_mailer.delivery_method = :test
27
-
28
- # Use SQL instead of Active Record's schema dumper when creating the test database.
29
- # This is necessary if your schema can't be completely dumped by the schema dumper,
30
- # like if you have constraints or database-specific column types
31
- # config.active_record.schema_format = :sql
32
-
33
- # Print deprecation notices to the stderr
34
- config.active_support.deprecation = :stderr
35
- end