code-box 0.3.0 → 0.3.1
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.
- data/Gemfile.lock +1 -1
- data/README.md +92 -9
- data/lib/code-box/acts_as_code.rb +1 -1
- data/lib/code-box/version.rb +1 -1
- metadata +3 -3
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -67,6 +67,8 @@ Example
|
|
67
67
|
code_attribute :nationality, :lookup_type => :lookup
|
68
68
|
end
|
69
69
|
|
70
|
+
# Note: Below class is a plain sample implementation. Code objects can be built easier with
|
71
|
+
# 'ActsAsCode' include (see below)
|
70
72
|
class Code::Nationality
|
71
73
|
attr_accessor :code, :name
|
72
74
|
|
@@ -79,7 +81,7 @@ Example
|
|
79
81
|
The include will create the following method in Person:
|
80
82
|
|
81
83
|
`#nationality` Will return the nationality object looked up using the method '.for_code' on the code class.
|
82
|
-
Translation then can be done within this class with the first method described above.
|
84
|
+
Translation then can be done within this class with the first method described above ('acts_as_code' facilitates this as well).
|
83
85
|
|
84
86
|
|
85
87
|
|
@@ -90,9 +92,11 @@ The code value is interpreted as a foreign key on an associated AR Code object.
|
|
90
92
|
class Person < ActiveRecord::Base
|
91
93
|
include CodeBox::CodeAttribute
|
92
94
|
|
93
|
-
code_attribute :nationality, :lookup_type => :
|
95
|
+
code_attribute :nationality, :lookup_type => :associated
|
94
96
|
end
|
95
97
|
|
98
|
+
# Note: Below class is a plain sample implementation. Code objects can be built easier with
|
99
|
+
# 'ActsAsCode' include (see below)
|
96
100
|
class Code::Nationality < ActiveRecord::Base
|
97
101
|
# has attribute 'code' of type string
|
98
102
|
end
|
@@ -106,19 +110,98 @@ The include and code specification will create the following methods in Person:
|
|
106
110
|
:foreign_key => :nationality_code,
|
107
111
|
:primary_key => :code
|
108
112
|
|
113
|
+
Above options can be overwritten in the 'code_attribute' option.
|
109
114
|
|
110
|
-
## Configuration details
|
111
115
|
|
112
|
-
### Lookup through I18n
|
113
|
-
... to be completed
|
114
116
|
|
115
|
-
###
|
116
|
-
... to be completed
|
117
|
+
### Defining code classes (acts_as_code)
|
117
118
|
|
118
|
-
|
119
|
-
... to be completed
|
119
|
+
As describe above code_attributes can reference code objects if the `code_attribute` is of type `:associated` or `:lookup`.
|
120
120
|
|
121
|
+
Making an code object `acts_as_code` provides the following features:
|
121
122
|
|
123
|
+
* `#translated_code(locale=I18n.locale, *other_locale_options)
|
124
|
+
Translates the code stored in `code`
|
125
|
+
|
126
|
+
* `#translated_code(locale=I18n.locale, *other_locale_options)`
|
127
|
+
Translates the code stored in `code`
|
128
|
+
|
129
|
+
* `.translate_code(code, *options)`
|
130
|
+
Translates a single code if `code` is a code, an array of codes of `code` is an array.
|
131
|
+
If code is an array the option :build => :zip can be used to build a select option capable array (e.g `[['Switzerland', 'SUI'],['Germany', 'GER'],['Denmark', 'DEN']]`)
|
132
|
+
|
133
|
+
* `.for_code(code)
|
134
|
+
Answers the code object for the given code (fetched from cache)
|
135
|
+
|
136
|
+
* `.clear_code_cache`
|
137
|
+
Clears the cache so its build up on need from all codes from scratch
|
138
|
+
|
139
|
+
|
140
|
+
__Note:__ The code name can be configures using the `:code_attribute` option.
|
141
|
+
`:code_attribute => :iso_code` leads to methods like #translate_iso_code etc.
|
142
|
+
|
143
|
+
|
144
|
+
#### Plain old ruby object codes (:poro)
|
145
|
+
|
146
|
+
Assuming we have a simple ruby class with default code attribute 'code' we can defined such a class like
|
147
|
+
|
148
|
+
class Codes::MySpecificCode
|
149
|
+
include CodeBox::ActsAsCode
|
150
|
+
|
151
|
+
# Above include cretes the following:
|
152
|
+
#
|
153
|
+
# attr_accessor :code
|
154
|
+
#
|
155
|
+
# def initialize(code)
|
156
|
+
# @code = code
|
157
|
+
# end
|
158
|
+
#
|
159
|
+
# def self.all
|
160
|
+
# raise "Sublass responsibility. You should implement '.all' returning all codes"
|
161
|
+
# end
|
162
|
+
|
163
|
+
# @return [Array] List if all code objects (instances of this)
|
164
|
+
def self.all
|
165
|
+
# you need to implement this
|
166
|
+
end
|
167
|
+
|
168
|
+
end
|
169
|
+
|
170
|
+
Configuration options are:
|
171
|
+
|
172
|
+
:type => :poro #(default, other :active_record)
|
173
|
+
:code_attribute => :code #(default) or any other name as symbol
|
174
|
+
|
175
|
+
|
176
|
+
|
177
|
+
#### ActiveRecod code objects (:active_record)
|
178
|
+
|
179
|
+
Assuming we have an ActiveRecod code class with `code_attribute :code` we can defined such a class like
|
180
|
+
|
181
|
+
class Codes::MySpecificCode < ActiveRecord::Base
|
182
|
+
include CodeBox::ActsAsCode[:type => :active_record]
|
183
|
+
|
184
|
+
# Above include cretes the following:
|
185
|
+
#
|
186
|
+
# validates_presence_of :code
|
187
|
+
# validates_uniqueness_of :code
|
188
|
+
#
|
189
|
+
# default_scope order('code')
|
190
|
+
|
191
|
+
end
|
192
|
+
|
193
|
+
Configuration options are:
|
194
|
+
|
195
|
+
:type => :active_record # other :poro(default)
|
196
|
+
:code_attribute => :code # (default) or any other name as symbol
|
197
|
+
:polymorphic => false # (default). If `true` the uniqueness validation is scope by the attribute `type`
|
198
|
+
:uniqueness_case_sensitive => true # (default). If `false` the the uniqueness validation is case insensitive
|
199
|
+
:position_attr => :position # If present, the order when fetching the codes is done with this expression (default scope - means - if you want to omit the order used `unscoped` on any AR operation).
|
200
|
+
|
201
|
+
|
202
|
+
|
203
|
+
### Examples
|
204
|
+
TO BE DONE…
|
122
205
|
|
123
206
|
## Contributing
|
124
207
|
|
data/lib/code-box/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: code-box
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.3.
|
4
|
+
version: 0.3.1
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -119,7 +119,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
119
119
|
version: '0'
|
120
120
|
segments:
|
121
121
|
- 0
|
122
|
-
hash: -
|
122
|
+
hash: -268048192927979465
|
123
123
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
124
124
|
none: false
|
125
125
|
requirements:
|
@@ -128,7 +128,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
128
128
|
version: '0'
|
129
129
|
segments:
|
130
130
|
- 0
|
131
|
-
hash: -
|
131
|
+
hash: -268048192927979465
|
132
132
|
requirements: []
|
133
133
|
rubyforge_project:
|
134
134
|
rubygems_version: 1.8.21
|