rails_dictionary 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +9 -7
- data/Rakefile +1 -1
- data/lib/{dictionary → rails_dictionary}/acts_as_dict_slave.rb +1 -0
- data/lib/{dictionary → rails_dictionary}/acts_as_dict_type.rb +8 -7
- data/lib/{dictionary → rails_dictionary}/acts_as_dictionary.rb +1 -1
- data/lib/{dictionary → rails_dictionary}/array_core_ext.rb +0 -0
- data/lib/rails_dictionary/version.rb +3 -0
- data/lib/rails_dictionary.rb +4 -4
- data/{dictionary.gemspec → rails_dictionary.gemspec} +3 -3
- data/test/{dictionary_test.rb → rails_dictionary_test.rb} +1 -2
- metadata +10 -11
- data/.gitignore +0 -4
- data/lib/dictionary/version.rb +0 -3
data/README.rdoc
CHANGED
@@ -5,7 +5,7 @@ In a student application,there will be some static data like diploma kind,school
|
|
5
5
|
= Usage
|
6
6
|
== Installation
|
7
7
|
Rail3
|
8
|
-
gem "
|
8
|
+
gem "rails_dictionary",:git => 'git://github.com/raykin/rails_dictionary'
|
9
9
|
or
|
10
10
|
gem 'rails_dictionary'
|
11
11
|
|
@@ -72,17 +72,19 @@ Student is a slave model which will be automatically generate a (or a list of)be
|
|
72
72
|
DictType.all_types => %w[student_city student_school]
|
73
73
|
Dictionary.student_city => [Dictionary.find(5),Dictionary.find(6)]
|
74
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 =>
|
75
|
+
Dictionary.student_school => []
|
76
76
|
Dictionary.student_city(:locale => :en) => [["shanghai",1],["beijing",2]]
|
77
77
|
so you can use these method in form select like
|
78
|
-
|
79
|
-
|
78
|
+
collection_select :student,:city,Dictionary.student_city,:id,:name_en
|
79
|
+
select :student,:city,Dictionary.student_city(params)
|
80
|
+
if params contains :locale => :fr,then it would return a list of french name of student city (from name_fr in Dictioanry)
|
80
81
|
Student.find(1).named_city = "beijing"
|
82
|
+
Here is an other solution for international translation.
|
81
83
|
Student.find(1).named_city(:zh) = "北京"
|
82
84
|
Student.find(1).named_city(:fr) = "Pékin"
|
83
|
-
|
85
|
+
Student has 2 belongs_to assocition which named as city_dict and school_dict,the naming convention is columname_dict.
|
84
86
|
Student.find(1).city_dict = Dictionary.find(6)
|
85
|
-
|
87
|
+
|
86
88
|
|
87
89
|
= Practical Suggestion
|
88
90
|
If you start a new application and there are more than 10 kinds of static data,you may have a try with the gem.
|
@@ -92,7 +94,7 @@ However,if you see many static data in an old system and want to refactor it,the
|
|
92
94
|
The most used debug method would be DictType.all_types and Dictionary.student_city(or other dynamic generate method)
|
93
95
|
When you get some confused with these method,try running
|
94
96
|
rails tmp:clear
|
95
|
-
cause these methods all return static data,I just
|
97
|
+
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
|
96
98
|
delete from dict_types;
|
97
99
|
The rails cache would not refresh.
|
98
100
|
In short,when you confused with the debug data,try running "rails tmp:clear" first.
|
data/Rakefile
CHANGED
@@ -1,4 +1,3 @@
|
|
1
|
-
require "rails"
|
2
1
|
module ActsAsDictType
|
3
2
|
def self.included(base)
|
4
3
|
base.extend(ClassMethods)
|
@@ -21,19 +20,21 @@ module ActsAsDictType
|
|
21
20
|
Rails.cache.fetch("DictType.cached_all") { all }.dup
|
22
21
|
end
|
23
22
|
|
23
|
+
# short method to transfer id to name or name to id
|
24
24
|
def self.revert(arg)
|
25
|
-
if arg.
|
26
|
-
DictType.
|
27
|
-
elsif arg.
|
28
|
-
DictType.
|
25
|
+
if arg.is_a?(String)
|
26
|
+
DictType.where(name: arg).try(:first).id
|
27
|
+
elsif arg.is_a?(Fixnum)
|
28
|
+
DictType.where(id: arg).try(:first).name
|
29
29
|
end
|
30
30
|
end
|
31
31
|
|
32
|
-
#TODO: get a more accurate method name
|
32
|
+
# TODO: get a more accurate method name
|
33
|
+
# parse the name to get which column and model are listed in DictType
|
33
34
|
def self.tab_and_column
|
34
35
|
@tab_and_column={}
|
35
36
|
@all_types=all_types
|
36
|
-
#TODO: any better way to retrive the class name in app/model ?
|
37
|
+
# TODO: any better way to retrive the class name in app/model ?
|
37
38
|
# Here maybe a problem when class like Ckeditor::Asset(.name.underscore => "ckeditor/asset"
|
38
39
|
all_tabs=ActiveRecord::Base.connection.tables.sort.reject! do |t|
|
39
40
|
['schema_migrations', 'sessions'].include?(t)
|
@@ -17,7 +17,7 @@ module ActsAsDictionary
|
|
17
17
|
if DictType.all_types.include? method_id.to_s
|
18
18
|
Rails.cache.fetch("Dictionary.#{method_name}") { Dictionary.joins(:dict_type).where('dict_types.name'=>method_name).all }
|
19
19
|
listed_attr=Rails.cache.read("Dictionary.#{method_name}").dup
|
20
|
-
if options.keys.include? :locale
|
20
|
+
if options.keys.include? :locale or options.keys.include? "locale"
|
21
21
|
locale="name_#{options[:locale]}"
|
22
22
|
listed_attr.map! { |a| [a.send(locale),a.id] }
|
23
23
|
listed_attr.sort {|a,b| a.last <=> b.last } # maybe remove this one
|
File without changes
|
data/lib/rails_dictionary.rb
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
require "
|
2
|
-
require "
|
3
|
-
require "
|
4
|
-
require "
|
1
|
+
require "rails_dictionary/acts_as_dictionary"
|
2
|
+
require "rails_dictionary/acts_as_dict_slave"
|
3
|
+
require "rails_dictionary/acts_as_dict_type"
|
4
|
+
require "rails_dictionary/array_core_ext"
|
@@ -1,14 +1,14 @@
|
|
1
1
|
# -*- encoding: utf-8 -*-
|
2
2
|
$:.push File.expand_path("../lib", __FILE__)
|
3
|
-
require "
|
3
|
+
require "rails_dictionary/version"
|
4
4
|
|
5
5
|
Gem::Specification.new do |s|
|
6
6
|
s.name = "rails_dictionary"
|
7
|
-
s.version =
|
7
|
+
s.version = RailsDictionary::VERSION
|
8
8
|
s.platform = Gem::Platform::RUBY
|
9
9
|
s.authors = ["raykin"]
|
10
10
|
s.email = ["raykincoldxiao@campus.com"]
|
11
|
-
s.homepage = "https://github.com/raykin/
|
11
|
+
s.homepage = "https://github.com/raykin/rails_dictionary"
|
12
12
|
s.summary = %q{dictionary data for application}
|
13
13
|
s.description = %q{mapping static data of web application to Dictionary class}
|
14
14
|
|
@@ -1,5 +1,4 @@
|
|
1
1
|
# -*- coding: utf-8 -*-
|
2
|
-
# min test
|
3
2
|
require "test/unit"
|
4
3
|
require "active_support"
|
5
4
|
require "active_record"
|
@@ -7,7 +6,7 @@ require "ruby-debug"
|
|
7
6
|
Object.const_set "RAILS_CACHE", ActiveSupport::Cache.lookup_store
|
8
7
|
require "active_support/cache"
|
9
8
|
require "rails"
|
10
|
-
# $: << "/home/raykin/studio/
|
9
|
+
# $: << "/home/raykin/studio/rails_dictionary/lib" # tmply added for local testing
|
11
10
|
require "#{File.dirname(__FILE__)}/../lib/rails_dictionary.rb"
|
12
11
|
|
13
12
|
ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => ":memory:")
|
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.3
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- raykin
|
@@ -24,20 +24,19 @@ extensions: []
|
|
24
24
|
extra_rdoc_files: []
|
25
25
|
|
26
26
|
files:
|
27
|
-
- .gitignore
|
28
27
|
- Gemfile
|
29
28
|
- README.rdoc
|
30
29
|
- Rakefile
|
31
|
-
- dictionary.gemspec
|
32
|
-
- lib/dictionary/acts_as_dict_slave.rb
|
33
|
-
- lib/dictionary/acts_as_dict_type.rb
|
34
|
-
- lib/dictionary/acts_as_dictionary.rb
|
35
|
-
- lib/dictionary/array_core_ext.rb
|
36
|
-
- lib/dictionary/version.rb
|
37
30
|
- lib/rails_dictionary.rb
|
38
|
-
-
|
31
|
+
- lib/rails_dictionary/acts_as_dict_slave.rb
|
32
|
+
- lib/rails_dictionary/acts_as_dict_type.rb
|
33
|
+
- lib/rails_dictionary/acts_as_dictionary.rb
|
34
|
+
- lib/rails_dictionary/array_core_ext.rb
|
35
|
+
- lib/rails_dictionary/version.rb
|
36
|
+
- rails_dictionary.gemspec
|
37
|
+
- test/rails_dictionary_test.rb
|
39
38
|
has_rdoc: true
|
40
|
-
homepage: https://github.com/raykin/
|
39
|
+
homepage: https://github.com/raykin/rails_dictionary
|
41
40
|
licenses: []
|
42
41
|
|
43
42
|
post_install_message:
|
@@ -65,4 +64,4 @@ signing_key:
|
|
65
64
|
specification_version: 3
|
66
65
|
summary: dictionary data for application
|
67
66
|
test_files:
|
68
|
-
- test/
|
67
|
+
- test/rails_dictionary_test.rb
|
data/.gitignore
DELETED
data/lib/dictionary/version.rb
DELETED