rails_dictionary 0.0.4 → 0.0.5
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/README.rdoc
CHANGED
@@ -14,7 +14,9 @@ In this usage sample,there are three tables.
|
|
14
14
|
=== Table Definition
|
15
15
|
Make sure you have two tables which named as dict_types and dictionaries.
|
16
16
|
For version 0.0.1,I'm not going to add more configurations.
|
17
|
+
Table dictionaries has one conventions on column naming: name_locale.So the name_fr means this column have a french value,you can see more usage later.
|
17
18
|
The students table is not required.
|
19
|
+
|
18
20
|
create_table :dict_types do |t|
|
19
21
|
t.string :name
|
20
22
|
t.string :comment
|
@@ -90,9 +92,9 @@ Student has 2 belongs_to assocition which named as city_dict and school_dict,the
|
|
90
92
|
If you start a new application and there are more than 10 kinds of static data,you may have a try with the gem.
|
91
93
|
However,if you see many static data in an old system and want to refactor it,the decision would be judged by the real situations.
|
92
94
|
|
93
|
-
=
|
95
|
+
= Beware
|
94
96
|
The most used debug method would be DictType.all_types and Dictionary.student_city(or other dynamic generate method)
|
95
|
-
When you get some confused with these method,try running
|
97
|
+
When you get some confused with the output of these method,try running
|
96
98
|
rails tmp:clear
|
97
99
|
cause these methods all return static data,I just caches these output for better performance.If you change db data in db console(not through Rails) like running
|
98
100
|
delete from dict_types;
|
@@ -100,7 +102,8 @@ The rails cache would not refresh.
|
|
100
102
|
In short,when you confused with the debug data,try running "rails tmp:clear" first.
|
101
103
|
|
102
104
|
= TODO & Problems
|
103
|
-
|
105
|
+
There are no convention and implemention to map Class like Ckeditor::Asset to a method.
|
106
|
+
Add generators.
|
104
107
|
|
105
108
|
= Help
|
106
109
|
Need an friendly english native guy to help me improve this readme to make it more clear and readable.
|
@@ -4,11 +4,15 @@ module ActsAsDictSlave
|
|
4
4
|
end
|
5
5
|
|
6
6
|
module ClassMethods
|
7
|
-
#
|
8
|
-
#
|
7
|
+
# :except - remove dict mapping column
|
8
|
+
# :add - add dict mapping column
|
9
|
+
# :locale - add and initialize class attribute default_dict_locale
|
9
10
|
def acts_as_dict_slave(ops={})
|
10
|
-
|
11
|
-
|
11
|
+
class_attribute :default_dict_locale,:instance_writer => false
|
12
|
+
cattr_accessor :dict_mapping_columns,:instance_writes => false
|
13
|
+
self.default_dict_locale = ops[:locale] if ops[:locale]
|
14
|
+
self.dict_mapping_columns = dict_columns(ops)
|
15
|
+
unless dict_mapping_columns.nil?
|
12
16
|
add_dynamic_column_method
|
13
17
|
end
|
14
18
|
end
|
@@ -37,19 +41,24 @@ module ActsAsDictSlave
|
|
37
41
|
# add a belongs_to(Dictionary) association and a named_{column} method
|
38
42
|
def add_dynamic_column_method
|
39
43
|
self.extend(DynamicInsMethods)
|
40
|
-
|
41
|
-
|
44
|
+
dict_mapping_columns.each { |e| belongs_to "#{e.to_s}_dict".to_sym,class_name: "Dictionary",foreign_key: e }
|
45
|
+
dict_mapping_columns.each { |ele| named_dict_value ele.to_sym }
|
42
46
|
end
|
43
47
|
end
|
44
48
|
|
45
49
|
module DynamicInsMethods
|
46
50
|
# generate dynamic instance method named_column to slave model
|
51
|
+
# def named_city(locale=nil)
|
52
|
+
# locale = locale.presence || default_dict_locale.presence || :en
|
53
|
+
# locale = "name_#{locale}"
|
54
|
+
# self.send(city_dict).try(:send,locale)
|
55
|
+
# end
|
47
56
|
def named_dict_value(method_name)
|
48
57
|
belongs_to_name="#{method_name.to_s}_dict".to_sym
|
49
58
|
method_name="named_#{method_name.to_s}"
|
50
|
-
define_method(method_name) do | locale
|
51
|
-
|
52
|
-
locale ="name_#{locale}"
|
59
|
+
define_method(method_name) do | locale=nil |
|
60
|
+
locale = locale.presence || default_dict_locale.presence || :en
|
61
|
+
locale = "name_#{locale}"
|
53
62
|
self.send(belongs_to_name).try(:send,locale)
|
54
63
|
end
|
55
64
|
end
|
@@ -32,16 +32,13 @@ module ActsAsDictType
|
|
32
32
|
# TODO: get a more accurate method name
|
33
33
|
# parse the name to get which column and model are listed in DictType
|
34
34
|
def self.tab_and_column
|
35
|
-
|
36
|
-
|
37
|
-
#
|
38
|
-
#
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
all_class=all_tabs.map(&:singularize)
|
43
|
-
all_tabs=all_class
|
44
|
-
@tab_and_column=@all_types.extract_to_hash(all_tabs)
|
35
|
+
tab_and_column={}
|
36
|
+
# There are two chooses,one is subclasses the other is descendants,
|
37
|
+
# I don't know which is better,but descendants contains subclass of subclass,it contains more.
|
38
|
+
# Class like +Ckeditor::Asset+ transfer to "ckeditor/asset",but we can not naming method like that,
|
39
|
+
# So it still not support, the solution may be simple,just make another convention to escape "/"
|
40
|
+
all_model_class=ActiveRecord::Base.descendants.map(&:name).map(&:underscore)
|
41
|
+
tab_and_column=all_types.extract_to_hash(all_model_class)
|
45
42
|
end
|
46
43
|
end
|
47
44
|
end
|
@@ -4,31 +4,33 @@ module ActsAsDictionary
|
|
4
4
|
end
|
5
5
|
|
6
6
|
module ClassMethods
|
7
|
-
def acts_as_dictionary
|
8
|
-
self.class_eval do
|
9
|
-
include InstanceMethods
|
10
|
-
belongs_to :dict_type
|
11
|
-
after_save :delete_dicts_cache
|
12
|
-
after_destroy :delete_dicts_cache
|
13
7
|
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
8
|
+
def acts_as_dictionary
|
9
|
+
belongs_to :dict_type
|
10
|
+
after_save :delete_dicts_cache
|
11
|
+
after_destroy :delete_dicts_cache
|
12
|
+
scope :dict_type_name_eq,lambda {|method_name| joins(:dict_type).where("dict_types.name" => method_name).all}
|
13
|
+
include InstanceMethods
|
14
|
+
# Generate methods like Dictionary.student_city
|
15
|
+
# Dictionary.student_city - a list of dictionary object which dict type is student_city
|
16
|
+
# Dictionary.student_city(:locale => :zh) - a select format array which can be used
|
17
|
+
# in view method select as choice params
|
18
|
+
# Is this design good?
|
19
|
+
def self.method_missing(method_id,options={},&block)
|
20
|
+
method_name=method_id.to_s.downcase
|
21
|
+
if DictType.all_types.include? method_id.to_s
|
22
|
+
Rails.cache.fetch("Dictionary.#{method_name}") { Dictionary.dict_type_name_eq(method_name) }
|
23
|
+
listed_attr=Rails.cache.read("Dictionary.#{method_name}").dup
|
24
|
+
if options.keys.include? :locale or options.keys.include? "locale"
|
25
|
+
locale="name_#{ options[:locale] }"
|
26
|
+
listed_attr.map! { |a| [a.send(locale),a.id] }
|
27
|
+
listed_attr.sort {|a,b| a.last <=> b.last } # maybe remove this line
|
27
28
|
else
|
28
|
-
|
29
|
+
listed_attr
|
29
30
|
end
|
31
|
+
else
|
32
|
+
super
|
30
33
|
end
|
31
|
-
|
32
34
|
end
|
33
35
|
|
34
36
|
end
|
@@ -2,7 +2,7 @@
|
|
2
2
|
require "test/unit"
|
3
3
|
require "active_support"
|
4
4
|
require "active_record"
|
5
|
-
require "ruby-debug"
|
5
|
+
require "ruby-debug" # coupled with debugger to debug code
|
6
6
|
Object.const_set "RAILS_CACHE", ActiveSupport::Cache.lookup_store
|
7
7
|
require "active_support/cache"
|
8
8
|
require "rails"
|
@@ -73,8 +73,6 @@ class DictTypeTest < Test::Unit::TestCase
|
|
73
73
|
@dy_beijing=Dictionary.create! name_en: "beijing",name_zh: "北京",name_fr: "Pékin",dict_type_id: @dt_stu_city.id
|
74
74
|
@stu_beijing=Student.create! email: "beijing@dict.com",city: @dy_beijing.id
|
75
75
|
@stu_shanghai=Student.create! email: "shanghai@dict.com",city: @dy_shanghai.id
|
76
|
-
Student.acts_as_dict_slave
|
77
|
-
# the acts_as_dict_slave need real data to generate dynamic method
|
78
76
|
end
|
79
77
|
|
80
78
|
def teardown
|
@@ -105,12 +103,20 @@ class DictTypeTest < Test::Unit::TestCase
|
|
105
103
|
|
106
104
|
# test dynamic instance methods in slave model
|
107
105
|
def test_named_city
|
106
|
+
Student.acts_as_dict_slave
|
107
|
+
# the acts_as_dict_slave need real data to generate dynamic method
|
108
108
|
assert_equal %w[city school],Student.columns_in_dict_type
|
109
109
|
assert_equal %w[city school],Student.dict_columns
|
110
110
|
assert_equal "shanghai",@stu_shanghai.named_city(:en)
|
111
|
+
assert_equal "shanghai",@stu_shanghai.named_city("")
|
111
112
|
assert_equal "Pékin",@stu_beijing.named_city(:fr)
|
112
113
|
end
|
113
114
|
|
115
|
+
def test_named_city_with_default_locale
|
116
|
+
Student.acts_as_dict_slave :locale => :fr
|
117
|
+
assert_equal "Pékin",@stu_beijing.named_city
|
118
|
+
end
|
119
|
+
|
114
120
|
def test_delete_dicts_cache
|
115
121
|
@dy_wuhan=Dictionary.create! name_en: "wuhan",name_zh: "武汉",name_fr: "wuhan",dict_type_id: @dt_stu_city.id
|
116
122
|
assert_equal [["shanghai", 1], ["beijing", 2], ["wuhan", 3]],Dictionary.student_city(:locale => :en)
|
metadata
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
name: rails_dictionary
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version: 0.0.
|
5
|
+
version: 0.0.5
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- raykin
|
@@ -10,7 +10,7 @@ autorequire:
|
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
12
|
|
13
|
-
date: 2011-03-
|
13
|
+
date: 2011-03-07 00:00:00 +08:00
|
14
14
|
default_executable:
|
15
15
|
dependencies: []
|
16
16
|
|
@@ -59,7 +59,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
59
59
|
requirements: []
|
60
60
|
|
61
61
|
rubyforge_project: rails_dictionary
|
62
|
-
rubygems_version: 1.
|
62
|
+
rubygems_version: 1.6.1
|
63
63
|
signing_key:
|
64
64
|
specification_version: 3
|
65
65
|
summary: dictionary data for application
|