select_options 1.0.0 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -10,15 +10,23 @@ In Gemfile:
10
10
 
11
11
  = Usage example
12
12
 
13
- For example with @user:
13
+ For example, you have a model User with the field role and a constant ROLES:
14
14
  class User < ActiveRecord::Base
15
15
  ROLES = %w(admin user)
16
16
  ...
17
17
  end
18
18
 
19
- in the template:
19
+ in the template you have just write:
20
20
  <%= f.select_with_options :role %>
21
21
 
22
+ In case if you have the constant, named differt from the field name, like this:
23
+ class Shape < ActiveRecord::Base
24
+ DIMENTION_VALUES = [2, 3]
25
+ ...
26
+ end
27
+ in the template you should use option :source
28
+ <%= f.select_with_options :dimention, :source => Shape::DIMENTION_VALUES %>
29
+
22
30
  Translations in en.yml for an ActiveRecord child look like this:
23
31
  en:
24
32
  activerecord:
@@ -27,6 +35,13 @@ Translations in en.yml for an ActiveRecord child look like this:
27
35
  role:
28
36
  admin: Administrator
29
37
  user: User
38
+ or like this:
39
+ en:
40
+ activerecord:
41
+ attributes:
42
+ shape:
43
+ dimention_2: 2D
44
+ dimention_3: 3D
30
45
 
31
46
  This code will generate translated collection of options for select tag:
32
47
  <select name="user[role]">
@@ -36,4 +51,4 @@ This code will generate translated collection of options for select tag:
36
51
 
37
52
  == Copyright
38
53
 
39
- Copyright (c) 2011 Dmitriy Vorotilin, Evrone.com. See LICENSE.txt for further details.
54
+ Copyright (c) 2011 Dmitriy Vorotilin, {Evrone.com}[http://evrone.com]. See LICENSE.txt for further details.
@@ -1,6 +1,6 @@
1
1
  module ActiveModel
2
2
  module Translation
3
- # Transforms composite attribute names into a more human format.
3
+ # Transforms composite attribute names into a humanized format.
4
4
  # Translations in en.yml for an ActiveRecord child looks like this:
5
5
  # en:
6
6
  # activerecord:
@@ -1,14 +1,17 @@
1
1
  module ActiveRecord
2
2
  class Base
3
- def self.attribute_options(name)
3
+ def self.attribute_options(name, source_constant = nil)
4
+ constant = source_constant.presence || constant_for_attribute(name)
5
+ constant.map { |value| [human_composite_attribute_name(name, value), value] } if constant.present?
6
+ end
7
+
8
+ def self.constant_for_attribute(name)
4
9
  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
10
+ const_get(constant_name) if const_defined?(constant_name)
8
11
  end
9
12
 
10
- def attribute_options(name)
11
- self.class.attribute_options(name)
13
+ def attribute_options(name, source_constant = nil)
14
+ self.class.attribute_options(name, source_constant)
12
15
  end
13
16
 
14
17
  def self.composite_attribute_name?(name)
@@ -14,6 +14,7 @@ module SelectOptions
14
14
  #
15
15
  # This helper takes parameters like a rails select.
16
16
  def select_with_options(method, options = {}, html_options = {})
17
- select(method, @object.attribute_options(method), options, html_options)
17
+ const_name = options.delete(:source)
18
+ select(method, @object.attribute_options(method, const_name), options, html_options)
18
19
  end
19
20
  end
metadata CHANGED
@@ -1,79 +1,56 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: select_options
3
- version: !ruby/object:Gem::Version
4
- hash: 23
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.1.0
5
5
  prerelease:
6
- segments:
7
- - 1
8
- - 0
9
- - 0
10
- version: 1.0.0
11
6
  platform: ruby
12
- authors:
7
+ authors:
13
8
  - Dmitriy Vorotilin
14
9
  autorequire:
15
10
  bindir: bin
16
11
  cert_chain: []
17
-
18
- date: 2011-10-04 00:00:00 Z
19
- dependencies:
20
- - !ruby/object:Gem::Dependency
12
+ date: 2011-10-04 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
21
15
  name: activemodel
22
- prerelease: false
23
- requirement: &id001 !ruby/object:Gem::Requirement
16
+ requirement: &2176981380 !ruby/object:Gem::Requirement
24
17
  none: false
25
- requirements:
26
- - - ">="
27
- - !ruby/object:Gem::Version
28
- hash: 7
29
- segments:
30
- - 3
31
- - 0
32
- - 0
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
33
21
  version: 3.0.0
34
22
  type: :runtime
35
- version_requirements: *id001
36
- - !ruby/object:Gem::Dependency
37
- name: activerecord
38
23
  prerelease: false
39
- requirement: &id002 !ruby/object:Gem::Requirement
24
+ version_requirements: *2176981380
25
+ - !ruby/object:Gem::Dependency
26
+ name: activerecord
27
+ requirement: &2176980100 !ruby/object:Gem::Requirement
40
28
  none: false
41
- requirements:
42
- - - ">="
43
- - !ruby/object:Gem::Version
44
- hash: 7
45
- segments:
46
- - 3
47
- - 0
48
- - 0
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
49
32
  version: 3.0.0
50
33
  type: :runtime
51
- version_requirements: *id002
52
- - !ruby/object:Gem::Dependency
53
- name: actionpack
54
34
  prerelease: false
55
- requirement: &id003 !ruby/object:Gem::Requirement
35
+ version_requirements: *2176980100
36
+ - !ruby/object:Gem::Dependency
37
+ name: actionpack
38
+ requirement: &2176978980 !ruby/object:Gem::Requirement
56
39
  none: false
57
- requirements:
58
- - - ">="
59
- - !ruby/object:Gem::Version
60
- hash: 7
61
- segments:
62
- - 3
63
- - 0
64
- - 0
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
65
43
  version: 3.0.0
66
44
  type: :runtime
67
- version_requirements: *id003
68
- description: Provides options for rails form_for select helper. It uses some conventions for your models.
45
+ prerelease: false
46
+ version_requirements: *2176978980
47
+ description: Provides options for rails form_for select helper. It uses some conventions
48
+ for your models.
69
49
  email: mydeeptown@gmail.com
70
50
  executables: []
71
-
72
51
  extensions: []
73
-
74
52
  extra_rdoc_files: []
75
-
76
- files:
53
+ files:
77
54
  - Gemfile
78
55
  - Gemfile.lock
79
56
  - README.rdoc
@@ -85,36 +62,26 @@ files:
85
62
  - lib/select_options/active_model/translation.rb
86
63
  homepage: https://github.com/route/select_options
87
64
  licenses: []
88
-
89
65
  post_install_message:
90
66
  rdoc_options: []
91
-
92
- require_paths:
67
+ require_paths:
93
68
  - lib
94
- required_ruby_version: !ruby/object:Gem::Requirement
69
+ required_ruby_version: !ruby/object:Gem::Requirement
95
70
  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
71
+ requirements:
72
+ - - ! '>='
73
+ - !ruby/object:Gem::Version
74
+ version: '0'
75
+ required_rubygems_version: !ruby/object:Gem::Requirement
104
76
  none: false
105
- requirements:
106
- - - ">="
107
- - !ruby/object:Gem::Version
108
- hash: 3
109
- segments:
110
- - 0
111
- version: "0"
77
+ requirements:
78
+ - - ! '>='
79
+ - !ruby/object:Gem::Version
80
+ version: '0'
112
81
  requirements: []
113
-
114
82
  rubyforge_project:
115
- rubygems_version: 1.8.6
83
+ rubygems_version: 1.8.10
116
84
  signing_key:
117
85
  specification_version: 3
118
86
  summary: select helper with options for rails form_for.
119
87
  test_files: []
120
-