rails_dictionary 0.2.6 → 0.3.pre.rc1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/.gitignore +3 -3
- data/CHANGELOG +4 -2
- data/Gemfile +2 -3
- 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 +5 -3
- 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 +37 -33
- data/Gemfile.lock +0 -193
- data/lib/rails_dictionary/active_record_extension.rb +0 -48
- data/lib/rails_dictionary/acts_as_dict_slave.rb +0 -88
- data/lib/rails_dictionary/acts_as_dict_type.rb +0 -56
- data/lib/rails_dictionary/acts_as_dictionary.rb +0 -86
- 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 -41
- data/spec/rails_dictionary_spec.rb +0 -130
- data/spec/spec_helper.rb +0 -23
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
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
|
@@ -16,20 +16,28 @@ dependencies:
|
|
16
16
|
requirements:
|
17
17
|
- - ">"
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: '
|
20
|
-
- - "<"
|
21
|
-
- !ruby/object:Gem::Version
|
22
|
-
version: '7.1'
|
19
|
+
version: '4.0'
|
23
20
|
type: :runtime
|
24
21
|
prerelease: false
|
25
22
|
version_requirements: !ruby/object:Gem::Requirement
|
26
23
|
requirements:
|
27
24
|
- - ">"
|
28
25
|
- !ruby/object:Gem::Version
|
29
|
-
version: '
|
30
|
-
|
26
|
+
version: '4.0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: database_cleaner
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
31
32
|
- !ruby/object:Gem::Version
|
32
|
-
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'
|
33
41
|
description: Rails plugin for mapping static data of web application to Dictionary
|
34
42
|
class
|
35
43
|
email:
|
@@ -41,28 +49,25 @@ files:
|
|
41
49
|
- ".gitignore"
|
42
50
|
- CHANGELOG
|
43
51
|
- Gemfile
|
44
|
-
-
|
45
|
-
- README.rdoc
|
52
|
+
- README.v2.0.rdoc
|
46
53
|
- Rakefile
|
54
|
+
- Readme.md
|
47
55
|
- lib/rails_dictionary.rb
|
48
|
-
- lib/rails_dictionary/active_record_extension.rb
|
49
|
-
- lib/rails_dictionary/
|
50
|
-
- lib/rails_dictionary/
|
51
|
-
- lib/rails_dictionary/acts_as_dictionary.rb
|
52
|
-
- 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
|
53
59
|
- lib/rails_dictionary/version.rb
|
54
60
|
- lib/tasks/dicts.rake
|
55
|
-
- pkg/rails_dictionary-0.2.2.gem
|
56
|
-
- pkg/rails_dictionary-0.2.3.gem
|
57
61
|
- rails_dictionary.gemspec
|
58
|
-
-
|
59
|
-
-
|
60
|
-
-
|
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
|
61
67
|
homepage: https://github.com/raykin/rails_dictionary
|
62
|
-
licenses:
|
63
|
-
- MIT
|
68
|
+
licenses: []
|
64
69
|
metadata: {}
|
65
|
-
post_install_message:
|
70
|
+
post_install_message:
|
66
71
|
rdoc_options: []
|
67
72
|
require_paths:
|
68
73
|
- lib
|
@@ -73,15 +78,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
73
78
|
version: '0'
|
74
79
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
75
80
|
requirements:
|
76
|
-
- - "
|
81
|
+
- - ">"
|
77
82
|
- !ruby/object:Gem::Version
|
78
|
-
version:
|
83
|
+
version: 1.3.1
|
79
84
|
requirements: []
|
80
|
-
|
81
|
-
|
85
|
+
rubyforge_project: rails_dictionary
|
86
|
+
rubygems_version: 2.2.2
|
87
|
+
signing_key:
|
82
88
|
specification_version: 4
|
83
89
|
summary: dictionary data for web application
|
84
|
-
test_files:
|
85
|
-
|
86
|
-
- spec/rails_dictionary_spec.rb
|
87
|
-
- spec/spec_helper.rb
|
90
|
+
test_files: []
|
91
|
+
has_rdoc:
|
data/Gemfile.lock
DELETED
@@ -1,193 +0,0 @@
|
|
1
|
-
PATH
|
2
|
-
remote: .
|
3
|
-
specs:
|
4
|
-
rails_dictionary (0.2.5)
|
5
|
-
rails (> 6.0, < 7.1)
|
6
|
-
|
7
|
-
GEM
|
8
|
-
remote: http://rubygems.org/
|
9
|
-
specs:
|
10
|
-
actioncable (7.0.2.2)
|
11
|
-
actionpack (= 7.0.2.2)
|
12
|
-
activesupport (= 7.0.2.2)
|
13
|
-
nio4r (~> 2.0)
|
14
|
-
websocket-driver (>= 0.6.1)
|
15
|
-
actionmailbox (7.0.2.2)
|
16
|
-
actionpack (= 7.0.2.2)
|
17
|
-
activejob (= 7.0.2.2)
|
18
|
-
activerecord (= 7.0.2.2)
|
19
|
-
activestorage (= 7.0.2.2)
|
20
|
-
activesupport (= 7.0.2.2)
|
21
|
-
mail (>= 2.7.1)
|
22
|
-
net-imap
|
23
|
-
net-pop
|
24
|
-
net-smtp
|
25
|
-
actionmailer (7.0.2.2)
|
26
|
-
actionpack (= 7.0.2.2)
|
27
|
-
actionview (= 7.0.2.2)
|
28
|
-
activejob (= 7.0.2.2)
|
29
|
-
activesupport (= 7.0.2.2)
|
30
|
-
mail (~> 2.5, >= 2.5.4)
|
31
|
-
net-imap
|
32
|
-
net-pop
|
33
|
-
net-smtp
|
34
|
-
rails-dom-testing (~> 2.0)
|
35
|
-
actionpack (7.0.2.2)
|
36
|
-
actionview (= 7.0.2.2)
|
37
|
-
activesupport (= 7.0.2.2)
|
38
|
-
rack (~> 2.0, >= 2.2.0)
|
39
|
-
rack-test (>= 0.6.3)
|
40
|
-
rails-dom-testing (~> 2.0)
|
41
|
-
rails-html-sanitizer (~> 1.0, >= 1.2.0)
|
42
|
-
actiontext (7.0.2.2)
|
43
|
-
actionpack (= 7.0.2.2)
|
44
|
-
activerecord (= 7.0.2.2)
|
45
|
-
activestorage (= 7.0.2.2)
|
46
|
-
activesupport (= 7.0.2.2)
|
47
|
-
globalid (>= 0.6.0)
|
48
|
-
nokogiri (>= 1.8.5)
|
49
|
-
actionview (7.0.2.2)
|
50
|
-
activesupport (= 7.0.2.2)
|
51
|
-
builder (~> 3.1)
|
52
|
-
erubi (~> 1.4)
|
53
|
-
rails-dom-testing (~> 2.0)
|
54
|
-
rails-html-sanitizer (~> 1.1, >= 1.2.0)
|
55
|
-
activejob (7.0.2.2)
|
56
|
-
activesupport (= 7.0.2.2)
|
57
|
-
globalid (>= 0.3.6)
|
58
|
-
activemodel (7.0.2.2)
|
59
|
-
activesupport (= 7.0.2.2)
|
60
|
-
activerecord (7.0.2.2)
|
61
|
-
activemodel (= 7.0.2.2)
|
62
|
-
activesupport (= 7.0.2.2)
|
63
|
-
activestorage (7.0.2.2)
|
64
|
-
actionpack (= 7.0.2.2)
|
65
|
-
activejob (= 7.0.2.2)
|
66
|
-
activerecord (= 7.0.2.2)
|
67
|
-
activesupport (= 7.0.2.2)
|
68
|
-
marcel (~> 1.0)
|
69
|
-
mini_mime (>= 1.1.0)
|
70
|
-
activesupport (7.0.2.2)
|
71
|
-
concurrent-ruby (~> 1.0, >= 1.0.2)
|
72
|
-
i18n (>= 1.6, < 2)
|
73
|
-
minitest (>= 5.1)
|
74
|
-
tzinfo (~> 2.0)
|
75
|
-
builder (3.2.4)
|
76
|
-
byebug (11.0.1)
|
77
|
-
coderay (1.1.2)
|
78
|
-
concurrent-ruby (1.1.9)
|
79
|
-
crass (1.0.6)
|
80
|
-
diff-lcs (1.5.0)
|
81
|
-
digest (3.1.0)
|
82
|
-
erubi (1.10.0)
|
83
|
-
globalid (1.0.0)
|
84
|
-
activesupport (>= 5.0)
|
85
|
-
i18n (1.10.0)
|
86
|
-
concurrent-ruby (~> 1.0)
|
87
|
-
io-wait (0.2.1)
|
88
|
-
loofah (2.14.0)
|
89
|
-
crass (~> 1.0.2)
|
90
|
-
nokogiri (>= 1.5.9)
|
91
|
-
mail (2.7.1)
|
92
|
-
mini_mime (>= 0.1.1)
|
93
|
-
marcel (1.0.2)
|
94
|
-
method_source (0.9.2)
|
95
|
-
mini_mime (1.1.2)
|
96
|
-
mini_portile2 (2.8.0)
|
97
|
-
minitest (5.15.0)
|
98
|
-
net-imap (0.2.3)
|
99
|
-
digest
|
100
|
-
net-protocol
|
101
|
-
strscan
|
102
|
-
net-pop (0.1.1)
|
103
|
-
digest
|
104
|
-
net-protocol
|
105
|
-
timeout
|
106
|
-
net-protocol (0.1.2)
|
107
|
-
io-wait
|
108
|
-
timeout
|
109
|
-
net-smtp (0.3.1)
|
110
|
-
digest
|
111
|
-
net-protocol
|
112
|
-
timeout
|
113
|
-
nio4r (2.5.8)
|
114
|
-
nokogiri (1.13.4)
|
115
|
-
mini_portile2 (~> 2.8.0)
|
116
|
-
racc (~> 1.4)
|
117
|
-
pry (0.12.2)
|
118
|
-
coderay (~> 1.1.0)
|
119
|
-
method_source (~> 0.9.0)
|
120
|
-
pry-byebug (3.7.0)
|
121
|
-
byebug (~> 11.0)
|
122
|
-
pry (~> 0.10)
|
123
|
-
racc (1.6.0)
|
124
|
-
rack (2.2.3)
|
125
|
-
rack-test (1.1.0)
|
126
|
-
rack (>= 1.0, < 3)
|
127
|
-
rails (7.0.2.2)
|
128
|
-
actioncable (= 7.0.2.2)
|
129
|
-
actionmailbox (= 7.0.2.2)
|
130
|
-
actionmailer (= 7.0.2.2)
|
131
|
-
actionpack (= 7.0.2.2)
|
132
|
-
actiontext (= 7.0.2.2)
|
133
|
-
actionview (= 7.0.2.2)
|
134
|
-
activejob (= 7.0.2.2)
|
135
|
-
activemodel (= 7.0.2.2)
|
136
|
-
activerecord (= 7.0.2.2)
|
137
|
-
activestorage (= 7.0.2.2)
|
138
|
-
activesupport (= 7.0.2.2)
|
139
|
-
bundler (>= 1.15.0)
|
140
|
-
railties (= 7.0.2.2)
|
141
|
-
rails-dom-testing (2.0.3)
|
142
|
-
activesupport (>= 4.2.0)
|
143
|
-
nokogiri (>= 1.6)
|
144
|
-
rails-html-sanitizer (1.4.2)
|
145
|
-
loofah (~> 2.3)
|
146
|
-
railties (7.0.2.2)
|
147
|
-
actionpack (= 7.0.2.2)
|
148
|
-
activesupport (= 7.0.2.2)
|
149
|
-
method_source
|
150
|
-
rake (>= 12.2)
|
151
|
-
thor (~> 1.0)
|
152
|
-
zeitwerk (~> 2.5)
|
153
|
-
rake (13.0.6)
|
154
|
-
rspec-core (3.11.0)
|
155
|
-
rspec-support (~> 3.11.0)
|
156
|
-
rspec-expectations (3.11.0)
|
157
|
-
diff-lcs (>= 1.2.0, < 2.0)
|
158
|
-
rspec-support (~> 3.11.0)
|
159
|
-
rspec-mocks (3.11.0)
|
160
|
-
diff-lcs (>= 1.2.0, < 2.0)
|
161
|
-
rspec-support (~> 3.11.0)
|
162
|
-
rspec-rails (5.1.0)
|
163
|
-
actionpack (>= 5.2)
|
164
|
-
activesupport (>= 5.2)
|
165
|
-
railties (>= 5.2)
|
166
|
-
rspec-core (~> 3.10)
|
167
|
-
rspec-expectations (~> 3.10)
|
168
|
-
rspec-mocks (~> 3.10)
|
169
|
-
rspec-support (~> 3.10)
|
170
|
-
rspec-support (3.11.0)
|
171
|
-
sqlite3 (1.4.2)
|
172
|
-
strscan (3.0.1)
|
173
|
-
thor (1.2.1)
|
174
|
-
timeout (0.2.0)
|
175
|
-
tzinfo (2.0.4)
|
176
|
-
concurrent-ruby (~> 1.0)
|
177
|
-
websocket-driver (0.7.5)
|
178
|
-
websocket-extensions (>= 0.1.0)
|
179
|
-
websocket-extensions (0.1.5)
|
180
|
-
zeitwerk (2.5.4)
|
181
|
-
|
182
|
-
PLATFORMS
|
183
|
-
ruby
|
184
|
-
|
185
|
-
DEPENDENCIES
|
186
|
-
pry-byebug
|
187
|
-
rails (~> 7.0)
|
188
|
-
rails_dictionary!
|
189
|
-
rspec-rails (= 5.1.0)
|
190
|
-
sqlite3
|
191
|
-
|
192
|
-
BUNDLED WITH
|
193
|
-
2.2.32
|
@@ -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,88 +0,0 @@
|
|
1
|
-
module RailsDictionary
|
2
|
-
module ActsAsDictSlave
|
3
|
-
def self.included(base)
|
4
|
-
base.extend(ClassMethods)
|
5
|
-
end
|
6
|
-
|
7
|
-
module ClassMethods
|
8
|
-
|
9
|
-
# return columns that exist in DictType#tab_and_column
|
10
|
-
def columns_in_dict_type
|
11
|
-
if ActiveRecord::VERSION::STRING < '3.1'
|
12
|
-
DictType.tab_and_column[self.name.underscore.to_sym]
|
13
|
-
elsif DictType.table_exists?
|
14
|
-
DictType.tab_and_column[self.name.underscore.to_sym]
|
15
|
-
else
|
16
|
-
[]
|
17
|
-
end
|
18
|
-
end
|
19
|
-
|
20
|
-
# columns which map to dictionary
|
21
|
-
def dict_columns(ops={})
|
22
|
-
conf = { except: nil, add: nil}
|
23
|
-
conf.update(ops)
|
24
|
-
cidt = columns_in_dict_type || []
|
25
|
-
cidt.delete(conf[:except])
|
26
|
-
case conf[:add]
|
27
|
-
when String
|
28
|
-
cidt.push(conf[:add])
|
29
|
-
when Array
|
30
|
-
cidt.push(*conf[:add])
|
31
|
-
else nil
|
32
|
-
end
|
33
|
-
cidt.uniq! || cidt
|
34
|
-
end
|
35
|
-
|
36
|
-
# add a belongs_to(Dictionary) association and a named_{column} method
|
37
|
-
def add_dynamic_column_method
|
38
|
-
dict_mapping_columns.each { |e| belongs_to "#{e}_dict".to_sym, class_name: "Dictionary", foreign_key: e.to_sym }
|
39
|
-
dict_mapping_columns.each { |ele| named_dict_value ele.to_sym }
|
40
|
-
dict_mapping_columns.each { |ele| dict_name_equal ele.to_sym }
|
41
|
-
end
|
42
|
-
|
43
|
-
# Generate dynamic instance method named_column to consumer model
|
44
|
-
# def named_city(locale=nil)
|
45
|
-
# locale = locale.presence || default_dict_locale.presence || :en
|
46
|
-
# locale = "name_#{locale}"
|
47
|
-
# self.send(city_dict).try(:send,locale)
|
48
|
-
# end
|
49
|
-
# alias_method :city_name, :named_city
|
50
|
-
def named_dict_value(method_name)
|
51
|
-
belongs_to_name="#{method_name}_dict".to_sym
|
52
|
-
origin_method_name = method_name
|
53
|
-
method_name="named_#{method_name}"
|
54
|
-
define_method(method_name) do | locale=nil |
|
55
|
-
locale = locale.presence || default_dict_locale.presence || :en
|
56
|
-
locale = "name_#{locale}"
|
57
|
-
self.send(belongs_to_name).try(:send,locale)
|
58
|
-
end
|
59
|
-
alias_method "#{origin_method_name}_name".to_sym, method_name.to_sym
|
60
|
-
end
|
61
|
-
|
62
|
-
# Build dynamic method column_name= to the consumer model
|
63
|
-
#
|
64
|
-
# def city_name=(value, options = {})
|
65
|
-
#
|
66
|
-
#
|
67
|
-
# end
|
68
|
-
def dict_name_equal(colname)
|
69
|
-
method_name = "#{colname}_name="
|
70
|
-
belongs_to_name="#{colname}_dict".to_sym
|
71
|
-
define_method(method_name) do |value, options={}|
|
72
|
-
options.merge!(name_en: value)
|
73
|
-
dict_type_id = DictType.revert("#{self.class.table_name.singularize}_#{colname}")
|
74
|
-
options.merge!(dict_type_id: dict_type_id)
|
75
|
-
exist_dictionary = Dictionary.where(options)
|
76
|
-
if exist_dictionary.present?
|
77
|
-
exist_id = exist_dictionary.first.id
|
78
|
-
else
|
79
|
-
exist_id = send("create_#{belongs_to_name}!", options).id
|
80
|
-
end
|
81
|
-
send "#{colname}=", exist_id
|
82
|
-
end
|
83
|
-
end
|
84
|
-
|
85
|
-
end # END ClassMethods
|
86
|
-
|
87
|
-
end
|
88
|
-
end
|
@@ -1,56 +0,0 @@
|
|
1
|
-
module RailsDictionary
|
2
|
-
module ActsAsDictType
|
3
|
-
def self.included(base)
|
4
|
-
base.extend(ClassMethods)
|
5
|
-
base.send :include, InstanceMethods
|
6
|
-
base.mattr_accessor :whole_types
|
7
|
-
end
|
8
|
-
|
9
|
-
module ClassMethods
|
10
|
-
|
11
|
-
def all_types
|
12
|
-
whole_types ||
|
13
|
-
self.whole_types = all.map(&:name).map(&:to_sym)
|
14
|
-
end
|
15
|
-
|
16
|
-
# short method to transfer id to name or name to id
|
17
|
-
# TODO: cache it
|
18
|
-
def revert(arg)
|
19
|
-
if arg.is_a?(String)
|
20
|
-
DictType.where(name: arg).first.try(:id)
|
21
|
-
elsif arg.is_a?(Integer)
|
22
|
-
DictType.where(id: arg).first.try(:name)
|
23
|
-
end
|
24
|
-
end
|
25
|
-
|
26
|
-
#
|
27
|
-
# Parse the name value to get which column and model(or table) are listed in DictType
|
28
|
-
#
|
29
|
-
# Programmer DOC:
|
30
|
-
# There are two chooses to get subclass,one is subclasses the other is descendants,
|
31
|
-
# I don't know which is better,but descendants contains subclass of subclass,it contains more.
|
32
|
-
#
|
33
|
-
# Class like +Ckeditor::Asset+ transfer to "ckeditor/asset",but we can not naming method like that,
|
34
|
-
# So it still not support, the solution may be simple,just make another convention to escape "/"
|
35
|
-
#
|
36
|
-
# Seems this method did not need to be cached in production.
|
37
|
-
# Because everyclass was cached before application was run.So after application was run, it never be run again.
|
38
|
-
# TODO:
|
39
|
-
# To cache this method output need more skills on how to caculate ActiveRecord::Base.descendants
|
40
|
-
# Temply remove the cache
|
41
|
-
# And add test for this situation
|
42
|
-
def tab_and_column
|
43
|
-
all_model_class=ActiveRecord::Base.descendants.map(&:name).map(&:underscore)
|
44
|
-
all_types.map(&:to_s).extract_to_hash(all_model_class)
|
45
|
-
end
|
46
|
-
end
|
47
|
-
|
48
|
-
module InstanceMethods
|
49
|
-
def delete_all_caches
|
50
|
-
self.whole_types = nil
|
51
|
-
return true
|
52
|
-
end
|
53
|
-
end
|
54
|
-
|
55
|
-
end
|
56
|
-
end
|
@@ -1,86 +0,0 @@
|
|
1
|
-
module RailsDictionary
|
2
|
-
module ActsAsDictionary
|
3
|
-
def self.included(base)
|
4
|
-
base.extend(ClassMethods)
|
5
|
-
base.send :include,InstanceMethods
|
6
|
-
end
|
7
|
-
|
8
|
-
module ClassMethods
|
9
|
-
|
10
|
-
# For rails3
|
11
|
-
# I thought it would be better to define a method in method_missing, Not just generate cache.
|
12
|
-
# Cause cache can not store ActiveRecord
|
13
|
-
# Generate methods like Dictionary.student_city
|
14
|
-
# Dictionary.student_city - a list of dictionary object which dict type is student_city
|
15
|
-
# Dictionary.student_city(:locale => :zh) - a select format array which can be used
|
16
|
-
# in view method select as choice params
|
17
|
-
# Programmer DOC && TODO:
|
18
|
-
# rethink about the cache.
|
19
|
-
# cache methods like Dictionary.student_city(:locale => :zh,:sort => :name_fr)
|
20
|
-
# but not cache Dictionary.student_city, return it as relation
|
21
|
-
#
|
22
|
-
# Remove nil noise,if listed_attr =[[nil, 201], [nil, 203], [nil, 202], ["Sciences", 200]]
|
23
|
-
# the sort would be failed of ArgumentError: comparison of Array with Array failed
|
24
|
-
# split this method ,make it more short and maintainance
|
25
|
-
def method_missing(method_id,options={})
|
26
|
-
if ::DictType.all_types.include? method_id
|
27
|
-
method_name=method_id.to_s.downcase
|
28
|
-
# TODO: If cache engine is failed, then the code will failed with null cant dup
|
29
|
-
Rails.cache.fetch("Dictionary.#{method_name}") { dict_type_name_eq(method_name).to_a }
|
30
|
-
listed_attr=Rails.cache.read("Dictionary.#{method_name}").dup # Instance of ActiveRecord::Relation can not be dup?
|
31
|
-
build_scope_method(method_id)
|
32
|
-
if options.keys.include? :locale or options.keys.include? "locale"
|
33
|
-
locale="name_#{ options[:locale] }"
|
34
|
-
sort_block=sort_dicts(options)
|
35
|
-
listed_attr.sort!(&sort_block) if sort_block
|
36
|
-
listed_attr.map! { |a| [a.send(locale),a.id] }.reject! {|ele| ele.first.nil?}
|
37
|
-
end
|
38
|
-
listed_attr
|
39
|
-
else
|
40
|
-
super
|
41
|
-
end
|
42
|
-
end
|
43
|
-
|
44
|
-
# Override this method to get customed sort block
|
45
|
-
def sort_dicts(options)
|
46
|
-
if options.keys.include? :locale or options.keys.include? "locale"
|
47
|
-
locale="name_#{ options[:locale] }"
|
48
|
-
if options[:locale].to_sym == :zh
|
49
|
-
Proc.new { |a,b| a.send(locale).encode('GBK') <=> b.send(locale).encode('GBK') }
|
50
|
-
else
|
51
|
-
Proc.new { |a,b| a.send(locale).downcase <=> b.send(locale).downcase }
|
52
|
-
end
|
53
|
-
else
|
54
|
-
false
|
55
|
-
end
|
56
|
-
end
|
57
|
-
|
58
|
-
def respond_to?(name, include_private=false)
|
59
|
-
::DictType.all_types.include?(name) || super
|
60
|
-
end
|
61
|
-
|
62
|
-
private
|
63
|
-
|
64
|
-
def build_scope_method(name)
|
65
|
-
scope_method_name = "scoped_#{name}".to_sym
|
66
|
-
unless respond_to? scope_method_name
|
67
|
-
define_singleton_method scope_method_name do
|
68
|
-
# see http://stackoverflow.com/questions/18198963/with-rails-4-model-scoped-is-deprecated-but-model-all-cant-replace-it
|
69
|
-
# for usage of where(nil)
|
70
|
-
dict_type_name_eq(name).where(nil)
|
71
|
-
end
|
72
|
-
end
|
73
|
-
end
|
74
|
-
|
75
|
-
end # End ClassMethods
|
76
|
-
|
77
|
-
module InstanceMethods
|
78
|
-
def delete_dicts_cache
|
79
|
-
method_name = ::DictType.revert(self.dict_type_id)
|
80
|
-
Rails.cache.delete("Dictionary.#{method_name}")
|
81
|
-
return true
|
82
|
-
end
|
83
|
-
end # End InstanceMethods
|
84
|
-
|
85
|
-
end
|
86
|
-
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,41 +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
|
-
# models
|
17
|
-
class DictType < ActiveRecord::Base
|
18
|
-
acts_as_dict_type
|
19
|
-
end
|
20
|
-
|
21
|
-
class Dictionary < ActiveRecord::Base
|
22
|
-
acts_as_dictionary
|
23
|
-
end
|
24
|
-
|
25
|
-
class Student < ActiveRecord::Base
|
26
|
-
end
|
27
|
-
|
28
|
-
#migrations
|
29
|
-
class CreateAllTables < ActiveRecord::VERSION::MAJOR >= 5 ? ActiveRecord::Migration[5.0] : ActiveRecord::Migration
|
30
|
-
def self.up
|
31
|
-
create_table(:dict_types) {|t| t.string :name}
|
32
|
-
create_table(:dictionaries) {|t| t.string :name_en; t.string :name_zh ; t.string :name_fr ; t.integer :dict_type_id}
|
33
|
-
create_table(:students) {|t| t.string :email; t.integer :city; t.integer :school}
|
34
|
-
end
|
35
|
-
|
36
|
-
def self.down
|
37
|
-
drop_table :dict_types
|
38
|
-
drop_table :dictionaries
|
39
|
-
drop_table :students
|
40
|
-
end
|
41
|
-
end
|