nofxx-symbolize 1.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,5 @@
1
+ .DS_Store
2
+ coverage
3
+ rdoc
4
+ doc
5
+ pkg
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2007-2008 Andreas Neuhaus <zargony@zargony.com>
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 ADDED
@@ -0,0 +1,164 @@
1
+ = Symbolize attribute values in ActiveRecord (e.g. for nicer enums)
2
+
3
+ This plugin introduces an easy way to use symbols for values of ActiveRecord
4
+ attributes. Symbolized attributes return a ruby symbol (or nil) as their value
5
+ and can be set using symbols.
6
+
7
+
8
+ == About
9
+
10
+ Since ActiveRecord does not natively support database column types of ENUM or
11
+ SET, you'll usually use a string attribute and restrict it to certain values
12
+ with validations. Using this plugin, the values of such pseudo-enums are
13
+ symbols, which look more ruby-style than strings.
14
+
15
+ Simply add "symbolize :attr_name" to your model class, and the specified
16
+ attribute will return symbol values and can be set using smbols (setting
17
+ string values will still work, which is important when using forms).
18
+
19
+ An attribute to symbolize should be a string (varchar) column in the database.
20
+
21
+ Blog: http://zargony.com/
22
+ Github: http://github.com/zargony/activerecord_symbolize
23
+
24
+
25
+ == Install
26
+
27
+ Gem:
28
+
29
+ gem install nofxx-symbolize
30
+ config.gem "nofxx-symbolize", :lib => "symbolize"
31
+
32
+
33
+ Plugin:
34
+
35
+ ./script/plugin install git://github.com/nofxx/activerecord_symbolize.git
36
+
37
+
38
+ == Usage
39
+
40
+ Add "symbolize :attr_name" to your model class. You may also want to add
41
+ validates_inclusion_of to restrict the possible values (just like an enum).
42
+
43
+ class User < ActiveRecord::Base
44
+ symbolize :gender, :in => [:female, :male]
45
+ symbolize :so, :in => {
46
+ :linux => "Linux",
47
+ :mac => "Mac OS X"
48
+ }
49
+ symbolize :gui, , :in => [:gnome, :kde, :xfce], :allow_blank => true
50
+ symbolize :browser, :in => [:firefox, :opera], :i18n => false, :methods => true
51
+ end
52
+
53
+ === in/within
54
+
55
+ The values allowed on the enum field, you can provide a hash with
56
+ {:value => "Human text"} or an array of keys to be i18n eval (or not).
57
+ Booleans are also supported. See below.
58
+
59
+ allow_(blank|nil): What you expect.
60
+
61
+
62
+ === i18n
63
+
64
+ If you don`t provide a hash with values, it will try i18n:
65
+
66
+ activerecord:
67
+ attributes:
68
+ user:
69
+ enums:
70
+ gui:
71
+ gnome: Gnome Desktop Enviroment
72
+ kde: K Desktop Enviroment
73
+ xfce: XFCE4
74
+ gender:
75
+ female: Girl
76
+ male: Boy
77
+
78
+ You can skip i18n lookup with :i18n => false
79
+ symbolize :gender, :in => [:female, :male], :i18n => false
80
+
81
+
82
+ === method
83
+
84
+ If you provide the method option, some fancy boolean methods will be added:
85
+ In our User example, browser has this option, so you can do:
86
+
87
+ @user.firefox?
88
+ @user.opera?
89
+
90
+
91
+ === Booleans
92
+
93
+ Its possible to use boolean fields also.
94
+ symbolize :switch, :in => [true, false]
95
+
96
+ ...
97
+ switch:
98
+ "true": On
99
+ "false": Off
100
+ "nil": Unknown
101
+
102
+
103
+ == Examples
104
+
105
+ u = User.find_by_name('Anna') # => #<User Anna>
106
+ u.gender # => :female
107
+
108
+ u = User.find_by_gender(:male) # => #<User Bob>
109
+ u.gender # => :male
110
+
111
+ u = User.find(:all, :conditions => { :gender => :female })
112
+
113
+ u = User.new(:name => 'ET', :gender => :unknown)
114
+ u.save # => validation fails
115
+
116
+
117
+ == Examples Helpers
118
+
119
+ <% form_for @user do |f| %>
120
+ <%= f.radio_sym "gender" %>
121
+ <!-- Alphabetic order -->
122
+ <%= f.select_sym "so" %>
123
+ <!-- Fixed order -->
124
+ <%= f.select_sym "office" %>
125
+ <% end %>
126
+
127
+ output:
128
+
129
+ <form action="users/1" method="post">
130
+ <div style="margin:0;padding:0">...</div>
131
+ <label>Female <input id="user_gender_female" name="user[gender]" type="radio" value="female"></label>
132
+ <label>Male <input checked="checked" id="user_gender_male" name="user[gender]" type="radio" value="male" ></label>
133
+ <!-- Alphabetic order -->
134
+ <select id="user_so" name="post[so]">
135
+ <option value="linux" selected="selected">Linux</option>
136
+ <option value="mac">Mac OS X</option>
137
+ <option value="windows">Windows XP</option>
138
+ </select>
139
+ <!-- Fixed order -->
140
+ <select id="user_office" name="post[office]">
141
+ <option value="kde" selected="selected">Koffice</option>
142
+ <option value="ms">Microsoft Office</option>
143
+ <option value="open">Open Office</option>
144
+ </select>
145
+ </form>
146
+
147
+
148
+ == Notes
149
+
150
+ This fork:
151
+ http://github.com/nofxx/symbolize
152
+
153
+
154
+ Forked from:
155
+ http://github.com/nuxlli/activerecord_symbolize
156
+
157
+
158
+ Initial work:
159
+ I've been using this for quite some time and made it a rails plugin now. More
160
+ background iinformation can be found at
161
+ http://zargony.com/2007/09/07/symbolize-attribute-values-in-activerecord
162
+
163
+
164
+ Copyright (c) 2007-2008 Andreas Neuhaus, released under the MIT license
data/Rakefile ADDED
@@ -0,0 +1,54 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+ require 'spec/rake/spectask'
4
+
5
+ begin
6
+ require 'jeweler'
7
+ Jeweler::Tasks.new do |gem|
8
+ gem.name = "symbolize"
9
+ gem.summary = "ActiveRecord enums with i18n"
10
+ gem.description = "ActiveRecord enums with i18n"
11
+ gem.email = "x@nofxx.com"
12
+ gem.homepage = "http://github.com/nofxx/symbolize"
13
+ gem.authors = ["Marcos Piccinini"]
14
+ gem.add_development_dependency "rspec"
15
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
16
+ end
17
+ rescue LoadError
18
+ puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
19
+ end
20
+
21
+ Spec::Rake::SpecTask.new(:spec) do |spec|
22
+ spec.libs << 'lib' << 'spec'
23
+ spec.spec_files = FileList['spec/**/*_spec.rb']
24
+ end
25
+
26
+ Spec::Rake::SpecTask.new(:rcov) do |spec|
27
+ spec.libs << 'lib' << 'spec'
28
+ spec.pattern = 'spec/**/*_spec.rb'
29
+ spec.rcov = true
30
+ end
31
+ task :default => :spec
32
+
33
+ # desc 'Generate documentation for the activerecord_symbolize plugin.'
34
+ # Rake::RDocTask.new(:rdoc) do |rdoc|
35
+ # rdoc.rdoc_dir = 'rdoc'
36
+ # rdoc.title = 'ActiverecordSymbolize'
37
+ # rdoc.options << '--line-numbers' << '--inline-source'
38
+ # rdoc.rdoc_files.include('README')
39
+ # rdoc.rdoc_files.include('lib/**/*.rb')
40
+ # end
41
+ require 'rake/rdoctask'
42
+ Rake::RDocTask.new do |rdoc|
43
+ if File.exist?('VERSION.yml')
44
+ config = YAML.load(File.read('VERSION.yml'))
45
+ version = "#{config[:major]}.#{config[:minor]}.#{config[:patch]}"
46
+ else
47
+ version = ""
48
+ end
49
+
50
+ rdoc.rdoc_dir = 'rdoc'
51
+ rdoc.title = "symbolize #{version}"
52
+ rdoc.rdoc_files.include('README*')
53
+ rdoc.rdoc_files.include('lib/**/*.rb')
54
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 1.0.1
data/init.rb ADDED
@@ -0,0 +1 @@
1
+ require File.dirname(__FILE__) + "/rails/init"
data/lib/symbolize.rb ADDED
@@ -0,0 +1,156 @@
1
+ module Symbolize
2
+ def self.included base
3
+ base.extend(ClassMethods)
4
+ end
5
+
6
+ # Symbolize ActiveRecord attributes. Add
7
+ # symbolize :attr_name
8
+ # to your model class, to make an attribute return symbols instead of
9
+ # string values. Setting such an attribute will accept symbols as well
10
+ # as strings. In the database, the symbolized attribute should have
11
+ # the column-type :string.
12
+ #
13
+ # Example:
14
+ # class User < ActiveRecord::Base
15
+ # symbolize :gender, :in => [:female, :male]
16
+ # symbolize :so, :in => {
17
+ # :linux => "Linux",
18
+ # :mac => "Mac OS X"
19
+ # }
20
+ # symbolize :gui, , :in => [:gnome, :kde, :xfce], :allow_blank => true
21
+ # symbolize :browser, :in => [:firefox, :opera], :i18n => false
22
+ # end
23
+ #
24
+ # It will automattically lookup for i18n:
25
+ #
26
+ # activerecord:
27
+ # attributes:
28
+ # user:
29
+ # enums:
30
+ # gender:
31
+ # female: Girl
32
+ # male: Boy
33
+ #
34
+ # You can skip i18n lookup with :i18n => false
35
+ # symbolize :gender, :in => [:female, :male], :i18n => false
36
+ #
37
+ # Its possible to use boolean fields also.
38
+ # symbolize :switch, :in => [true, false]
39
+ #
40
+ # ...
41
+ # switch:
42
+ # "true": On
43
+ # "false": Off
44
+ # "nil": Unknown
45
+ #
46
+ module ClassMethods
47
+ # Specifies that values of the given attributes should be returned
48
+ # as symbols. The table column should be created of type string.
49
+ def symbolize *attr_names
50
+ configuration = {}
51
+ configuration.update(attr_names.extract_options!)
52
+
53
+ enum = configuration[:in] || configuration[:within]
54
+ i18n = configuration[:i18n].nil? && !enum.instance_of?(Hash) && enum ? true : configuration[:i18n]
55
+ methods = configuration[:methods]
56
+
57
+ unless enum.nil?
58
+ # Little monkeypatching, <1.8 Hashes aren't ordered.
59
+ hsh = if RUBY_VERSION > '1.9' || !defined?('ActiveSupport')
60
+ Hash
61
+ else
62
+ ActiveSupport::OrderedHash
63
+ end
64
+
65
+ attr_names.each do |attr_name|
66
+ attr_name = attr_name.to_s
67
+ if enum.instance_of?(Hash)
68
+ values = enum
69
+ else
70
+ if i18n
71
+ values = hsh[*enum.map { |v| [v, I18n.translate("activerecord.attributes.#{ActiveSupport::Inflector.underscore(self)}.enums.#{attr_name}.#{v}")] }.flatten]
72
+ else
73
+ values = hsh[*enum.map { |v| [v, (configuration[:capitalize] ? v.to_s.capitalize : v.to_s)] }.flatten]
74
+ end
75
+ end
76
+
77
+ # Get the values of :in
78
+ const = "#{attr_name}_values"
79
+ const_set const.upcase, values unless const_defined? const.upcase
80
+ # This one is a dropdown helper
81
+ class_eval "def self.get_#{const}; #{const.upcase}.map(&:reverse); end"
82
+
83
+ if methods
84
+ values.each do |value|
85
+ define_method("#{value[0]}?") do
86
+ self.send(attr_name) == value[0]
87
+ end
88
+ end
89
+ end
90
+ end
91
+
92
+ class_eval "validates_inclusion_of :#{attr_names.join(', :')}, configuration"
93
+ end
94
+
95
+ attr_names.each do |attr_name|
96
+ attr_name = attr_name.to_s
97
+ class_eval("def #{attr_name}; read_and_symbolize_attribute('#{attr_name}'); end")
98
+ class_eval("def #{attr_name}= (value); write_symbolized_attribute('#{attr_name}', value); end")
99
+ if i18n
100
+ class_eval("def #{attr_name}_text; read_i18n_attribute('#{attr_name}'); end")
101
+ elsif enum
102
+ class_eval("def #{attr_name}_text; #{attr_name.upcase}_VALUES[#{attr_name}]; end")
103
+ else
104
+ class_eval("def #{attr_name}_text; #{attr_name}.to_s; end")
105
+ end
106
+ end
107
+ end
108
+ end
109
+
110
+ # String becomes symbol, booleans string and nil nil.
111
+ def symbolize_attribute attr
112
+ case attr
113
+ when String then attr.empty? ? nil : attr.to_sym
114
+ when Symbol, TrueClass, FalseClass then attr
115
+ else nil
116
+ end
117
+ end
118
+
119
+ # Return an attribute's value as a symbol or nil
120
+ def read_and_symbolize_attribute attr_name
121
+ symbolize_attribute read_attribute(attr_name)
122
+ end
123
+
124
+ # Return an attribute's i18n
125
+ def read_i18n_attribute attr_name
126
+ I18n.translate("activerecord.attributes.#{ActiveSupport::Inflector.underscore(self.class)}.enums.#{attr_name}.#{read_attribute(attr_name)}") #.to_sym rescue nila
127
+ end
128
+
129
+ # Write a symbolized value. Watch out for booleans.
130
+ def write_symbolized_attribute attr_name, value
131
+ val = { "true" => true, "false" => false }[value]
132
+ val = symbolize_attribute(value) if val.nil?
133
+ write_attribute(attr_name, val)
134
+ end
135
+ end
136
+
137
+ # The Symbol class is extended by method quoted_id which returns a string.
138
+ # The idea behind this is, that symbols are converted to plain strings
139
+ # when being quoted by ActiveRecord::ConnectionAdapters::Quoting#quote.
140
+ # This makes it possible to work with symbolized attibutes in sql conditions.
141
+ # E.g. validates_uniqueness_of could not use :scope with a symbolized
142
+ # attribute, because AR quotes it to YAML:
143
+ # "... AND status = '--- :active\n'"
144
+ # Having support for quoted_id in Symbol, makes AR quoting symbols correctly:
145
+ # "... AND status = 'active'"
146
+ # NOTE: Normally quoted_id should be implemented as a singleton method
147
+ # only used on symbols returned by read_and_symbolize_attribute,
148
+ # but unfortunately this is not possible since Symbol is an immediate
149
+ # value and therefore does not support singleton methods.
150
+ class Symbol
151
+ def quoted_id
152
+ # A symbol can contain almost every character (even a backslash or an
153
+ # apostrophe), so make sure to properly quote the string value here.
154
+ "'#{ActiveRecord::Base.connection.quote_string(self.to_s)}'"
155
+ end
156
+ end
@@ -0,0 +1,60 @@
1
+ # Goes in lib/meta_tag_helper.rb
2
+ module ActionView
3
+ module Helpers
4
+ module FormHelper
5
+ # helper to create a select drop down list for the symbolize values
6
+ def select_sym(object, method, choices = nil, options = {}, html_options = {})
7
+
8
+ InstanceTag.new(object, method, self, options.delete(:object)).
9
+ to_select_sym_tag(choices, options, html_options)
10
+ end
11
+
12
+ def radio_sym(object, method, choices = nil, options = {})
13
+ InstanceTag.new(object, method, self, options.delete(:object)).
14
+ to_radio_sym_tag(choices, options)
15
+ end
16
+ end
17
+
18
+ class FormBuilder
19
+ def select_sym(method, choices = nil, options = {}, html_options = {})
20
+ @template.select_sym(@object_name, method, choices, options, html_options)
21
+ end
22
+
23
+ def radio_sym(method, choices = nil, options = {})
24
+ @template.radio_sym(@object_name, method, choices, options)
25
+ end
26
+ end
27
+
28
+ class InstanceTag
29
+ # Create a select tag and one option for each of the
30
+ # symbolize values.
31
+ def to_select_sym_tag(choices, options, html_options)
32
+ choices = symbolize_values(choices)
33
+ to_select_tag(choices, options, html_options)
34
+ end
35
+
36
+ def to_radio_sym_tag(choices, options)
37
+ choices = symbolize_values(choices)
38
+ raise ArgumentError, "No values for radio tag" unless choices
39
+ add_default_name_and_id(options)
40
+ v = value(object)
41
+ tag_text = ''
42
+ template = options.dup
43
+ template.delete('checked')
44
+ choices.each do |choice|
45
+ opts = template.dup
46
+ opts['checked'] = 'checked' if v and v == choice[1]
47
+ opts['id'] = "#{opts['id']}_#{choice[1]}"
48
+ tag_text << "<label>#{choice[0]}: "
49
+ tag_text << to_radio_button_tag(choice[1], opts)
50
+ tag_text << "</label>"
51
+ end
52
+ tag_text
53
+ end
54
+
55
+ def symbolize_values(choices)
56
+ choices.nil? ? object.class.send("get_#{@method_name}_values") : choices
57
+ end
58
+ end
59
+ end
60
+ end
data/rails/init.rb ADDED
@@ -0,0 +1,2 @@
1
+ require File.join(File.dirname(__FILE__), '..', "lib", "symbolize_helper")
2
+ ActiveRecord::Base.send(:include, Symbolize)
@@ -0,0 +1,17 @@
1
+ class CreateTestingStructure < ActiveRecord::Migration
2
+ def self.up
3
+ create_table :users do |t|
4
+ t.string :name, :so, :gui, :other, :status, :language
5
+ t.string :limited, :limit => 10
6
+ t.string :karma, :limit => 5
7
+ t.boolean :sex
8
+ end
9
+ create_table :user_skills do |t|
10
+ t.string :kind
11
+ end
12
+ end
13
+
14
+ def self.down
15
+ drop_table :users
16
+ end
17
+ end
@@ -0,0 +1,16 @@
1
+ pt:
2
+ activerecord:
3
+ attributes:
4
+ user:
5
+ enums:
6
+ language:
7
+ pt: Português
8
+ en: Inglês
9
+ sex:
10
+ "true": Feminino
11
+ "false": Masculino
12
+ user_skill:
13
+ enums:
14
+ kind:
15
+ magic: Mágica
16
+ agility: Agilidade
@@ -0,0 +1,22 @@
1
+ require 'rubygems'
2
+ require 'spec'
3
+
4
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
5
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
6
+ require 'active_record'
7
+ require 'action_controller'
8
+ require 'action_view'
9
+ require 'symbolize'
10
+ require File.join(File.dirname(__FILE__), '..', 'init')
11
+
12
+ ActiveRecord::Base.establish_connection(:adapter => 'sqlite3', :database => ':memory:')
13
+ require File.dirname(__FILE__) + "/db/create_testing_structure"
14
+ I18n.load_path += Dir[File.join(File.dirname(__FILE__), "locales", "*.{rb,yml}")]
15
+ I18n.default_locale = "pt"
16
+ CreateTestingStructure.migrate(:up)
17
+
18
+
19
+ Spec::Runner.configure do |config|
20
+
21
+
22
+ end
@@ -0,0 +1,252 @@
1
+ # -*- coding: utf-8 -*-
2
+ require File.dirname(__FILE__) + '/spec_helper'
3
+
4
+ #
5
+ # Test model
6
+ class User < ActiveRecord::Base
7
+ symbolize :other
8
+ symbolize :language, :in => [:pt, :en]
9
+ symbolize :sex, :in => [true, false]
10
+ symbolize :status , :in => [:active, :inactive], :i18n => false, :capitalize => true
11
+ symbolize :so, :allow_blank => true, :in => {
12
+ :linux => 'Linux',
13
+ :mac => 'Mac OS X',
14
+ :win => 'Videogame'
15
+ }
16
+ symbolize :gui, :allow_blank => true, :in => [:cocoa, :qt, :gtk], :i18n => false
17
+ symbolize :karma, :in => [:good, :bad, :ugly], :methods => true, :i18n => false, :allow_nil => true
18
+
19
+ end
20
+
21
+ class UserSkill < ActiveRecord::Base
22
+ symbolize :kind, :in => [:agility, :magic]
23
+ end
24
+
25
+ # Make with_scope public-usable for testing
26
+ class << ActiveRecord::Base
27
+ public :with_scope
28
+ end
29
+
30
+ # Test records
31
+ User.create(:name => 'Anna', :other => :fo, :status => :active , :so => :linux, :gui => :qt, :language => :pt, :sex => true)
32
+ User.create(:name => 'Bob' , :other => :bar,:status => :inactive, :so => :mac, :gui => :gtk, :language => :en, :sex => false)
33
+
34
+
35
+ describe "Symbolize" do
36
+
37
+
38
+ it "should respond to symbolize" do
39
+ ActiveRecord::Base.should respond_to :symbolize
40
+ end
41
+
42
+ describe "Instantiated" do
43
+ before(:each) do
44
+ @user = User.first
45
+ end
46
+
47
+ it "test_symbolize_string" do
48
+ @user.status = 'inactive'
49
+ @user.status.should eql(:inactive)
50
+ # @user.status_before_type_cast.should eql(:inactive)
51
+ # @user.read_attribute(:status).should eql('inactive')
52
+ end
53
+
54
+ it "test_symbolize_symbol" do
55
+ @user.status = :active
56
+ @user.status.should eql(:active)
57
+ @user.status_before_type_cast.should eql(:active)
58
+ # @user.read_attribute(:status).should eql('active')
59
+ end
60
+
61
+ it "should acts nice with numbers" do
62
+ @user.status = 43
63
+ @user.status.should be_nil
64
+ @user.status_before_type_cast.should be_nil
65
+ @user.read_attribute(:status).should be_nil
66
+ end
67
+
68
+ it "should acts nice with nil" do
69
+ @user.status = nil
70
+ @user.status.should be_nil
71
+ @user.status_before_type_cast.should be_nil
72
+ @user.read_attribute(:status).should be_nil
73
+ end
74
+
75
+ it "should acts nice with blank" do
76
+ @user.status = ""
77
+ @user.status.should be_nil
78
+ @user.status_before_type_cast.should be_nil
79
+ @user.read_attribute(:status).should be_nil
80
+ end
81
+
82
+ it "test_symbols_quoted_id" do
83
+ @user.status = :active
84
+ @user.status.quoted_id.should eql("'active'")
85
+ end
86
+
87
+ it "should not validates other" do
88
+ @user.other = nil
89
+ @user.should be_valid
90
+ @user.other = ""
91
+ @user.should be_valid
92
+ end
93
+
94
+ it "should get the correct values" do
95
+ User.get_status_values.should eql([["Active", :active],["Inactive", :inactive]])
96
+ User::STATUS_VALUES.should eql({:inactive=>"Inactive", :active=>"Active"})
97
+ end
98
+
99
+ it "test_symbolize_humanize" do
100
+ @user.status_text.should eql("Active")
101
+ end
102
+
103
+ it "should get the correct values" do
104
+ User.get_gui_values.should =~ [["cocoa", :cocoa], ["qt", :qt], ["gtk", :gtk]]
105
+ User::GUI_VALUES.should eql({:cocoa=>"cocoa", :qt=>"qt", :gtk=>"gtk"})
106
+ end
107
+
108
+ it "test_symbolize_humanize" do
109
+ @user.gui_text.should eql("qt")
110
+ end
111
+
112
+ it "should get the correct values" do
113
+ User.get_so_values.should =~ [["Linux", :linux], ["Mac OS X", :mac], ["Videogame", :win]]
114
+ User::SO_VALUES.should eql({:linux => "Linux", :mac => "Mac OS X", :win => "Videogame"})
115
+ end
116
+
117
+ it "test_symbolize_humanize" do
118
+ @user.so_text.should eql("Linux")
119
+ end
120
+
121
+ it "test_symbolize_humanize" do
122
+ @user.so = :mac
123
+ @user.so_text.should eql("Mac OS X")
124
+ end
125
+
126
+ it "should stringify" do
127
+ @user.other_text.should eql("fo")
128
+ @user.other = :foo
129
+ @user.other_text.should eql("foo")
130
+ end
131
+
132
+ it "should validate status" do
133
+ @user.status = nil
134
+ @user.should_not be_valid
135
+ @user.should have(1).errors
136
+ end
137
+
138
+ it "should not validate so" do
139
+ @user.so = nil
140
+ @user.should be_valid
141
+ end
142
+
143
+ it "test_symbols_with_weird_chars_quoted_id" do
144
+ @user.status = :"weird'; chars"
145
+ @user.status_before_type_cast.should eql(:"weird'; chars")
146
+ # assert_equal "weird'; chars", @user.read_attribute(:status)
147
+ # assert_equal "'weird''; chars'", @user.status.quoted_id
148
+ end
149
+
150
+ it "test_symbolized_finder" do
151
+ User.find(:all, :conditions => { :status => :inactive }).map(&:name).should eql(['Bob'])
152
+ User.find_all_by_status(:inactive).map(&:name).should eql(['Bob'])
153
+ end
154
+
155
+ it "test_symbolized_with_scope" do
156
+ User.with_scope(:find => { :conditions => { :status => :inactive }}) do
157
+ User.find(:all).map(&:name).should eql(['Bob'])
158
+ end
159
+ end
160
+
161
+ describe "View helpers" do
162
+ include ActionView::Helpers::FormHelper
163
+ include ActionView::Helpers::FormOptionsHelper
164
+
165
+ before(:each) do
166
+ @options_status = [['Active', :active], ['Inactive', :inactive]]
167
+ @options_gui = [["cocoa", :cocoa], ["qt", :qt], ["gtk", :gtk]]
168
+ @options_so = [["Linux", :linux] , ["Mac OS X", :mac], ["Videogame", :win]]
169
+ end
170
+
171
+ it "test_helper_select_sym" do
172
+ @user.status = :inactive
173
+ output = "<select id=\"user_status\" name=\"user[status]\">#{options_for_select(@options_status, @user.status)}</select>"
174
+ output.should eql(select_sym("user", "status", nil))
175
+
176
+
177
+ output = "<select id=\"user_status\" name=\"user[status]\">#{options_for_select(@options_status, @user.status)}</select>"
178
+ output.should eql(select_sym("user", "status", nil))
179
+ end
180
+
181
+ def test_helper_select_sym_order
182
+ output_so = "<select id=\"user_so\" name=\"user[so]\">#{options_for_select(@options_so, @user.so)}</select>"
183
+ output_office = "<select id=\"user_office\" name=\"user[office]\">#{options_for_select(@options_office, @user.office)}</select>"
184
+
185
+ assert_equal output_so, select_sym("user", "so", nil)
186
+ assert_equal output_office, select_sym("user", "office", nil)
187
+ end
188
+
189
+ def test_helper_radio_sym
190
+ output = radio_sym("user", "status", nil)
191
+ assert_equal("<label>Active: <input checked=\"checked\" id=\"user_status_active\" name=\"user[status]\" type=\"radio\" value=\"active\" /></label><label>Inactive: <input id=\"user_status_inactive\" name=\"user[status]\" type=\"radio\" value=\"inactive\" /></label>", output)
192
+ end
193
+
194
+ end
195
+
196
+ describe "i18n" do
197
+
198
+ it "should test i18n ones" do
199
+ @user.language_text.should eql("Português")
200
+ end
201
+
202
+ it "should get the correct values" do
203
+ User.get_language_values.should =~ [["Português", :pt], ["Inglês", :en]]
204
+ end
205
+
206
+ it "should get the correct values" do
207
+ User::LANGUAGE_VALUES.should eql({:pt => "Português", :en => "Inglês"})
208
+ end
209
+
210
+ it "should test boolean" do
211
+ @user.sex_text.should eql("Feminino")
212
+ end
213
+
214
+ it "should get the correct values" do
215
+ User.get_sex_values.should eql([["Feminino", true],["Masculino", false]])
216
+ end
217
+
218
+ it "should get the correct values" do
219
+ User::SEX_VALUES.should eql({false=>"Masculino", true=>"Feminino"})
220
+ end
221
+
222
+ it "should translate a multiword class" do
223
+ @skill = UserSkill.create(:kind => :magic)
224
+ @skill.kind_text.should eql("Mágica")
225
+ end
226
+
227
+ end
228
+
229
+ describe "Methods" do
230
+
231
+ it "should play nice with other stuff" do
232
+ @user.karma.should be_nil
233
+ User::KARMA_VALUES.should eql({:bad => "bad", :ugly => "ugly", :good => "good"})
234
+ end
235
+
236
+ it "should provide a boolean method" do
237
+ @user.should_not be_good
238
+ @user.karma = :ugly
239
+ @user.should be_ugly
240
+ end
241
+
242
+ it "should work" do
243
+ @user.karma = "good"
244
+ @user.should be_good
245
+ @user.should_not be_bad
246
+ end
247
+
248
+ end
249
+
250
+ end
251
+
252
+ end
data/symbolize.gemspec ADDED
@@ -0,0 +1,58 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run `rake gemspec`
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{symbolize}
8
+ s.version = "1.0.1"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Marcos Piccinini"]
12
+ s.date = %q{2009-08-26}
13
+ s.description = %q{ActiveRecord enums with i18n}
14
+ s.email = %q{x@nofxx.com}
15
+ s.extra_rdoc_files = [
16
+ "README"
17
+ ]
18
+ s.files = [
19
+ ".document",
20
+ ".gitignore",
21
+ "MIT-LICENSE",
22
+ "README",
23
+ "Rakefile",
24
+ "VERSION",
25
+ "init.rb",
26
+ "lib/symbolize.rb",
27
+ "lib/symbolize_helper.rb",
28
+ "rails/init.rb",
29
+ "spec/db/create_testing_structure.rb",
30
+ "spec/locales/pt.yml",
31
+ "spec/spec_helper.rb",
32
+ "spec/symbolize_spec.rb",
33
+ "symbolize.gemspec"
34
+ ]
35
+ s.homepage = %q{http://github.com/nofxx/symbolize}
36
+ s.rdoc_options = ["--charset=UTF-8"]
37
+ s.require_paths = ["lib"]
38
+ s.rubygems_version = %q{1.3.5}
39
+ s.summary = %q{ActiveRecord enums with i18n}
40
+ s.test_files = [
41
+ "spec/db/create_testing_structure.rb",
42
+ "spec/spec_helper.rb",
43
+ "spec/symbolize_spec.rb"
44
+ ]
45
+
46
+ if s.respond_to? :specification_version then
47
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
48
+ s.specification_version = 3
49
+
50
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
51
+ s.add_development_dependency(%q<rspec>, [">= 0"])
52
+ else
53
+ s.add_dependency(%q<rspec>, [">= 0"])
54
+ end
55
+ else
56
+ s.add_dependency(%q<rspec>, [">= 0"])
57
+ end
58
+ end
metadata ADDED
@@ -0,0 +1,78 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: nofxx-symbolize
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Marcos Piccinini
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-08-26 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: rspec
17
+ type: :development
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: "0"
24
+ version:
25
+ description: ActiveRecord enums with i18n
26
+ email: x@nofxx.com
27
+ executables: []
28
+
29
+ extensions: []
30
+
31
+ extra_rdoc_files:
32
+ - README
33
+ files:
34
+ - .document
35
+ - .gitignore
36
+ - MIT-LICENSE
37
+ - README
38
+ - Rakefile
39
+ - VERSION
40
+ - init.rb
41
+ - lib/symbolize.rb
42
+ - lib/symbolize_helper.rb
43
+ - rails/init.rb
44
+ - spec/db/create_testing_structure.rb
45
+ - spec/locales/pt.yml
46
+ - spec/spec_helper.rb
47
+ - spec/symbolize_spec.rb
48
+ - symbolize.gemspec
49
+ has_rdoc: false
50
+ homepage: http://github.com/nofxx/symbolize
51
+ post_install_message:
52
+ rdoc_options:
53
+ - --charset=UTF-8
54
+ require_paths:
55
+ - lib
56
+ required_ruby_version: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ version: "0"
61
+ version:
62
+ required_rubygems_version: !ruby/object:Gem::Requirement
63
+ requirements:
64
+ - - ">="
65
+ - !ruby/object:Gem::Version
66
+ version: "0"
67
+ version:
68
+ requirements: []
69
+
70
+ rubyforge_project:
71
+ rubygems_version: 1.2.0
72
+ signing_key:
73
+ specification_version: 3
74
+ summary: ActiveRecord enums with i18n
75
+ test_files:
76
+ - spec/db/create_testing_structure.rb
77
+ - spec/spec_helper.rb
78
+ - spec/symbolize_spec.rb