rails_dictionary 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +99 -7
- data/dictionary.gemspec +1 -1
- data/lib/dictionary/version.rb +1 -1
- data/lib/{dictionary.rb → rails_dictionary.rb} +1 -5
- data/test/dictionary_test.rb +1 -1
- metadata +4 -4
data/README.rdoc
CHANGED
@@ -1,12 +1,104 @@
|
|
1
1
|
= Intro
|
2
|
-
|
3
|
-
|
2
|
+
There are many static data in application.
|
3
|
+
In a student application,there will be some static data like diploma kind,school and city.This gem can map these static data to a Dictionary#method like Dictionary#student_diploma_kind which return data list of diploma kind and a Student.named_school like @student.named_city which return city name of @student city
|
4
4
|
|
5
|
-
|
5
|
+
= Usage
|
6
|
+
== Installation
|
6
7
|
Rail3
|
7
|
-
|
8
|
+
gem "dictionary",:git => 'git://github.com/raykin/dictionary'
|
9
|
+
or
|
10
|
+
gem 'rails_dictionary'
|
11
|
+
|
12
|
+
== Sample
|
13
|
+
In this usage sample,there are three tables.
|
14
|
+
=== Table Definition
|
15
|
+
Make sure you have two tables which named as dict_types and dictionaries.
|
16
|
+
For version 0.0.1,I'm not going to add more configurations.
|
17
|
+
The students table is not required.
|
18
|
+
create_table :dict_types do |t|
|
19
|
+
t.string :name
|
20
|
+
t.string :comment
|
21
|
+
t.timestamps
|
22
|
+
end
|
23
|
+
create_table :dictionaries do |t|
|
24
|
+
t.string :name_en
|
25
|
+
t.string :name_zh
|
26
|
+
t.string :name_fr
|
27
|
+
t.integer :dict_type_id
|
28
|
+
t.timestamps
|
29
|
+
end
|
30
|
+
create_table :students do |t|
|
31
|
+
t.string :email
|
32
|
+
t.integer :city
|
33
|
+
t.integer :school
|
34
|
+
t.timestamps
|
35
|
+
end
|
36
|
+
|
37
|
+
=== Class Definition
|
38
|
+
Method acts_as_dict_type,acts_as_dictionary,acts_as_dict_slave like other simple gem method used as class method.
|
39
|
+
Here is what should be like.Student model can be other models.
|
40
|
+
class DictType < ActiveRecord::Base
|
41
|
+
acts_as_dict_type
|
42
|
+
end
|
43
|
+
|
44
|
+
class Dictionary < ActiveRecord::Base
|
45
|
+
acts_as_dictionary
|
46
|
+
end
|
47
|
+
|
48
|
+
class Student < ActiveRecord::Base
|
49
|
+
acts_as_dict_slave
|
50
|
+
end
|
51
|
+
|
52
|
+
=== Sample Data
|
53
|
+
Conventions over configuration make magic again.
|
54
|
+
The name value convention of DictType is "model_column".Here the city and school column of student model are static values that will be mapping to dynamic method,so the DictType value should be as following code according convention.
|
55
|
+
Dictionary value has simple convention,just keep the name structure as "name_locale".Dictionary store the real static data that will not change in certain period.
|
56
|
+
Student is a slave model which will be automatically generate a (or a list of)belongs_to associations to dictionary and a list of dynamic query method according to DictType data.
|
57
|
+
DictType.find(1).name => "student_city"
|
58
|
+
DictType.find(2).name => "student_school"
|
59
|
+
DictType.count => 2
|
60
|
+
Dictionary.count => 2
|
61
|
+
Dictionary.find(5) =>
|
62
|
+
#<Dictionary id: 1, name_en: "shanghai", name_zh: "上海", name_fr: "shanghai", dict_type_id: 1, created_at: "2011-02-27 13:28:55", updated_at: "2011-02-27 13:28:55">
|
63
|
+
Dictionary.find(6) =>
|
64
|
+
#<Dictionary id: 2, name_en: "beijing", name_zh: "北京", name_fr: "Pékin", dict_type_id: 1, created_at: "2011-02-27 13:28:55", updated_at: "2011-02-27 13:28:55">
|
65
|
+
Student.find(1) =>
|
66
|
+
#<Student id: 1, email: "beijing@dict.com", city: 6, school: nil, created_at: "2011-02-27 13:28:55", updated_at: "2011-02-27 13:28:55">
|
67
|
+
Student.find(2) =>
|
68
|
+
#<Student id: 2, email: "shanghai@dict.com", city: 5, school: nil, created_at: "2011-02-27 13:28:55", updated_at: "2011-02-27 13:28:55">
|
69
|
+
|
70
|
+
== Methods and results (relies on the above data)
|
71
|
+
DictType.all_cached => return cache of DictType.all
|
72
|
+
DictType.all_types => %w[student_city student_school]
|
73
|
+
Dictionary.student_city => [Dictionary.find(5),Dictionary.find(6)]
|
74
|
+
# student_city is a dynamic method which returns a list of dictionary object which dict_type is student_city.Actually Dictionary will has as many dynamic methods as DictType.count and the dynamic method name is DictType.name,so there is another dynamic method which return nil
|
75
|
+
Dictionary.student_school => nil
|
76
|
+
Dictionary.student_city(:locale => :en) => [["shanghai",1],["beijing",2]]
|
77
|
+
so you can use these method in form select like
|
78
|
+
select :student,:city,Dictionary.student_city
|
79
|
+
this select method has a more clear format which may do some other magic coding
|
80
|
+
Student.find(1).named_city = "beijing"
|
81
|
+
Student.find(1).named_city(:zh) = "北京"
|
82
|
+
Student.find(1).named_city(:fr) = "Pékin"
|
83
|
+
Here is an other solution for international translation.
|
84
|
+
Student.find(1).city_dict = Dictionary.find(6)
|
85
|
+
In these simple example,Student has 2 belongs_to assocition which named as city_dict and school_dict,the naming convention is columname_dict.
|
86
|
+
|
87
|
+
= Practical Suggestion
|
88
|
+
If you start a new application and there are more than 10 kinds of static data,you may have a try with the gem.
|
89
|
+
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.
|
90
|
+
|
91
|
+
= Warnning
|
92
|
+
The most used debug method would be DictType.all_types and Dictionary.student_city(or other dynamic generate method)
|
93
|
+
When you get some confused with these method,try running
|
94
|
+
rails tmp:clear
|
95
|
+
cause these methods all return static data,I just add caches to these output for better performance.If you straight forward change db data in db console(not throught Rails) like running
|
96
|
+
delete from dict_types;
|
97
|
+
The rails cache would not refresh.
|
98
|
+
In short,when you confused with the debug data,try running "rails tmp:clear" first.
|
8
99
|
|
9
|
-
|
100
|
+
= TODO & Problems
|
101
|
+
Get a more smart method(Rails way method) to get all Model Class and there is a problem with it.Class like Ckeditor::Asset will not be recognized.
|
10
102
|
|
11
|
-
|
12
|
-
|
103
|
+
= Help
|
104
|
+
Need an friendly english native guy to help me improve this readme to make it more clear and readable.
|
data/dictionary.gemspec
CHANGED
@@ -10,7 +10,7 @@ Gem::Specification.new do |s|
|
|
10
10
|
s.email = ["raykincoldxiao@campus.com"]
|
11
11
|
s.homepage = "https://github.com/raykin/dictionary"
|
12
12
|
s.summary = %q{dictionary data for application}
|
13
|
-
s.description = %q{mapping
|
13
|
+
s.description = %q{mapping static data of web application to Dictionary class}
|
14
14
|
|
15
15
|
s.rubyforge_project = "rails_dictionary"
|
16
16
|
|
data/lib/dictionary/version.rb
CHANGED
data/test/dictionary_test.rb
CHANGED
@@ -8,7 +8,7 @@ Object.const_set "RAILS_CACHE", ActiveSupport::Cache.lookup_store
|
|
8
8
|
require "active_support/cache"
|
9
9
|
require "rails"
|
10
10
|
# $: << "/home/raykin/studio/dictionary/lib" # tmply added for local testing
|
11
|
-
require "#{File.dirname(__FILE__)}/../lib/
|
11
|
+
require "#{File.dirname(__FILE__)}/../lib/rails_dictionary.rb"
|
12
12
|
|
13
13
|
ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => ":memory:")
|
14
14
|
|
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.2
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- raykin
|
@@ -10,11 +10,11 @@ autorequire:
|
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
12
|
|
13
|
-
date: 2011-02-
|
13
|
+
date: 2011-02-28 00:00:00 +08:00
|
14
14
|
default_executable:
|
15
15
|
dependencies: []
|
16
16
|
|
17
|
-
description: mapping
|
17
|
+
description: mapping static data of web application to Dictionary class
|
18
18
|
email:
|
19
19
|
- raykincoldxiao@campus.com
|
20
20
|
executables: []
|
@@ -29,12 +29,12 @@ files:
|
|
29
29
|
- README.rdoc
|
30
30
|
- Rakefile
|
31
31
|
- dictionary.gemspec
|
32
|
-
- lib/dictionary.rb
|
33
32
|
- lib/dictionary/acts_as_dict_slave.rb
|
34
33
|
- lib/dictionary/acts_as_dict_type.rb
|
35
34
|
- lib/dictionary/acts_as_dictionary.rb
|
36
35
|
- lib/dictionary/array_core_ext.rb
|
37
36
|
- lib/dictionary/version.rb
|
37
|
+
- lib/rails_dictionary.rb
|
38
38
|
- test/dictionary_test.rb
|
39
39
|
has_rdoc: true
|
40
40
|
homepage: https://github.com/raykin/dictionary
|