rails_dictionary 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +8 -4
- data/lib/rails_dictionary/array_core_ext.rb +1 -1
- data/lib/rails_dictionary/models/active_record_extension.rb +4 -2
- data/lib/rails_dictionary/models/acts_as_dict_slave.rb +2 -2
- data/lib/rails_dictionary/models/acts_as_dict_type.rb +7 -7
- data/lib/rails_dictionary/models/acts_as_dictionary.rb +23 -18
- data/lib/rails_dictionary/version.rb +1 -1
- data/spec/rails_dictionary_spec.rb +6 -0
- metadata +6 -6
data/README.rdoc
CHANGED
@@ -54,7 +54,7 @@ These task are just generate table dictionaries,dict_types,students and some sam
|
|
54
54
|
| 2 | shanghai@dict.com | 3 | |
|
55
55
|
+----+-------------------+------+--------+
|
56
56
|
2 rows in set
|
57
|
-
There is one convention
|
57
|
+
There is one convention on DictType.name .All value of DictType.name is "model_method" : student is model and city is method of student model.
|
58
58
|
|
59
59
|
=== Table Definition
|
60
60
|
Make sure you have two tables which named as dict_types and dictionaries.
|
@@ -81,11 +81,13 @@ Here is what should be like.Student model can be other models.
|
|
81
81
|
DictType.all_cached #=> return cache of DictType.all
|
82
82
|
DictType.all_types = [:student_city,:student_school] # also cached
|
83
83
|
Dictionary.student_city #=> [Dictionary.find(5),Dictionary.find(6)]
|
84
|
-
student_city is a dynamic method which returns a list of dictionary object which dict_type is "student_city".
|
84
|
+
student_city is a dynamic method(from method missing) which returns a list of dictionary object which dict_type is "student_city".
|
85
85
|
Actually Dictionary will have as many dynamic methods as DictType.count and each dynamic method name is DictType.name.
|
86
|
-
|
86
|
+
And student_city return an array,not ActiveRelation.So
|
87
87
|
Dictionary.student_school = []
|
88
88
|
Dictionary.student_city :locale => :en #=> [["beijing", 2],["shanghai",1]]
|
89
|
+
If you need a ActiveRelation, try scoped_student_city like
|
90
|
+
Dictionary.scoped_student_city.where(...)
|
89
91
|
You can use it in form select method like
|
90
92
|
collection_select :student,:city,Dictionary.student_city,:id,:name_en
|
91
93
|
select :student,:city,Dictionary.student_city(params)
|
@@ -121,7 +123,9 @@ The rails cache would not refresh.
|
|
121
123
|
In short,when you confused with the debug data,try running "rails tmp:clear" first.
|
122
124
|
|
123
125
|
= TODO & Problems
|
124
|
-
|
126
|
+
Remove engine. Becase for the view layer we can use gem rails_admin. so this gem did not need rails engine.
|
127
|
+
Make Dictionary.student_city return a scope.
|
128
|
+
Is there any exist low level method to monitor the change of descendents?
|
125
129
|
Add test code for cache DictType.tab_and_column,then uncomment the cache code.
|
126
130
|
Remove gems/rails_dictionary-0.0.9.4/app/views from rails view path
|
127
131
|
|
@@ -22,17 +22,19 @@ module RailsDictionary
|
|
22
22
|
belongs_to :dict_type
|
23
23
|
after_save :delete_dicts_cache
|
24
24
|
after_destroy :delete_dicts_cache
|
25
|
+
scope :dict_type_name_eq, lambda { |name| joins(:dict_type).where("dict_types.name" => name) }
|
25
26
|
|
26
27
|
include RailsDictionary::ActsAsDictionary
|
27
28
|
end
|
28
29
|
|
30
|
+
# Ex: acts_as_dict_slave :add => :category
|
29
31
|
# :except - remove dict mapping column
|
30
32
|
# :add - add dict mapping column
|
31
33
|
# :locale - add and initialize class attribute default_dict_locale
|
32
34
|
def acts_as_dict_slave(ops={})
|
33
35
|
include RailsDictionary::ActsAsDictSlave
|
34
|
-
class_attribute :default_dict_locale
|
35
|
-
cattr_accessor :dict_mapping_columns
|
36
|
+
class_attribute :default_dict_locale, :instance_writer => false
|
37
|
+
cattr_accessor :dict_mapping_columns, :instance_writes => false
|
36
38
|
self.default_dict_locale = ops[:locale] if ops[:locale]
|
37
39
|
self.dict_mapping_columns = dict_columns(ops)
|
38
40
|
unless dict_mapping_columns.nil?
|
@@ -19,9 +19,9 @@ module RailsDictionary
|
|
19
19
|
|
20
20
|
# columns which map to dictionary
|
21
21
|
def dict_columns(ops={})
|
22
|
-
conf={except: nil,add: nil}
|
22
|
+
conf = { except: nil, add: nil}
|
23
23
|
conf.update(ops)
|
24
|
-
cidt=
|
24
|
+
cidt = columns_in_dict_type || []
|
25
25
|
cidt.delete(conf[:except])
|
26
26
|
case conf[:add]
|
27
27
|
when String
|
@@ -24,24 +24,25 @@ module RailsDictionary
|
|
24
24
|
end
|
25
25
|
end
|
26
26
|
|
27
|
-
# TODO: get a more accurate method name
|
28
27
|
#
|
29
|
-
# Parse the name value to get which column and model are listed in DictType
|
28
|
+
# Parse the name value to get which column and model(or table) are listed in DictType
|
30
29
|
#
|
31
30
|
# Programmer DOC:
|
32
31
|
# There are two chooses to get subclass,one is subclasses the other is descendants,
|
33
32
|
# I don't know which is better,but descendants contains subclass of subclass,it contains more.
|
33
|
+
#
|
34
34
|
# Class like +Ckeditor::Asset+ transfer to "ckeditor/asset",but we can not naming method like that,
|
35
35
|
# So it still not support, the solution may be simple,just make another convention to escape "/"
|
36
|
+
#
|
37
|
+
# Seems this method did not need to be cached in production.
|
38
|
+
# Because everyclass was cached before application was run.So after application was run, it never be run again.
|
36
39
|
# TODO:
|
37
40
|
# To cache this method output need more skills on how to caculate ActiveRecord::Base.descendants
|
38
41
|
# Temply remove the cache
|
39
42
|
# And add test for this situation
|
40
43
|
def tab_and_column
|
41
|
-
|
42
|
-
|
43
|
-
all_types.map(&:to_s).extract_to_hash(all_model_class)
|
44
|
-
#end
|
44
|
+
all_model_class=ActiveRecord::Base.descendants.map(&:name).map(&:underscore)
|
45
|
+
all_types.map(&:to_s).extract_to_hash(all_model_class)
|
45
46
|
end
|
46
47
|
end
|
47
48
|
|
@@ -49,7 +50,6 @@ module RailsDictionary
|
|
49
50
|
def delete_all_caches
|
50
51
|
Rails.cache.delete("DictType.all_types")
|
51
52
|
Rails.cache.delete("DictType.cached_all")
|
52
|
-
#Rails.cache.delete("DictType.tab_and_column")
|
53
53
|
return true
|
54
54
|
end
|
55
55
|
end
|
@@ -7,16 +7,9 @@ module RailsDictionary
|
|
7
7
|
|
8
8
|
module ClassMethods
|
9
9
|
|
10
|
-
#
|
11
|
-
#
|
12
|
-
#
|
13
|
-
# scope :dict_type_name_eq,lambda {|method_name| joins(:dict_type).where("dict_types.name" => method_name)}
|
14
|
-
# remove the all seems not pass test,return following errors
|
15
|
-
# TypeError: no marshal_dump is defined for class SQLite3::Database
|
16
|
-
def dict_type_name_eq(method_name)
|
17
|
-
joins(:dict_type).where("dict_types.name" => method_name).all
|
18
|
-
end
|
19
|
-
|
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
|
20
13
|
# Generate methods like Dictionary.student_city
|
21
14
|
# Dictionary.student_city - a list of dictionary object which dict type is student_city
|
22
15
|
# Dictionary.student_city(:locale => :zh) - a select format array which can be used
|
@@ -24,17 +17,17 @@ module RailsDictionary
|
|
24
17
|
# Programmer DOC && TODO:
|
25
18
|
# rethink about the cache.
|
26
19
|
# cache methods like Dictionary.student_city(:locale => :zh,:sort => :name_fr)
|
27
|
-
# but not cache Dictionary.student_city,return it as relation
|
20
|
+
# but not cache Dictionary.student_city, return it as relation
|
28
21
|
#
|
29
22
|
# Remove nil noise,if listed_attr =[[nil, 201], [nil, 203], [nil, 202], ["Sciences", 200]]
|
30
23
|
# the sort would be failed of ArgumentError: comparison of Array with Array failed
|
31
24
|
# split this method ,make it more short and maintainance
|
32
25
|
def method_missing(method_id,options={})
|
33
|
-
method_name=method_id.to_s.downcase
|
34
26
|
if DictType.all_types.include? method_id
|
35
|
-
|
36
|
-
|
37
|
-
# Instance of
|
27
|
+
method_name=method_id.to_s.downcase
|
28
|
+
Rails.cache.fetch("Dictionary.#{method_name}") { dict_type_name_eq(method_name).all }
|
29
|
+
listed_attr=Rails.cache.read("Dictionary.#{method_name}").dup # Instance of ActiveRecord::Relation can not be dup?
|
30
|
+
build_scope_method(method_id)
|
38
31
|
if options.keys.include? :locale or options.keys.include? "locale"
|
39
32
|
locale="name_#{ options[:locale] }"
|
40
33
|
sort_block=sort_dicts(options)
|
@@ -47,7 +40,7 @@ module RailsDictionary
|
|
47
40
|
end
|
48
41
|
end
|
49
42
|
|
50
|
-
#
|
43
|
+
# Override this method to get customed sort block
|
51
44
|
def sort_dicts(options)
|
52
45
|
if options.keys.include? :locale or options.keys.include? "locale"
|
53
46
|
locale="name_#{ options[:locale] }"
|
@@ -62,7 +55,18 @@ module RailsDictionary
|
|
62
55
|
end
|
63
56
|
end
|
64
57
|
|
65
|
-
|
58
|
+
private
|
59
|
+
|
60
|
+
def build_scope_method(name)
|
61
|
+
scope_method_name = "scoped_#{name}".to_sym
|
62
|
+
unless respond_to? scope_method_name
|
63
|
+
define_singleton_method scope_method_name do
|
64
|
+
dict_type_name_eq(name).scoped
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
end # End ClassMethods
|
66
70
|
|
67
71
|
module InstanceMethods
|
68
72
|
def delete_dicts_cache
|
@@ -70,6 +74,7 @@ module RailsDictionary
|
|
70
74
|
Rails.cache.delete("Dictionary.#{method_name}")
|
71
75
|
return true
|
72
76
|
end
|
73
|
-
end
|
77
|
+
end # End InstanceMethods
|
78
|
+
|
74
79
|
end
|
75
80
|
end
|
@@ -48,12 +48,18 @@ describe RailsDictionary do
|
|
48
48
|
Dictionary.student_city(:locale => :zh).should == [["北京", 2],["上海",1]]
|
49
49
|
end
|
50
50
|
|
51
|
+
it "build scope method scoped_student_city" do
|
52
|
+
Dictionary.scoped_student_city.class.name.should == "ActiveRecord::Relation"
|
53
|
+
Dictionary.scoped_student_city.where(:id => 1).should == [dy_shanghai]
|
54
|
+
end
|
55
|
+
|
51
56
|
it "after record added or removed" do
|
52
57
|
@dy_wuhan=Dictionary.create! name_en: "wuhan",name_zh: "武汉",name_fr: "wuhan",dict_type_id: dt_stu_city.id
|
53
58
|
Dictionary.student_city(:locale => :en).should == [["beijing", 2],["shanghai",1],["wuhan", 3]]
|
54
59
|
@dy_wuhan.destroy
|
55
60
|
Dictionary.student_city(:locale => :en).should == [["beijing", 2],["shanghai",1]]
|
56
61
|
end
|
62
|
+
|
57
63
|
end
|
58
64
|
|
59
65
|
describe Student do
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rails_dictionary
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2011-12-
|
12
|
+
date: 2011-12-28 00:00:00.000000000Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rails
|
16
|
-
requirement: &
|
16
|
+
requirement: &11331140 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>'
|
@@ -21,10 +21,10 @@ dependencies:
|
|
21
21
|
version: '3.0'
|
22
22
|
type: :runtime
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *11331140
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: rails
|
27
|
-
requirement: &
|
27
|
+
requirement: &11327380 !ruby/object:Gem::Requirement
|
28
28
|
none: false
|
29
29
|
requirements:
|
30
30
|
- - ! '>'
|
@@ -32,7 +32,7 @@ dependencies:
|
|
32
32
|
version: '3.0'
|
33
33
|
type: :development
|
34
34
|
prerelease: false
|
35
|
-
version_requirements: *
|
35
|
+
version_requirements: *11327380
|
36
36
|
description: Rails plugin for mapping static data of web application to Dictionary
|
37
37
|
class
|
38
38
|
email:
|