rails_dictionary 0.0.7 → 0.0.8

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1 @@
1
+ pkg/
data/README.rdoc CHANGED
@@ -1,44 +1,58 @@
1
1
  = Intro
2
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
3
+ For example in a student application,there may be some static data like diploma kind,school and city of school.This gem can map these static data to a Dictionary#method like Dictionary#student_diploma_kind which return data list of diploma kind and generate a list of instance method on Student like @student.named_city which return city name of @student city
4
4
 
5
5
  = Usage
6
6
  == Installation
7
7
  Rail3
8
- gem "rails_dictionary",:git => 'git://github.com/raykin/rails_dictionary'
9
- or
10
8
  gem 'rails_dictionary'
9
+ or
10
+ gem "rails_dictionary",:git => 'git://github.com/raykin/rails_dictionary'
11
11
 
12
12
  == Sample
13
- In this usage sample,there are three tables.
13
+ Run following task will give you a simple start.Maybe you should try it in a new application first.
14
+ rake dicts:generate
15
+ rake dicts:sample_slave
16
+ rake db:migrate
17
+ rake dicts:sample_data
18
+ These task are just generate table dictionaries,dict_types,students and some sample data.The data should be
19
+ irb(main):013:0> DictType.select("id,name").all
20
+ DictType Load (0.4ms) SELECT id,name FROM `dict_types`
21
+ +----+----------------+
22
+ | id | name |
23
+ +----+----------------+
24
+ | 1 | student_city |
25
+ | 2 | student_school |
26
+ +----+----------------+
27
+ 2 rows in set
28
+ irb(main):014:0> Dictionary.select("id,name_en,name_zh,name_fr,dict_type_id").all
29
+ Dictionary Load (1.2ms) SELECT id,name_en,name_zh,name_fr,dict_type_id FROM `dictionaries`
30
+ +----+----------+---------+----------+--------------+
31
+ | id | name_en | name_zh | name_fr | dict_type_id |
32
+ +----+----------+---------+----------+--------------+
33
+ | 1 | shanghai | 上海 | shanghai | 1 |
34
+ | 2 | beijing | 北京 | Pékin | 1 |
35
+ +----+----------+---------+----------+--------------+
36
+ 2 rows in set
37
+ irb(main):016:0> Student.select("id,email,city,school").all
38
+ Student Load (0.4ms) SELECT id,email,city,school FROM `students`
39
+ +----+-------------------+------+--------+
40
+ | id | email | city | school |
41
+ +----+-------------------+------+--------+
42
+ | 1 | beijing@dict.com | 4 | |
43
+ | 2 | shanghai@dict.com | 3 | |
44
+ +----+-------------------+------+--------+
45
+ 2 rows in set
46
+ There is one conventions of DictType.name value.All value of DictType.name is "model_method" : student is model and city is method of student model.
47
+
14
48
  === Table Definition
15
49
  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
- 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.
18
- The students table is not required.
19
-
20
- create_table :dict_types do |t|
21
- t.string :name
22
- t.string :comment
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
50
+ Table dictionaries has one convention of naming column : name_locale.So the name_fr means this column have a french value,you can see more usage later.
51
+ The students table is not required and variable by your application.
38
52
 
39
53
  === Class Definition
40
- Method acts_as_dict_type,acts_as_dictionary,acts_as_dict_slave like other simple gem method used as class method.
41
54
  Here is what should be like.Student model can be other models.
55
+
42
56
  class DictType < ActiveRecord::Base
43
57
  acts_as_dict_type
44
58
  end
@@ -51,42 +65,23 @@ Here is what should be like.Student model can be other models.
51
65
  acts_as_dict_slave
52
66
  end
53
67
 
54
- === Sample Data
55
- Conventions over configuration make magic again.
56
- 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.
57
- 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.
58
- 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.
59
- DictType.find(1).name => "student_city"
60
- DictType.find(2).name => "student_school"
61
- DictType.count => 2
62
- Dictionary.count => 2
63
- Dictionary.find(5) =>
64
- #<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">
65
- Dictionary.find(6) =>
66
- #<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">
67
- Student.find(1) =>
68
- #<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">
69
- Student.find(2) =>
70
- #<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">
71
-
72
- == Methods and results (relies on the above data)
73
- DictType.all_cached => return cache of DictType.all
74
- DictType.all_types => %w[student_city student_school]
75
- Dictionary.student_city => [Dictionary.find(5),Dictionary.find(6)]
76
- # 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
77
- Dictionary.student_school => []
78
- Dictionary.student_city(:locale => :en) => [["shanghai",1],["beijing",2]]
79
- so you can use these method in form select like
68
+ == Features : Methods and results (relies on the above data)
69
+ DictType.all_cached #=> return cache of DictType.all
70
+ DictType.all_types = %w[student_city student_school] # also cached
71
+ Dictionary.student_city #=> [Dictionary.find(5),Dictionary.find(6)]
72
+ student_city is a dynamic method which returns a list of dictionary object which dict_type is "student_city".Actually Dictionary will have as many dynamic methods as DictType.count and each dynamic method name is DictType.name(This methods returning an array,not ActiveRelation).
73
+ Dictionary.student_school = []
74
+ Dictionary.student_city :locale => :en = [["shanghai",1],["beijing",2]]
75
+ You can use it in form select method like
80
76
  collection_select :student,:city,Dictionary.student_city,:id,:name_en
81
77
  select :student,:city,Dictionary.student_city(params)
82
- if params contains :locale => :fr,then it would return a list of french name of student city (from name_fr in Dictioanry)
83
- Student.find(1).named_city = "beijing"
78
+ If params contains :locale => :fr,it returns a list of french name of student city (from name_fr in Dictioanry)
79
+ Student.find(1).named_city = "beijing" # when default locale is :en
84
80
  Here is an other solution for international translation.
85
81
  Student.find(1).named_city(:zh) = "北京"
86
82
  Student.find(1).named_city(:fr) = "Pékin"
87
- Student has 2 belongs_to assocition which named as city_dict and school_dict,the naming convention is columname_dict.
88
- Student.find(1).city_dict = Dictionary.find(6)
89
-
83
+ Student has two belongs_to assocition which named as city_dict and school_dict,the naming convention is method_dict.
84
+ Student.find(1).city_dict #=> Dictionary.find(6)
90
85
 
91
86
  = Practical Suggestion
92
87
  If you start a new application and there are more than 10 kinds of static data,you may have a try with the gem.
@@ -96,14 +91,11 @@ However,if you see many static data in an old system and want to refactor it,the
96
91
  The most used debug method would be DictType.all_types and Dictionary.student_city(or other dynamic generate method)
97
92
  When you get some confused with the output of these method,try running
98
93
  rails tmp:clear
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
94
+ cause these methods all return static data(may be a mass of data),I just caches these output for better performance.If you change db data in db console(not through Rails) like running
100
95
  delete from dict_types;
101
96
  The rails cache would not refresh.
102
97
  In short,when you confused with the debug data,try running "rails tmp:clear" first.
103
98
 
104
99
  = TODO & Problems
105
100
  There are no convention and implemention to map Class like Ckeditor::Asset to a method.
106
- Add generators. Rewrite Testing,make it more maintainable.
107
-
108
- = Help
109
- Need an friendly english native guy to help me improve this readme to make it more clear and readable.
101
+ Rewrite Testing,make it more maintainable.
@@ -30,7 +30,7 @@ module ActsAsDictType
30
30
  end
31
31
 
32
32
  # TODO: get a more accurate method name
33
- # parse the name to get which column and model are listed in DictType
33
+ # Parse the name value to get which column and model are listed in DictType
34
34
  def self.tab_and_column
35
35
  tab_and_column={}
36
36
  # There are two chooses,one is subclasses the other is descendants,
@@ -1,3 +1,3 @@
1
1
  module RailsDictionary
2
- VERSION = "0.0.7"
2
+ VERSION = "0.0.8"
3
3
  end
@@ -0,0 +1,21 @@
1
+ # -*- coding: utf-8 -*-
2
+ namespace :dicts do
3
+ desc "Generate dictionary and dict_type model"
4
+ task :generate do
5
+ system "rails g model dictionary name_en:string name_zh:string name_fr:string dict_type_id:integer"
6
+ system "rails g model dict_type name:string"
7
+ end
8
+ desc "Generate student model"
9
+ task :sample_slave do
10
+ system "rails g model student email:string city:integer school:integer"
11
+ end
12
+ desc "Generate sample data for rails_dictionary gem"
13
+ task :sample_data => [:environment] do
14
+ @dt_stu_city=DictType.create! :name => "student_city"
15
+ @dt_stu_school=DictType.create! :name => "student_school"
16
+ @dy_shanghai=Dictionary.create! name_en: "shanghai",name_zh: "上海",name_fr: "shanghai",dict_type_id: @dt_stu_city.id
17
+ @dy_beijing=Dictionary.create! name_en: "beijing",name_zh: "北京",name_fr: "Pékin",dict_type_id: @dt_stu_city.id
18
+ @stu_beijing=Student.create! email: "beijing@dict.com",city: @dy_beijing.id
19
+ @stu_shanghai=Student.create! email: "shanghai@dict.com",city: @dy_shanghai.id
20
+ end
21
+ end
@@ -9,8 +9,8 @@ Gem::Specification.new do |s|
9
9
  s.authors = ["raykin"]
10
10
  s.email = ["raykincoldxiao@campus.com"]
11
11
  s.homepage = "https://github.com/raykin/rails_dictionary"
12
- s.summary = %q{dictionary data for application}
13
- s.description = %q{mapping static data of web application to Dictionary class}
12
+ s.summary = %q{dictionary data for web application}
13
+ s.description = %q{Rails plugin for mapping static data of web application to Dictionary class}
14
14
 
15
15
  s.rubyforge_project = "rails_dictionary"
16
16
 
@@ -18,7 +18,6 @@ def setup_db
18
18
  ActiveRecord::Schema.define(:version => 1) do
19
19
  create_table :dict_types do |t|
20
20
  t.string :name
21
- t.string :comment
22
21
  t.timestamps
23
22
  end
24
23
  create_table :dictionaries do |t|
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.7
5
+ version: 0.0.8
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-03-07 00:00:00 +08:00
13
+ date: 2011-03-21 00:00:00 +08:00
14
14
  default_executable:
15
15
  dependencies: []
16
16
 
17
- description: mapping static data of web application to Dictionary class
17
+ description: Rails plugin for mapping static data of web application to Dictionary class
18
18
  email:
19
19
  - raykincoldxiao@campus.com
20
20
  executables: []
@@ -24,6 +24,7 @@ extensions: []
24
24
  extra_rdoc_files: []
25
25
 
26
26
  files:
27
+ - .gitignore
27
28
  - Gemfile
28
29
  - README.rdoc
29
30
  - Rakefile
@@ -33,6 +34,7 @@ files:
33
34
  - lib/rails_dictionary/acts_as_dictionary.rb
34
35
  - lib/rails_dictionary/array_core_ext.rb
35
36
  - lib/rails_dictionary/version.rb
37
+ - lib/tasks/dicts.rake
36
38
  - rails_dictionary.gemspec
37
39
  - test/rails_dictionary_test.rb
38
40
  has_rdoc: true
@@ -59,9 +61,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
59
61
  requirements: []
60
62
 
61
63
  rubyforge_project: rails_dictionary
62
- rubygems_version: 1.6.1
64
+ rubygems_version: 1.6.2
63
65
  signing_key:
64
66
  specification_version: 3
65
- summary: dictionary data for application
67
+ summary: dictionary data for web application
66
68
  test_files:
67
69
  - test/rails_dictionary_test.rb