rails_dictionary 0.2.7 → 0.3.pre.rc1
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.
- checksums.yaml +5 -5
- data/.gitignore +3 -5
- data/CHANGELOG +4 -2
- data/Gemfile +3 -5
- data/{README.rdoc → README.v2.0.rdoc} +2 -5
- data/Rakefile +3 -2
- data/Readme.md +10 -0
- data/lib/rails_dictionary/models/active_record_extension.rb +38 -0
- data/lib/rails_dictionary/models/acts_as_dict_consumer.rb +77 -0
- data/lib/rails_dictionary/models/acts_as_dictionary.rb +27 -0
- data/lib/rails_dictionary/version.rb +1 -1
- data/lib/rails_dictionary.rb +28 -8
- data/lib/tasks/dicts.rake +11 -9
- data/rails_dictionary.gemspec +4 -4
- data/test/acts_as_consumer_test.rb +44 -0
- data/test/fake_app.rb +40 -0
- data/test/lookup_test.rb +39 -0
- data/test/rails_dictionary_test.rb +81 -0
- data/test/test_helper.rb +36 -0
- metadata +41 -33
- data/Gemfile.lock +0 -243
- data/lib/rails_dictionary/active_record_extension.rb +0 -48
- data/lib/rails_dictionary/acts_as_dict_slave.rb +0 -85
- data/lib/rails_dictionary/acts_as_dict_type.rb +0 -52
- data/lib/rails_dictionary/acts_as_dictionary.rb +0 -82
- data/lib/rails_dictionary/array_core_ext.rb +0 -13
- data/pkg/rails_dictionary-0.2.2.gem +0 -0
- data/pkg/rails_dictionary-0.2.3.gem +0 -0
- data/spec/fake_app.rb +0 -29
- data/spec/init_models.rb +0 -12
- data/spec/rails_dictionary_spec.rb +0 -131
- data/spec/spec_helper.rb +0 -22
metadata
CHANGED
@@ -1,29 +1,43 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rails_dictionary
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.pre.rc1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Raykin Lee
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2015-03-21 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
|
-
- - "
|
17
|
+
- - ">"
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: '
|
19
|
+
version: '4.0'
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
|
-
- - "
|
24
|
+
- - ">"
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version: '
|
26
|
+
version: '4.0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: database_cleaner
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
27
41
|
description: Rails plugin for mapping static data of web application to Dictionary
|
28
42
|
class
|
29
43
|
email:
|
@@ -35,29 +49,25 @@ files:
|
|
35
49
|
- ".gitignore"
|
36
50
|
- CHANGELOG
|
37
51
|
- Gemfile
|
38
|
-
-
|
39
|
-
- README.rdoc
|
52
|
+
- README.v2.0.rdoc
|
40
53
|
- Rakefile
|
54
|
+
- Readme.md
|
41
55
|
- lib/rails_dictionary.rb
|
42
|
-
- lib/rails_dictionary/active_record_extension.rb
|
43
|
-
- lib/rails_dictionary/
|
44
|
-
- lib/rails_dictionary/
|
45
|
-
- lib/rails_dictionary/acts_as_dictionary.rb
|
46
|
-
- lib/rails_dictionary/array_core_ext.rb
|
56
|
+
- lib/rails_dictionary/models/active_record_extension.rb
|
57
|
+
- lib/rails_dictionary/models/acts_as_dict_consumer.rb
|
58
|
+
- lib/rails_dictionary/models/acts_as_dictionary.rb
|
47
59
|
- lib/rails_dictionary/version.rb
|
48
60
|
- lib/tasks/dicts.rake
|
49
|
-
- pkg/rails_dictionary-0.2.2.gem
|
50
|
-
- pkg/rails_dictionary-0.2.3.gem
|
51
61
|
- rails_dictionary.gemspec
|
52
|
-
-
|
53
|
-
-
|
54
|
-
-
|
55
|
-
-
|
62
|
+
- test/acts_as_consumer_test.rb
|
63
|
+
- test/fake_app.rb
|
64
|
+
- test/lookup_test.rb
|
65
|
+
- test/rails_dictionary_test.rb
|
66
|
+
- test/test_helper.rb
|
56
67
|
homepage: https://github.com/raykin/rails_dictionary
|
57
|
-
licenses:
|
58
|
-
- MIT
|
68
|
+
licenses: []
|
59
69
|
metadata: {}
|
60
|
-
post_install_message:
|
70
|
+
post_install_message:
|
61
71
|
rdoc_options: []
|
62
72
|
require_paths:
|
63
73
|
- lib
|
@@ -65,19 +75,17 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
65
75
|
requirements:
|
66
76
|
- - ">="
|
67
77
|
- !ruby/object:Gem::Version
|
68
|
-
version:
|
78
|
+
version: '0'
|
69
79
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
70
80
|
requirements:
|
71
|
-
- - "
|
81
|
+
- - ">"
|
72
82
|
- !ruby/object:Gem::Version
|
73
|
-
version:
|
83
|
+
version: 1.3.1
|
74
84
|
requirements: []
|
75
|
-
|
76
|
-
|
85
|
+
rubyforge_project: rails_dictionary
|
86
|
+
rubygems_version: 2.2.2
|
87
|
+
signing_key:
|
77
88
|
specification_version: 4
|
78
89
|
summary: dictionary data for web application
|
79
|
-
test_files:
|
80
|
-
|
81
|
-
- spec/init_models.rb
|
82
|
-
- spec/rails_dictionary_spec.rb
|
83
|
-
- spec/spec_helper.rb
|
90
|
+
test_files: []
|
91
|
+
has_rdoc:
|
data/Gemfile.lock
DELETED
@@ -1,243 +0,0 @@
|
|
1
|
-
PATH
|
2
|
-
remote: .
|
3
|
-
specs:
|
4
|
-
rails_dictionary (0.2.6)
|
5
|
-
rails (< 8)
|
6
|
-
|
7
|
-
GEM
|
8
|
-
remote: https://rubygems.org/
|
9
|
-
specs:
|
10
|
-
actioncable (7.2.1.2)
|
11
|
-
actionpack (= 7.2.1.2)
|
12
|
-
activesupport (= 7.2.1.2)
|
13
|
-
nio4r (~> 2.0)
|
14
|
-
websocket-driver (>= 0.6.1)
|
15
|
-
zeitwerk (~> 2.6)
|
16
|
-
actionmailbox (7.2.1.2)
|
17
|
-
actionpack (= 7.2.1.2)
|
18
|
-
activejob (= 7.2.1.2)
|
19
|
-
activerecord (= 7.2.1.2)
|
20
|
-
activestorage (= 7.2.1.2)
|
21
|
-
activesupport (= 7.2.1.2)
|
22
|
-
mail (>= 2.8.0)
|
23
|
-
actionmailer (7.2.1.2)
|
24
|
-
actionpack (= 7.2.1.2)
|
25
|
-
actionview (= 7.2.1.2)
|
26
|
-
activejob (= 7.2.1.2)
|
27
|
-
activesupport (= 7.2.1.2)
|
28
|
-
mail (>= 2.8.0)
|
29
|
-
rails-dom-testing (~> 2.2)
|
30
|
-
actionpack (7.2.1.2)
|
31
|
-
actionview (= 7.2.1.2)
|
32
|
-
activesupport (= 7.2.1.2)
|
33
|
-
nokogiri (>= 1.8.5)
|
34
|
-
racc
|
35
|
-
rack (>= 2.2.4, < 3.2)
|
36
|
-
rack-session (>= 1.0.1)
|
37
|
-
rack-test (>= 0.6.3)
|
38
|
-
rails-dom-testing (~> 2.2)
|
39
|
-
rails-html-sanitizer (~> 1.6)
|
40
|
-
useragent (~> 0.16)
|
41
|
-
actiontext (7.2.1.2)
|
42
|
-
actionpack (= 7.2.1.2)
|
43
|
-
activerecord (= 7.2.1.2)
|
44
|
-
activestorage (= 7.2.1.2)
|
45
|
-
activesupport (= 7.2.1.2)
|
46
|
-
globalid (>= 0.6.0)
|
47
|
-
nokogiri (>= 1.8.5)
|
48
|
-
actionview (7.2.1.2)
|
49
|
-
activesupport (= 7.2.1.2)
|
50
|
-
builder (~> 3.1)
|
51
|
-
erubi (~> 1.11)
|
52
|
-
rails-dom-testing (~> 2.2)
|
53
|
-
rails-html-sanitizer (~> 1.6)
|
54
|
-
activejob (7.2.1.2)
|
55
|
-
activesupport (= 7.2.1.2)
|
56
|
-
globalid (>= 0.3.6)
|
57
|
-
activemodel (7.2.1.2)
|
58
|
-
activesupport (= 7.2.1.2)
|
59
|
-
activerecord (7.2.1.2)
|
60
|
-
activemodel (= 7.2.1.2)
|
61
|
-
activesupport (= 7.2.1.2)
|
62
|
-
timeout (>= 0.4.0)
|
63
|
-
activestorage (7.2.1.2)
|
64
|
-
actionpack (= 7.2.1.2)
|
65
|
-
activejob (= 7.2.1.2)
|
66
|
-
activerecord (= 7.2.1.2)
|
67
|
-
activesupport (= 7.2.1.2)
|
68
|
-
marcel (~> 1.0)
|
69
|
-
activesupport (7.2.1.2)
|
70
|
-
base64
|
71
|
-
bigdecimal
|
72
|
-
concurrent-ruby (~> 1.0, >= 1.3.1)
|
73
|
-
connection_pool (>= 2.2.5)
|
74
|
-
drb
|
75
|
-
i18n (>= 1.6, < 2)
|
76
|
-
logger (>= 1.4.2)
|
77
|
-
minitest (>= 5.1)
|
78
|
-
securerandom (>= 0.3)
|
79
|
-
tzinfo (~> 2.0, >= 2.0.5)
|
80
|
-
base64 (0.2.0)
|
81
|
-
bigdecimal (3.1.8)
|
82
|
-
builder (3.3.0)
|
83
|
-
concurrent-ruby (1.3.4)
|
84
|
-
connection_pool (2.4.1)
|
85
|
-
crass (1.0.6)
|
86
|
-
date (3.3.4)
|
87
|
-
diff-lcs (1.5.1)
|
88
|
-
drb (2.2.1)
|
89
|
-
erubi (1.13.0)
|
90
|
-
globalid (1.2.1)
|
91
|
-
activesupport (>= 6.1)
|
92
|
-
i18n (1.14.6)
|
93
|
-
concurrent-ruby (~> 1.0)
|
94
|
-
io-console (0.7.2)
|
95
|
-
irb (1.14.1)
|
96
|
-
rdoc (>= 4.0.0)
|
97
|
-
reline (>= 0.4.2)
|
98
|
-
logger (1.6.1)
|
99
|
-
loofah (2.23.1)
|
100
|
-
crass (~> 1.0.2)
|
101
|
-
nokogiri (>= 1.12.0)
|
102
|
-
mail (2.8.1)
|
103
|
-
mini_mime (>= 0.1.1)
|
104
|
-
net-imap
|
105
|
-
net-pop
|
106
|
-
net-smtp
|
107
|
-
marcel (1.0.4)
|
108
|
-
mini_mime (1.1.5)
|
109
|
-
minitest (5.25.1)
|
110
|
-
net-imap (0.5.0)
|
111
|
-
date
|
112
|
-
net-protocol
|
113
|
-
net-pop (0.1.2)
|
114
|
-
net-protocol
|
115
|
-
net-protocol (0.2.2)
|
116
|
-
timeout
|
117
|
-
net-smtp (0.5.0)
|
118
|
-
net-protocol
|
119
|
-
nio4r (2.7.3)
|
120
|
-
nokogiri (1.16.7-aarch64-linux)
|
121
|
-
racc (~> 1.4)
|
122
|
-
nokogiri (1.16.7-arm-linux)
|
123
|
-
racc (~> 1.4)
|
124
|
-
nokogiri (1.16.7-arm64-darwin)
|
125
|
-
racc (~> 1.4)
|
126
|
-
nokogiri (1.16.7-x86-linux)
|
127
|
-
racc (~> 1.4)
|
128
|
-
nokogiri (1.16.7-x86_64-darwin)
|
129
|
-
racc (~> 1.4)
|
130
|
-
nokogiri (1.16.7-x86_64-linux)
|
131
|
-
racc (~> 1.4)
|
132
|
-
psych (5.1.2)
|
133
|
-
stringio
|
134
|
-
racc (1.8.1)
|
135
|
-
rack (3.1.8)
|
136
|
-
rack-session (2.0.0)
|
137
|
-
rack (>= 3.0.0)
|
138
|
-
rack-test (2.1.0)
|
139
|
-
rack (>= 1.3)
|
140
|
-
rackup (2.1.0)
|
141
|
-
rack (>= 3)
|
142
|
-
webrick (~> 1.8)
|
143
|
-
rails (7.2.1.2)
|
144
|
-
actioncable (= 7.2.1.2)
|
145
|
-
actionmailbox (= 7.2.1.2)
|
146
|
-
actionmailer (= 7.2.1.2)
|
147
|
-
actionpack (= 7.2.1.2)
|
148
|
-
actiontext (= 7.2.1.2)
|
149
|
-
actionview (= 7.2.1.2)
|
150
|
-
activejob (= 7.2.1.2)
|
151
|
-
activemodel (= 7.2.1.2)
|
152
|
-
activerecord (= 7.2.1.2)
|
153
|
-
activestorage (= 7.2.1.2)
|
154
|
-
activesupport (= 7.2.1.2)
|
155
|
-
bundler (>= 1.15.0)
|
156
|
-
railties (= 7.2.1.2)
|
157
|
-
rails-dom-testing (2.2.0)
|
158
|
-
activesupport (>= 5.0.0)
|
159
|
-
minitest
|
160
|
-
nokogiri (>= 1.6)
|
161
|
-
rails-html-sanitizer (1.6.0)
|
162
|
-
loofah (~> 2.21)
|
163
|
-
nokogiri (~> 1.14)
|
164
|
-
railties (7.2.1.2)
|
165
|
-
actionpack (= 7.2.1.2)
|
166
|
-
activesupport (= 7.2.1.2)
|
167
|
-
irb (~> 1.13)
|
168
|
-
rackup (>= 1.0.0)
|
169
|
-
rake (>= 12.2)
|
170
|
-
thor (~> 1.0, >= 1.2.2)
|
171
|
-
zeitwerk (~> 2.6)
|
172
|
-
rake (13.2.1)
|
173
|
-
rdoc (6.7.0)
|
174
|
-
psych (>= 4.0.0)
|
175
|
-
reline (0.5.10)
|
176
|
-
io-console (~> 0.5)
|
177
|
-
rspec-core (3.13.2)
|
178
|
-
rspec-support (~> 3.13.0)
|
179
|
-
rspec-expectations (3.13.3)
|
180
|
-
diff-lcs (>= 1.2.0, < 2.0)
|
181
|
-
rspec-support (~> 3.13.0)
|
182
|
-
rspec-mocks (3.13.2)
|
183
|
-
diff-lcs (>= 1.2.0, < 2.0)
|
184
|
-
rspec-support (~> 3.13.0)
|
185
|
-
rspec-rails (7.0.1)
|
186
|
-
actionpack (>= 7.0)
|
187
|
-
activesupport (>= 7.0)
|
188
|
-
railties (>= 7.0)
|
189
|
-
rspec-core (~> 3.13)
|
190
|
-
rspec-expectations (~> 3.13)
|
191
|
-
rspec-mocks (~> 3.13)
|
192
|
-
rspec-support (~> 3.13)
|
193
|
-
rspec-support (3.13.1)
|
194
|
-
securerandom (0.3.1)
|
195
|
-
sqlite3 (2.1.1-aarch64-linux-gnu)
|
196
|
-
sqlite3 (2.1.1-aarch64-linux-musl)
|
197
|
-
sqlite3 (2.1.1-arm-linux-gnu)
|
198
|
-
sqlite3 (2.1.1-arm-linux-musl)
|
199
|
-
sqlite3 (2.1.1-arm64-darwin)
|
200
|
-
sqlite3 (2.1.1-x86-linux-gnu)
|
201
|
-
sqlite3 (2.1.1-x86-linux-musl)
|
202
|
-
sqlite3 (2.1.1-x86_64-darwin)
|
203
|
-
sqlite3 (2.1.1-x86_64-linux-gnu)
|
204
|
-
sqlite3 (2.1.1-x86_64-linux-musl)
|
205
|
-
stringio (3.1.1)
|
206
|
-
thor (1.3.2)
|
207
|
-
timeout (0.4.1)
|
208
|
-
tzinfo (2.0.6)
|
209
|
-
concurrent-ruby (~> 1.0)
|
210
|
-
useragent (0.16.10)
|
211
|
-
webrick (1.8.2)
|
212
|
-
websocket-driver (0.7.6)
|
213
|
-
websocket-extensions (>= 0.1.0)
|
214
|
-
websocket-extensions (0.1.5)
|
215
|
-
zeitwerk (2.7.1)
|
216
|
-
|
217
|
-
PLATFORMS
|
218
|
-
aarch64-linux
|
219
|
-
aarch64-linux-gnu
|
220
|
-
aarch64-linux-musl
|
221
|
-
arm-linux
|
222
|
-
arm-linux-gnu
|
223
|
-
arm-linux-musl
|
224
|
-
arm64-darwin
|
225
|
-
x86-linux
|
226
|
-
x86-linux-gnu
|
227
|
-
x86-linux-musl
|
228
|
-
x86_64-darwin
|
229
|
-
x86_64-linux
|
230
|
-
x86_64-linux-gnu
|
231
|
-
x86_64-linux-musl
|
232
|
-
|
233
|
-
DEPENDENCIES
|
234
|
-
rails (< 8.0)
|
235
|
-
rails_dictionary!
|
236
|
-
rspec-rails (< 8)
|
237
|
-
sqlite3
|
238
|
-
|
239
|
-
RUBY VERSION
|
240
|
-
ruby 3.3.5p100
|
241
|
-
|
242
|
-
BUNDLED WITH
|
243
|
-
2.5.17
|
@@ -1,48 +0,0 @@
|
|
1
|
-
module RailsDictionary
|
2
|
-
module ActiveRecordExtension
|
3
|
-
def self.included(base)
|
4
|
-
base.extend(ClassMethods)
|
5
|
-
end
|
6
|
-
|
7
|
-
module ClassMethods
|
8
|
-
# TODO: move macro define in each module file
|
9
|
-
# See Usage in readme.doc.
|
10
|
-
def acts_as_dict_type
|
11
|
-
|
12
|
-
has_many :dictionaries
|
13
|
-
validates_uniqueness_of :name
|
14
|
-
after_save :delete_all_caches
|
15
|
-
after_destroy :delete_all_caches
|
16
|
-
|
17
|
-
include RailsDictionary::ActsAsDictType
|
18
|
-
end
|
19
|
-
|
20
|
-
def acts_as_dictionary
|
21
|
-
|
22
|
-
belongs_to :dict_type
|
23
|
-
after_save :delete_dicts_cache
|
24
|
-
after_destroy :delete_dicts_cache
|
25
|
-
scope :dict_type_name_eq, ->(name) { joins(:dict_type).where({ "dict_types.name" => name }) }
|
26
|
-
|
27
|
-
include RailsDictionary::ActsAsDictionary
|
28
|
-
end
|
29
|
-
|
30
|
-
# Ex: acts_as_dict_slave :add => :category
|
31
|
-
# :except - remove dict mapping column
|
32
|
-
# :add - add dict mapping column
|
33
|
-
# :locale - add and initialize class attribute default_dict_locale
|
34
|
-
def acts_as_dict_slave(ops={})
|
35
|
-
include RailsDictionary::ActsAsDictSlave
|
36
|
-
class_attribute :default_dict_locale, :instance_writer => false
|
37
|
-
cattr_accessor :dict_mapping_columns, :instance_writer => false
|
38
|
-
self.default_dict_locale = ops[:locale] if ops[:locale]
|
39
|
-
self.dict_mapping_columns = dict_columns(ops)
|
40
|
-
unless dict_mapping_columns.nil?
|
41
|
-
add_dynamic_column_method
|
42
|
-
end
|
43
|
-
end
|
44
|
-
|
45
|
-
alias_method :acts_as_dict_consumer, :acts_as_dict_slave
|
46
|
-
end
|
47
|
-
end
|
48
|
-
end
|
@@ -1,85 +0,0 @@
|
|
1
|
-
module RailsDictionary
|
2
|
-
module ActsAsDictSlave
|
3
|
-
extend ActiveSupport::Concern
|
4
|
-
|
5
|
-
class_methods do
|
6
|
-
# return columns that exist in DictType#tab_and_column
|
7
|
-
def columns_in_dict_type
|
8
|
-
if ActiveRecord::VERSION::STRING < '3.1'
|
9
|
-
DictType.tab_and_column[self.name.underscore.to_sym]
|
10
|
-
elsif ::DictType.table_exists?
|
11
|
-
DictType.tab_and_column[self.name.underscore.to_sym]
|
12
|
-
else
|
13
|
-
[]
|
14
|
-
end
|
15
|
-
end
|
16
|
-
|
17
|
-
# columns which map to dictionary
|
18
|
-
def dict_columns(ops={})
|
19
|
-
conf = { except: nil, add: nil}
|
20
|
-
conf.update(ops)
|
21
|
-
cidt = columns_in_dict_type || []
|
22
|
-
cidt.delete(conf[:except])
|
23
|
-
case conf[:add]
|
24
|
-
when String
|
25
|
-
cidt.push(conf[:add])
|
26
|
-
when Array
|
27
|
-
cidt.push(*conf[:add])
|
28
|
-
else nil
|
29
|
-
end
|
30
|
-
cidt.uniq! || cidt
|
31
|
-
end
|
32
|
-
|
33
|
-
# add a belongs_to(Dictionary) association and a named_{column} method
|
34
|
-
def add_dynamic_column_method
|
35
|
-
dict_mapping_columns.each { |e| belongs_to "#{e}_dict".to_sym, class_name: "Dictionary", foreign_key: e.to_sym }
|
36
|
-
dict_mapping_columns.each { |ele| named_dict_value ele.to_sym }
|
37
|
-
dict_mapping_columns.each { |ele| dict_name_equal ele.to_sym }
|
38
|
-
end
|
39
|
-
|
40
|
-
# Generate dynamic instance method named_column to consumer model
|
41
|
-
# def named_city(locale=nil)
|
42
|
-
# locale = locale.presence || default_dict_locale.presence || :en
|
43
|
-
# locale = "name_#{locale}"
|
44
|
-
# self.send(city_dict).try(:send,locale)
|
45
|
-
# end
|
46
|
-
# alias_method :city_name, :named_city
|
47
|
-
def named_dict_value(method_name)
|
48
|
-
belongs_to_name="#{method_name}_dict".to_sym
|
49
|
-
origin_method_name = method_name
|
50
|
-
method_name="named_#{method_name}"
|
51
|
-
define_method(method_name) do | locale=nil |
|
52
|
-
locale = locale.presence || default_dict_locale.presence || :en
|
53
|
-
locale = "name_#{locale}"
|
54
|
-
self.send(belongs_to_name).try(:send,locale)
|
55
|
-
end
|
56
|
-
alias_method "#{origin_method_name}_name".to_sym, method_name.to_sym
|
57
|
-
end
|
58
|
-
|
59
|
-
# Build dynamic method column_name= to the consumer model
|
60
|
-
#
|
61
|
-
# def city_name=(value, options = {})
|
62
|
-
#
|
63
|
-
#
|
64
|
-
# end
|
65
|
-
def dict_name_equal(colname)
|
66
|
-
method_name = "#{colname}_name="
|
67
|
-
belongs_to_name="#{colname}_dict".to_sym
|
68
|
-
define_method(method_name) do |value, options={}|
|
69
|
-
options.merge!(name_en: value)
|
70
|
-
dict_type_id = DictType.revert("#{self.class.table_name.singularize}_#{colname}")
|
71
|
-
options.merge!(dict_type_id: dict_type_id)
|
72
|
-
exist_dictionary = Dictionary.where(options)
|
73
|
-
if exist_dictionary.present?
|
74
|
-
exist_id = exist_dictionary.first.id
|
75
|
-
else
|
76
|
-
exist_id = send("create_#{belongs_to_name}!", options).id
|
77
|
-
end
|
78
|
-
send "#{colname}=", exist_id
|
79
|
-
end
|
80
|
-
end
|
81
|
-
|
82
|
-
end # END class_methods
|
83
|
-
|
84
|
-
end
|
85
|
-
end
|
@@ -1,52 +0,0 @@
|
|
1
|
-
module RailsDictionary
|
2
|
-
module ActsAsDictType
|
3
|
-
def self.included(base)
|
4
|
-
base.extend(ClassMethods)
|
5
|
-
base.mattr_accessor :whole_types
|
6
|
-
end
|
7
|
-
|
8
|
-
module ClassMethods
|
9
|
-
|
10
|
-
def all_types
|
11
|
-
whole_types ||
|
12
|
-
self.whole_types = all.map(&:name).map(&:to_sym)
|
13
|
-
end
|
14
|
-
|
15
|
-
# short method to transfer id to name or name to id
|
16
|
-
# TODO: cache it
|
17
|
-
def revert(arg)
|
18
|
-
if arg.is_a?(String)
|
19
|
-
DictType.where(name: arg).first.try(:id)
|
20
|
-
elsif arg.is_a?(Integer)
|
21
|
-
DictType.where(id: arg).first.try(:name)
|
22
|
-
end
|
23
|
-
end
|
24
|
-
|
25
|
-
#
|
26
|
-
# Parse the name value to get which column and model(or table) are listed in DictType
|
27
|
-
#
|
28
|
-
# Programmer DOC:
|
29
|
-
# There are two chooses to get subclass,one is subclasses the other is descendants,
|
30
|
-
# I don't know which is better,but descendants contains subclass of subclass,it contains more.
|
31
|
-
#
|
32
|
-
# Class like +Ckeditor::Asset+ transfer to "ckeditor/asset",but we can not naming method like that,
|
33
|
-
# So it still not support, the solution may be simple,just make another convention to escape "/"
|
34
|
-
#
|
35
|
-
# Seems this method did not need to be cached in production.
|
36
|
-
# Because everyclass was cached before application was run.So after application was run, it never be run again.
|
37
|
-
# TODO:
|
38
|
-
# To cache this method output need more skills on how to caculate ActiveRecord::Base.descendants
|
39
|
-
# Temply remove the cache
|
40
|
-
# And add test for this situation
|
41
|
-
def tab_and_column
|
42
|
-
all_model_class=ActiveRecord::Base.descendants.map(&:name).map(&:underscore)
|
43
|
-
all_types.map(&:to_s).extract_to_hash(all_model_class)
|
44
|
-
end
|
45
|
-
end
|
46
|
-
|
47
|
-
def delete_all_caches
|
48
|
-
self.whole_types = nil
|
49
|
-
return true
|
50
|
-
end
|
51
|
-
end
|
52
|
-
end
|
@@ -1,82 +0,0 @@
|
|
1
|
-
require "active_support/concern"
|
2
|
-
|
3
|
-
module RailsDictionary
|
4
|
-
module ActsAsDictionary
|
5
|
-
extend ActiveSupport::Concern
|
6
|
-
|
7
|
-
class_methods do
|
8
|
-
|
9
|
-
# For rails3
|
10
|
-
# I thought it would be better to define a method in method_missing, Not just generate cache.
|
11
|
-
# Cause cache can not store ActiveRecord
|
12
|
-
# Generate methods like Dictionary.student_city
|
13
|
-
# Dictionary.student_city - a list of dictionary object which dict type is student_city
|
14
|
-
# Dictionary.student_city(:locale => :zh) - a select format array which can be used
|
15
|
-
# in view method select as choice params
|
16
|
-
# Programmer DOC && TODO:
|
17
|
-
# rethink about the cache.
|
18
|
-
# cache methods like Dictionary.student_city(:locale => :zh,:sort => :name_fr)
|
19
|
-
# but not cache Dictionary.student_city, return it as relation
|
20
|
-
#
|
21
|
-
# Remove nil noise,if listed_attr =[[nil, 201], [nil, 203], [nil, 202], ["Sciences", 200]]
|
22
|
-
# the sort would be failed of ArgumentError: comparison of Array with Array failed
|
23
|
-
# split this method ,make it more short and maintainance
|
24
|
-
def method_missing(method_id,options={})
|
25
|
-
if ::DictType.all_types.include? method_id
|
26
|
-
method_name=method_id.to_s.downcase
|
27
|
-
# TODO: If cache engine is failed, then the code will failed with null cant dup
|
28
|
-
Rails.cache.fetch("Dictionary.#{method_name}") { dict_type_name_eq(method_name).to_a }
|
29
|
-
listed_attr=Rails.cache.read("Dictionary.#{method_name}").dup # Instance of ActiveRecord::Relation can not be dup?
|
30
|
-
build_scope_method(method_id)
|
31
|
-
if options.keys.include? :locale or options.keys.include? "locale"
|
32
|
-
locale="name_#{ options[:locale] }"
|
33
|
-
sort_block=sort_dicts(options)
|
34
|
-
listed_attr.sort!(&sort_block) if sort_block
|
35
|
-
listed_attr.map! { |a| [a.send(locale),a.id] }.reject! {|ele| ele.first.nil?}
|
36
|
-
end
|
37
|
-
listed_attr
|
38
|
-
else
|
39
|
-
super
|
40
|
-
end
|
41
|
-
end
|
42
|
-
|
43
|
-
# Override this method to get customed sort block
|
44
|
-
def sort_dicts(options)
|
45
|
-
if options.keys.include? :locale or options.keys.include? "locale"
|
46
|
-
locale="name_#{ options[:locale] }"
|
47
|
-
if options[:locale].to_sym == :zh
|
48
|
-
Proc.new { |a,b| a.send(locale).encode('GBK') <=> b.send(locale).encode('GBK') }
|
49
|
-
else
|
50
|
-
Proc.new { |a,b| a.send(locale).downcase <=> b.send(locale).downcase }
|
51
|
-
end
|
52
|
-
else
|
53
|
-
false
|
54
|
-
end
|
55
|
-
end
|
56
|
-
|
57
|
-
def respond_to?(name, include_private=false)
|
58
|
-
::DictType.all_types.include?(name) || super
|
59
|
-
end
|
60
|
-
|
61
|
-
private
|
62
|
-
|
63
|
-
def build_scope_method(name)
|
64
|
-
scope_method_name = "scoped_#{name}".to_sym
|
65
|
-
unless respond_to? scope_method_name
|
66
|
-
define_singleton_method scope_method_name do
|
67
|
-
# see http://stackoverflow.com/questions/18198963/with-rails-4-model-scoped-is-deprecated-but-model-all-cant-replace-it
|
68
|
-
# for usage of where(nil)
|
69
|
-
dict_type_name_eq(name).where(nil)
|
70
|
-
end
|
71
|
-
end
|
72
|
-
end
|
73
|
-
|
74
|
-
end # End class_method
|
75
|
-
|
76
|
-
def delete_dicts_cache
|
77
|
-
method_name = ::DictType.revert(self.dict_type_id)
|
78
|
-
Rails.cache.delete("Dictionary.#{method_name}")
|
79
|
-
return true
|
80
|
-
end
|
81
|
-
end
|
82
|
-
end
|
@@ -1,13 +0,0 @@
|
|
1
|
-
class Array
|
2
|
-
# Return a hash by compare two arrays
|
3
|
-
def extract_to_hash(keys_array)
|
4
|
-
ret_hash={}
|
5
|
-
keys_array.each {|ky| ret_hash[ky.to_sym]=[]}
|
6
|
-
self.each do |sf|
|
7
|
-
keys_array.each do |ky|
|
8
|
-
ret_hash[ky.to_sym] << sf.sub("#{ky}_","") if sf =~ Regexp.new("^#{ky}_")
|
9
|
-
end
|
10
|
-
end
|
11
|
-
ret_hash.reject { |k,v| v.blank? }
|
12
|
-
end
|
13
|
-
end
|
Binary file
|
Binary file
|
data/spec/fake_app.rb
DELETED
@@ -1,29 +0,0 @@
|
|
1
|
-
# database
|
2
|
-
ActiveRecord::Base.configurations = {'test' => {:adapter => 'sqlite3', :database => ':memory:'}}
|
3
|
-
|
4
|
-
if ActiveRecord::VERSION::MAJOR >= 5
|
5
|
-
ActiveRecord::Base.establish_connection(:adapter => 'sqlite3', :database => ':memory:')
|
6
|
-
else
|
7
|
-
ActiveRecord::Base.establish_connection(:test)
|
8
|
-
end
|
9
|
-
|
10
|
-
# config
|
11
|
-
app = Class.new(Rails::Application)
|
12
|
-
app.config.active_support.deprecation = :log
|
13
|
-
app.config.eager_load = false
|
14
|
-
app.initialize!
|
15
|
-
|
16
|
-
#migrations
|
17
|
-
class CreateAllTables < ActiveRecord::VERSION::MAJOR >= 5 ? ActiveRecord::Migration[5.0] : ActiveRecord::Migration
|
18
|
-
def self.up
|
19
|
-
create_table(:dict_types) {|t| t.string :name}
|
20
|
-
create_table(:dictionaries) {|t| t.string :name_en; t.string :name_zh ; t.string :name_fr ; t.integer :dict_type_id}
|
21
|
-
create_table(:students) {|t| t.string :email; t.integer :city; t.integer :school}
|
22
|
-
end
|
23
|
-
|
24
|
-
def self.down
|
25
|
-
drop_table :dict_types
|
26
|
-
drop_table :dictionaries
|
27
|
-
drop_table :students
|
28
|
-
end
|
29
|
-
end
|
data/spec/init_models.rb
DELETED
@@ -1,12 +0,0 @@
|
|
1
|
-
class Dictionary < ActiveRecord::Base
|
2
|
-
acts_as_dictionary
|
3
|
-
end
|
4
|
-
|
5
|
-
class DictType < ActiveRecord::Base
|
6
|
-
acts_as_dict_type
|
7
|
-
end
|
8
|
-
|
9
|
-
# TODO: Here is a serious code loading issue. Student can't be load before DictType and Dictionary
|
10
|
-
class Student < ActiveRecord::Base
|
11
|
-
acts_as_dict_slave
|
12
|
-
end
|