rails_dictionary 0.0.9.3 → 0.0.9.4
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +13 -5
- data/lib/rails_dictionary/models/active_record_extension.rb +11 -11
- data/lib/rails_dictionary/models/acts_as_dictionary.rb +19 -4
- data/lib/rails_dictionary/railtie.rb +1 -0
- data/lib/rails_dictionary/version.rb +1 -1
- data/spec/rails_dictionary_spec.rb +1 -1
- metadata +2 -4
- data/test/rails_dictionary_test.rb +0 -137
data/README.rdoc
CHANGED
@@ -14,6 +14,8 @@ or
|
|
14
14
|
rails runner "DictType.new.delete_all_caches"
|
15
15
|
and in production
|
16
16
|
rails runner "DictType.new.delete_all_caches" -e production
|
17
|
+
or if you use file cache,just run
|
18
|
+
rake tmp:clear
|
17
19
|
See change log for brief info.
|
18
20
|
|
19
21
|
== Sample
|
@@ -81,7 +83,7 @@ student_city is a dynamic method which returns a list of dictionary object which
|
|
81
83
|
Actually Dictionary will have as many dynamic methods as DictType.count and each dynamic method name is DictType.name.
|
82
84
|
Before version 0.1.0 or 0.2.0 ,all dynamic methods return an array,not ActiveRelation.So
|
83
85
|
Dictionary.student_school = []
|
84
|
-
Dictionary.student_city :locale => :en #=> [["
|
86
|
+
Dictionary.student_city :locale => :en #=> [["beijing", 2],["shanghai",1]]
|
85
87
|
You can use it in form select method like
|
86
88
|
collection_select :student,:city,Dictionary.student_city,:id,:name_en
|
87
89
|
select :student,:city,Dictionary.student_city(params)
|
@@ -96,6 +98,13 @@ Make sure your locale is en,not en-US.
|
|
96
98
|
Student has two belongs_to assocition which named as city_dict and school_dict,the naming convention is method_dict.
|
97
99
|
Student.find(1).city_dict #=> Dictionary.find(6)
|
98
100
|
|
101
|
+
=== Sort Feature
|
102
|
+
Static data need orders frequently,so Dictionary.student_city :locale => :en has a default sort rules.
|
103
|
+
By default,if the options contains locale,the results are sorted by the name value.
|
104
|
+
If locale is :zh,sort rule is order by GBK encoding.
|
105
|
+
Other locales are just order by alphabetical without case sensitive.
|
106
|
+
You can override Dictionary.sort_dicts to customize your sort rule.But it is not recommended now as the code of sort design maybe change in a few month.
|
107
|
+
|
99
108
|
= Practical Suggestion
|
100
109
|
If you start a new application and there are more than 10 kinds of static data,you may have a try with the gem.
|
101
110
|
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.
|
@@ -110,8 +119,7 @@ The rails cache would not refresh.
|
|
110
119
|
In short,when you confused with the debug data,try running "rails tmp:clear" first.
|
111
120
|
|
112
121
|
= TODO & Problems
|
113
|
-
|
114
|
-
|
115
|
-
Rebuild core code.Using Rails engine.
|
116
|
-
|
122
|
+
Is there any exist low level method to monitor the change of descendents
|
117
123
|
Add test code for cache DictType.tab_and_column,then uncomment the cache code.
|
124
|
+
|
125
|
+
There are no convention and implemention to map Class like Ckeditor::Asset to a legal method name.
|
@@ -8,21 +8,21 @@ module RailsDictionary
|
|
8
8
|
# TODO: move macro define in each module file
|
9
9
|
# See Usage in readme.doc.
|
10
10
|
def acts_as_dict_type
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
11
|
+
|
12
|
+
has_many :dictionaries
|
13
|
+
validates_uniqueness_of :name
|
14
|
+
after_save :delete_all_caches
|
15
|
+
after_destroy :delete_all_caches
|
16
|
+
|
17
17
|
include RailsDictionary::ActsAsDictType
|
18
18
|
end
|
19
19
|
|
20
20
|
def acts_as_dictionary
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
21
|
+
|
22
|
+
belongs_to :dict_type
|
23
|
+
after_save :delete_dicts_cache
|
24
|
+
after_destroy :delete_dicts_cache
|
25
|
+
|
26
26
|
include RailsDictionary::ActsAsDictionary
|
27
27
|
end
|
28
28
|
|
@@ -37,16 +37,31 @@ module RailsDictionary
|
|
37
37
|
# Instance of activerelation can not be dup?
|
38
38
|
if options.keys.include? :locale or options.keys.include? "locale"
|
39
39
|
locale="name_#{ options[:locale] }"
|
40
|
+
sort_block=sort_dicts(options)
|
41
|
+
listed_attr.sort!(&sort_block) if sort_block
|
40
42
|
listed_attr.map! { |a| [a.send(locale),a.id] }.reject! {|ele| ele.first.nil?}
|
41
|
-
listed_attr.sort { |a,b| a.first.downcase <=> b.first.downcase }
|
42
|
-
# maybe remove the above line,or change some sorting and caching design
|
43
|
-
else
|
44
|
-
listed_attr
|
45
43
|
end
|
44
|
+
listed_attr
|
46
45
|
else
|
47
46
|
super
|
48
47
|
end
|
49
48
|
end
|
49
|
+
|
50
|
+
# overide this method to get customed sort block
|
51
|
+
def sort_dicts(options)
|
52
|
+
if options.keys.include? :locale or options.keys.include? "locale"
|
53
|
+
locale="name_#{ options[:locale] }"
|
54
|
+
if options[:locale].to_sym == :zh
|
55
|
+
conv = Iconv.new("GBK", "utf-8")
|
56
|
+
Proc.new { |a,b| conv.iconv(a.send(locale)) <=> conv.iconv(b.send(locale)) }
|
57
|
+
else
|
58
|
+
Proc.new { |a,b| a.send(locale).downcase <=> b.send(locale).downcase }
|
59
|
+
end
|
60
|
+
else
|
61
|
+
false
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
50
65
|
end
|
51
66
|
|
52
67
|
module InstanceMethods
|
@@ -45,7 +45,7 @@ describe RailsDictionary do
|
|
45
45
|
end
|
46
46
|
|
47
47
|
it "generate student_city method with locale" do
|
48
|
-
Dictionary.student_city(:locale => :zh).should == [["
|
48
|
+
Dictionary.student_city(:locale => :zh).should == [["北京", 2],["上海",1]]
|
49
49
|
end
|
50
50
|
|
51
51
|
it "after record added or removed" do
|
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.9.
|
5
|
+
version: 0.0.9.4
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Raykin Lee
|
@@ -10,7 +10,7 @@ autorequire:
|
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
12
|
|
13
|
-
date: 2011-04-
|
13
|
+
date: 2011-04-27 00:00:00 Z
|
14
14
|
dependencies: []
|
15
15
|
|
16
16
|
description: Rails plugin for mapping static data of web application to Dictionary class
|
@@ -43,7 +43,6 @@ files:
|
|
43
43
|
- spec/fake_app.rb
|
44
44
|
- spec/rails_dictionary_spec.rb
|
45
45
|
- spec/spec_helper.rb
|
46
|
-
- test/rails_dictionary_test.rb
|
47
46
|
homepage: https://github.com/raykin/rails_dictionary
|
48
47
|
licenses: []
|
49
48
|
|
@@ -75,5 +74,4 @@ test_files:
|
|
75
74
|
- spec/fake_app.rb
|
76
75
|
- spec/rails_dictionary_spec.rb
|
77
76
|
- spec/spec_helper.rb
|
78
|
-
- test/rails_dictionary_test.rb
|
79
77
|
has_rdoc:
|
@@ -1,137 +0,0 @@
|
|
1
|
-
# -*- coding: utf-8 -*-
|
2
|
-
# Testing for version 0.0.8 or lower.Copy the test design from acts_as_tree
|
3
|
-
# Usage: run "ruby -I . test/rails_dictionary_test.rb " in root dir
|
4
|
-
#
|
5
|
-
require "test/unit"
|
6
|
-
require "active_support"
|
7
|
-
require "active_record"
|
8
|
-
require "ruby-debug" # coupled with debugger to debug code
|
9
|
-
Object.const_set "RAILS_CACHE", ActiveSupport::Cache.lookup_store
|
10
|
-
require "active_support/cache"
|
11
|
-
require "rails"
|
12
|
-
require "#{File.dirname(__FILE__)}/../lib/rails_dictionary.rb"
|
13
|
-
|
14
|
-
ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => ":memory:")
|
15
|
-
|
16
|
-
$stdout = StringIO.new
|
17
|
-
|
18
|
-
def setup_db
|
19
|
-
ActiveRecord::Base.logger
|
20
|
-
ActiveRecord::Schema.define(:version => 1) do
|
21
|
-
create_table :dict_types do |t|
|
22
|
-
t.string :name
|
23
|
-
t.timestamps
|
24
|
-
end
|
25
|
-
create_table :dictionaries do |t|
|
26
|
-
t.string :name_en
|
27
|
-
t.string :name_zh
|
28
|
-
t.string :name_fr
|
29
|
-
t.integer :dict_type_id
|
30
|
-
t.timestamps
|
31
|
-
end
|
32
|
-
create_table :students do |t|
|
33
|
-
t.string :email
|
34
|
-
t.integer :city
|
35
|
-
t.integer :school
|
36
|
-
t.timestamps
|
37
|
-
end
|
38
|
-
end
|
39
|
-
end
|
40
|
-
|
41
|
-
def teardown_db
|
42
|
-
ActiveRecord::Base.connection.tables.each do |table|
|
43
|
-
ActiveRecord::Base.connection.drop_table(table)
|
44
|
-
end
|
45
|
-
end
|
46
|
-
|
47
|
-
class DictType < ActiveRecord::Base
|
48
|
-
acts_as_dict_type
|
49
|
-
end
|
50
|
-
|
51
|
-
class Dictionary < ActiveRecord::Base
|
52
|
-
acts_as_dictionary
|
53
|
-
end
|
54
|
-
|
55
|
-
class Student < ActiveRecord::Base
|
56
|
-
end
|
57
|
-
|
58
|
-
class CoreExtTest < Test::Unit::TestCase
|
59
|
-
def test_array
|
60
|
-
expected_hash={:student => %w[school city],:admin => %w[role]}
|
61
|
-
blank_hash={}
|
62
|
-
assert_equal expected_hash, %w[student_school student_city admin_role].extract_to_hash(%w[student admin])
|
63
|
-
assert_equal expected_hash, %w[root student_school student_city admin_role].extract_to_hash(%w[student admin])
|
64
|
-
assert_equal blank_hash, %w[root students_school students_city].extract_to_hash(%w[student])
|
65
|
-
end
|
66
|
-
end
|
67
|
-
|
68
|
-
class DictTypeTest < Test::Unit::TestCase
|
69
|
-
def setup
|
70
|
-
setup_db
|
71
|
-
#Object.const_set('Student',Class.new(ActiveRecord::Base))
|
72
|
-
@dt_stu_city=DictType.create! :name => "student_city"
|
73
|
-
@dt_stu_school=DictType.create! :name => "student_school"
|
74
|
-
@dy_shanghai=Dictionary.create! name_en: "shanghai",name_zh: "上海",name_fr: "shanghai",dict_type_id: @dt_stu_city.id
|
75
|
-
@dy_beijing=Dictionary.create! name_en: "beijing",name_zh: "北京",name_fr: "Pékin",dict_type_id: @dt_stu_city.id
|
76
|
-
@stu_beijing=Student.create! email: "beijing@dict.com",city: @dy_beijing.id
|
77
|
-
@stu_shanghai=Student.create! email: "shanghai@dict.com",city: @dy_shanghai.id
|
78
|
-
end
|
79
|
-
|
80
|
-
def teardown
|
81
|
-
teardown_db
|
82
|
-
end
|
83
|
-
|
84
|
-
def test_tab_and_column
|
85
|
-
expected_hash={:student => %w[city school]}
|
86
|
-
assert_equal expected_hash,DictType.tab_and_column
|
87
|
-
end
|
88
|
-
|
89
|
-
def test_all_types
|
90
|
-
assert_equal %w[student_city student_school],DictType.all_types
|
91
|
-
end
|
92
|
-
|
93
|
-
# test revert method in acts_as_dict_type
|
94
|
-
def test_dt_revert
|
95
|
-
assert_equal "student_school",DictType.revert(@dt_stu_school.id)
|
96
|
-
assert_equal nil,DictType.revert(100)
|
97
|
-
end
|
98
|
-
|
99
|
-
# test dynamic instance methods in slave model
|
100
|
-
def test_named_city
|
101
|
-
Student.acts_as_dict_slave
|
102
|
-
# the acts_as_dict_slave need real data to generate dynamic method
|
103
|
-
assert_equal %w[city school],Student.columns_in_dict_type
|
104
|
-
assert_equal %w[city school],Student.dict_columns
|
105
|
-
assert_equal "shanghai",@stu_shanghai.named_city(:en)
|
106
|
-
assert_equal "shanghai",@stu_shanghai.named_city("")
|
107
|
-
assert_equal "Pékin",@stu_beijing.named_city(:fr)
|
108
|
-
end
|
109
|
-
|
110
|
-
def test_named_city_with_default_locale
|
111
|
-
Student.acts_as_dict_slave :locale => :fr
|
112
|
-
assert_equal "Pékin",@stu_beijing.named_city
|
113
|
-
end
|
114
|
-
|
115
|
-
def test_dictionary_method_missing
|
116
|
-
assert_equal [["beijing",2],["shanghai",1]],Dictionary.student_city(:locale => :en)
|
117
|
-
end
|
118
|
-
|
119
|
-
def test_dictionary_method_missing_with_locale
|
120
|
-
assert_equal [["上海", 1],["北京", 2]],Dictionary.student_city(:locale => :zh)
|
121
|
-
end
|
122
|
-
|
123
|
-
def test_delete_dicts_cache
|
124
|
-
@dy_wuhan=Dictionary.create! name_en: "wuhan",name_zh: "武汉",name_fr: "wuhan",dict_type_id: @dt_stu_city.id
|
125
|
-
assert_equal [["beijing", 2],["shanghai",1],["wuhan", 3]],Dictionary.student_city(:locale => :en)
|
126
|
-
@dy_wuhan.destroy
|
127
|
-
assert_equal [["beijing", 2],["shanghai",1]],Dictionary.student_city(:locale => :en)
|
128
|
-
assert_equal [@dy_shanghai,@dy_beijing],Dictionary.student_city
|
129
|
-
end
|
130
|
-
|
131
|
-
def test_delete_all_caches
|
132
|
-
assert_equal %w[student_city student_school],DictType.all_types
|
133
|
-
@dt_stu_school.destroy
|
134
|
-
assert_equal %w[student_city],DictType.all_types
|
135
|
-
end
|
136
|
-
|
137
|
-
end
|