select_options 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ source :rubygems
2
+
3
+ gem 'activemodel', '>= 3.0.0'
4
+ gem 'activerecord', '>= 3.0.0'
5
+ gem 'actionpack', '>= 3.0.0'
data/Gemfile.lock ADDED
@@ -0,0 +1,52 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ actionpack (3.1.1)
5
+ activemodel (= 3.1.1)
6
+ activesupport (= 3.1.1)
7
+ builder (~> 3.0.0)
8
+ erubis (~> 2.7.0)
9
+ i18n (~> 0.6)
10
+ rack (~> 1.3.2)
11
+ rack-cache (~> 1.1)
12
+ rack-mount (~> 0.8.2)
13
+ rack-test (~> 0.6.1)
14
+ sprockets (~> 2.0.2)
15
+ activemodel (3.1.1)
16
+ activesupport (= 3.1.1)
17
+ builder (~> 3.0.0)
18
+ i18n (~> 0.6)
19
+ activerecord (3.1.1)
20
+ activemodel (= 3.1.1)
21
+ activesupport (= 3.1.1)
22
+ arel (~> 2.2.1)
23
+ tzinfo (~> 0.3.29)
24
+ activesupport (3.1.1)
25
+ multi_json (~> 1.0)
26
+ arel (2.2.1)
27
+ builder (3.0.0)
28
+ erubis (2.7.0)
29
+ hike (1.2.1)
30
+ i18n (0.6.0)
31
+ multi_json (1.0.3)
32
+ rack (1.3.4)
33
+ rack-cache (1.1)
34
+ rack (>= 0.4)
35
+ rack-mount (0.8.3)
36
+ rack (>= 1.0.0)
37
+ rack-test (0.6.1)
38
+ rack (>= 1.0)
39
+ sprockets (2.0.2)
40
+ hike (~> 1.2)
41
+ rack (~> 1.0)
42
+ tilt (!= 1.3.0, ~> 1.1)
43
+ tilt (1.3.3)
44
+ tzinfo (0.3.30)
45
+
46
+ PLATFORMS
47
+ ruby
48
+
49
+ DEPENDENCIES
50
+ actionpack (>= 3.0.0)
51
+ activemodel (>= 3.0.0)
52
+ activerecord (>= 3.0.0)
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 Dmitriy Vorotilin
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,39 @@
1
+ = Select Options
2
+
3
+ This gem creates a select tag and a series of contained option tags for the provided object and method.
4
+ Helper takes options from model's constant and translates it according to the translations file (`composite_attributes` section).
5
+
6
+ = Installation
7
+
8
+ In Gemfile:
9
+ gem 'select_options'
10
+
11
+ = Usage example
12
+
13
+ For example with @user:
14
+ class User < ActiveRecord::Base
15
+ ROLES = %w(admin user)
16
+ ...
17
+ end
18
+
19
+ in the template:
20
+ <%= f.select_with_options :role %>
21
+
22
+ Translations in en.yml for an ActiveRecord child look like this:
23
+ en:
24
+ activerecord:
25
+ composite_attributes:
26
+ user:
27
+ role:
28
+ admin: Administrator
29
+ user: User
30
+
31
+ This code will generate translated collection of options for select tag:
32
+ <select name="user[role]">
33
+ <option value="admin" selected="selected">Administrator</option>
34
+ <option value="user">User</option>
35
+ </select>
36
+
37
+ == Copyright
38
+
39
+ Copyright (c) 2011 Dmitriy Vorotilin, Evrone.com. See LICENSE.txt for further details.
data/Rakefile ADDED
@@ -0,0 +1,7 @@
1
+ require 'rake/testtask'
2
+
3
+ Rake::TestTask.new do |t|
4
+ t.libs << 'test'
5
+ end
6
+
7
+ task :default => :test
@@ -0,0 +1,6 @@
1
+ require "select_options/active_model/translation"
2
+ require "select_options/active_record/base"
3
+ require "action_controller"
4
+ require "select_options/select_options"
5
+
6
+ ActionView::Helpers::FormBuilder.send :include, SelectOptions
@@ -0,0 +1,30 @@
1
+ module ActiveModel
2
+ module Translation
3
+ # Transforms composite attribute names into a more human format.
4
+ # Translations in en.yml for an ActiveRecord child looks like this:
5
+ # en:
6
+ # activerecord:
7
+ # composite_attributes:
8
+ # user:
9
+ # role:
10
+ # admin: Administrator
11
+ # user: User
12
+ #
13
+ # User.human_composite_attribute_name("role", "user") # => "User"
14
+ # or
15
+ # en:
16
+ # activerecord:
17
+ # attributes:
18
+ # user:
19
+ # role_admin: Administrator
20
+ # role_user: User
21
+ #
22
+ # User.human_composite_attribute_name("role", "admin") # => "Administrator"
23
+ # Specify +options+ with additional translating options.
24
+ def human_composite_attribute_name(attribute, value, options = {})
25
+ return "" unless value.present?
26
+ default = :"#{i18n_scope}.composite_attributes.#{model_name.i18n_key}.#{attribute}.#{value}"
27
+ human_attribute_name "#{attribute}_#{value}", options.reverse_merge(:default => default)
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,22 @@
1
+ module ActiveRecord
2
+ class Base
3
+ def self.attribute_options(name)
4
+ constant_name = name.to_s.pluralize.upcase
5
+ if const_defined?(constant_name)
6
+ const_get(constant_name).map { |value| [human_composite_attribute_name(name, value), value] }
7
+ end
8
+ end
9
+
10
+ def attribute_options(name)
11
+ self.class.attribute_options(name)
12
+ end
13
+
14
+ def self.composite_attribute_name?(name)
15
+ attribute_options(name).present?
16
+ end
17
+
18
+ def composite_attribute_name?(name)
19
+ self.class.composite_attribute_name?(name)
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,19 @@
1
+ module SelectOptions
2
+ # Create a select tag and a series of contained option tags for the provided object and method.
3
+ # Helper would take options from model's constant and translations to them from translations file.
4
+ # For example with @user:
5
+ # class User < ActiveRecord::Base
6
+ # ROLES = %w(admin user)
7
+ # ...
8
+ # end
9
+ #
10
+ # in the template:
11
+ # f.select_with_options :role
12
+ # is the same that:
13
+ # f.select :role, User::ROLES.map { |value| [translation_for_this_value, value] }
14
+ #
15
+ # This helper takes parameters like a rails select.
16
+ def select_with_options(method, options = {}, html_options = {})
17
+ select(method, @object.attribute_options(method), options, html_options)
18
+ end
19
+ end
metadata ADDED
@@ -0,0 +1,120 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: select_options
3
+ version: !ruby/object:Gem::Version
4
+ hash: 23
5
+ prerelease:
6
+ segments:
7
+ - 1
8
+ - 0
9
+ - 0
10
+ version: 1.0.0
11
+ platform: ruby
12
+ authors:
13
+ - Dmitriy Vorotilin
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-10-04 00:00:00 Z
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: activemodel
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ hash: 7
29
+ segments:
30
+ - 3
31
+ - 0
32
+ - 0
33
+ version: 3.0.0
34
+ type: :runtime
35
+ version_requirements: *id001
36
+ - !ruby/object:Gem::Dependency
37
+ name: activerecord
38
+ prerelease: false
39
+ requirement: &id002 !ruby/object:Gem::Requirement
40
+ none: false
41
+ requirements:
42
+ - - ">="
43
+ - !ruby/object:Gem::Version
44
+ hash: 7
45
+ segments:
46
+ - 3
47
+ - 0
48
+ - 0
49
+ version: 3.0.0
50
+ type: :runtime
51
+ version_requirements: *id002
52
+ - !ruby/object:Gem::Dependency
53
+ name: actionpack
54
+ prerelease: false
55
+ requirement: &id003 !ruby/object:Gem::Requirement
56
+ none: false
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ hash: 7
61
+ segments:
62
+ - 3
63
+ - 0
64
+ - 0
65
+ version: 3.0.0
66
+ type: :runtime
67
+ version_requirements: *id003
68
+ description: Provides options for rails form_for select helper. It uses some conventions for your models.
69
+ email: mydeeptown@gmail.com
70
+ executables: []
71
+
72
+ extensions: []
73
+
74
+ extra_rdoc_files: []
75
+
76
+ files:
77
+ - Gemfile
78
+ - Gemfile.lock
79
+ - README.rdoc
80
+ - LICENSE.txt
81
+ - Rakefile
82
+ - lib/select_options.rb
83
+ - lib/select_options/select_options.rb
84
+ - lib/select_options/active_record/base.rb
85
+ - lib/select_options/active_model/translation.rb
86
+ homepage: https://github.com/route/select_options
87
+ licenses: []
88
+
89
+ post_install_message:
90
+ rdoc_options: []
91
+
92
+ require_paths:
93
+ - lib
94
+ required_ruby_version: !ruby/object:Gem::Requirement
95
+ none: false
96
+ requirements:
97
+ - - ">="
98
+ - !ruby/object:Gem::Version
99
+ hash: 3
100
+ segments:
101
+ - 0
102
+ version: "0"
103
+ required_rubygems_version: !ruby/object:Gem::Requirement
104
+ none: false
105
+ requirements:
106
+ - - ">="
107
+ - !ruby/object:Gem::Version
108
+ hash: 3
109
+ segments:
110
+ - 0
111
+ version: "0"
112
+ requirements: []
113
+
114
+ rubyforge_project:
115
+ rubygems_version: 1.8.6
116
+ signing_key:
117
+ specification_version: 3
118
+ summary: select helper with options for rails form_for.
119
+ test_files: []
120
+