rails_dictionary 0.2.7 → 0.3.pre.rc1
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.
- checksums.yaml +5 -5
- data/.gitignore +3 -5
- data/CHANGELOG +4 -2
- data/Gemfile +3 -5
- data/{README.rdoc → README.v2.0.rdoc} +2 -5
- data/Rakefile +3 -2
- data/Readme.md +10 -0
- data/lib/rails_dictionary/models/active_record_extension.rb +38 -0
- data/lib/rails_dictionary/models/acts_as_dict_consumer.rb +77 -0
- data/lib/rails_dictionary/models/acts_as_dictionary.rb +27 -0
- data/lib/rails_dictionary/version.rb +1 -1
- data/lib/rails_dictionary.rb +28 -8
- data/lib/tasks/dicts.rake +11 -9
- data/rails_dictionary.gemspec +4 -4
- data/test/acts_as_consumer_test.rb +44 -0
- data/test/fake_app.rb +40 -0
- data/test/lookup_test.rb +39 -0
- data/test/rails_dictionary_test.rb +81 -0
- data/test/test_helper.rb +36 -0
- metadata +41 -33
- data/Gemfile.lock +0 -243
- data/lib/rails_dictionary/active_record_extension.rb +0 -48
- data/lib/rails_dictionary/acts_as_dict_slave.rb +0 -85
- data/lib/rails_dictionary/acts_as_dict_type.rb +0 -52
- data/lib/rails_dictionary/acts_as_dictionary.rb +0 -82
- data/lib/rails_dictionary/array_core_ext.rb +0 -13
- data/pkg/rails_dictionary-0.2.2.gem +0 -0
- data/pkg/rails_dictionary-0.2.3.gem +0 -0
- data/spec/fake_app.rb +0 -29
- data/spec/init_models.rb +0 -12
- data/spec/rails_dictionary_spec.rb +0 -131
- data/spec/spec_helper.rb +0 -22
@@ -1,131 +0,0 @@
|
|
1
|
-
# coding: utf-8
|
2
|
-
require File.expand_path('spec_helper', File.dirname(__FILE__))
|
3
|
-
|
4
|
-
describe RailsDictionary::ActiveRecordExtension do
|
5
|
-
%w[acts_as_dict_type acts_as_dictionary acts_as_dict_slave].each do |method_name|
|
6
|
-
it "contains #{method_name}" do
|
7
|
-
expect(ActiveRecord::Base.methods).to include(method_name.to_sym)
|
8
|
-
end
|
9
|
-
end
|
10
|
-
end
|
11
|
-
|
12
|
-
describe RailsDictionary do
|
13
|
-
let!(:dt_stu_city) { DictType.create! :name => "student_city" }
|
14
|
-
let!(:dt_stu_school) { DictType.create! :name => "student_school" }
|
15
|
-
let!(:dy_shanghai) { Dictionary.create! name_en: "shanghai",name_zh: "上海",name_fr: "shanghai",dict_type_id: dt_stu_city.id }
|
16
|
-
let!(:dy_beijing) { Dictionary.create! name_en: "beijing",name_zh: "北京",name_fr: "Pékin",dict_type_id: dt_stu_city.id }
|
17
|
-
let!(:stu_beijing) { Student.create! email: "beijing@dict.com",city: dy_beijing.id }
|
18
|
-
let!(:stu_shanghai) { Student.create! email: "shanghai@dict.com",city: dy_shanghai.id }
|
19
|
-
|
20
|
-
describe DictType do
|
21
|
-
it "parse model and method" do
|
22
|
-
expected_hash={:student => %w[city school]}
|
23
|
-
DictType.tab_and_column.should == expected_hash
|
24
|
-
end
|
25
|
-
|
26
|
-
it "all types" do
|
27
|
-
DictType.all_types.should == [:student_city, :student_school]
|
28
|
-
end
|
29
|
-
|
30
|
-
it "reverts id to name or name to id" do
|
31
|
-
DictType.revert(dt_stu_school.id).should == "student_school"
|
32
|
-
DictType.revert(100).should be_nil
|
33
|
-
end
|
34
|
-
|
35
|
-
it "after one record removed" do
|
36
|
-
DictType.all_types
|
37
|
-
DictType.whole_types.should == [:student_city, :student_school]
|
38
|
-
dt_stu_school.destroy
|
39
|
-
DictType.all_types.should == [:student_city]
|
40
|
-
DictType.tab_and_column.should == Hash[:student,["city"]]
|
41
|
-
end
|
42
|
-
end
|
43
|
-
|
44
|
-
describe Dictionary do
|
45
|
-
it "should respond to student_city" do
|
46
|
-
Dictionary.should respond_to(:student_city)
|
47
|
-
end
|
48
|
-
|
49
|
-
it "generate student_city method" do
|
50
|
-
Dictionary.student_city.should == [dy_shanghai,dy_beijing]
|
51
|
-
end
|
52
|
-
|
53
|
-
it "generate student_city method with locale" do
|
54
|
-
Dictionary.student_city(:locale => :zh).should == [["北京", 2],["上海",1]]
|
55
|
-
end
|
56
|
-
|
57
|
-
it "build scope method scoped_student_city" do
|
58
|
-
Dictionary.scoped_student_city.class.name.should == "ActiveRecord::Relation"
|
59
|
-
Dictionary.scoped_student_city.where(:id => 1).should == [dy_shanghai]
|
60
|
-
end
|
61
|
-
|
62
|
-
it "after record added or removed" do
|
63
|
-
@dy_wuhan=Dictionary.create! name_en: "wuhan",name_zh: "武汉",name_fr: "wuhan",dict_type_id: dt_stu_city.id
|
64
|
-
Dictionary.student_city(:locale => :en).should == [["beijing", 2],["shanghai",1],["wuhan", 3]]
|
65
|
-
@dy_wuhan.destroy
|
66
|
-
Dictionary.student_city(:locale => :en).should == [["beijing", 2],["shanghai",1]]
|
67
|
-
end
|
68
|
-
|
69
|
-
end
|
70
|
-
|
71
|
-
describe Student do
|
72
|
-
# Run it because dict type value has cached at runtime but data inserted after cache created
|
73
|
-
before :each do
|
74
|
-
Student.acts_as_dict_slave
|
75
|
-
end
|
76
|
-
it "method of columns_in_dict_type and dict_columns" do
|
77
|
-
Student.columns_in_dict_type.should == %w[city school]
|
78
|
-
Student.dict_columns == %w[city school]
|
79
|
-
end
|
80
|
-
|
81
|
-
it "named_city with different locale" do
|
82
|
-
stu_shanghai.named_city(:en).should == "shanghai"
|
83
|
-
stu_shanghai.city_name(:en).should == "shanghai"
|
84
|
-
stu_shanghai.city_name.should == "shanghai"
|
85
|
-
stu_shanghai.named_city("").should == "shanghai"
|
86
|
-
stu_beijing.named_city(:fr).should == "Pékin"
|
87
|
-
end
|
88
|
-
|
89
|
-
it "update city by set city_name to a value" do
|
90
|
-
stu_shanghai.update city_name: "wuhan"
|
91
|
-
stu_shanghai.reload.city_name.should == "wuhan"
|
92
|
-
Dictionary.student_city.map(&:name_en).include?("wuhan").should be_truthy
|
93
|
-
end
|
94
|
-
|
95
|
-
it "update city by set city_name to an exist dictionary name" do
|
96
|
-
Dictionary.where(name_en: "beijing").count.should eq(1)
|
97
|
-
stu_shanghai.update city_name: "beijing"
|
98
|
-
stu_shanghai.reload.city_name.should eq('beijing')
|
99
|
-
Dictionary.where(name_en: "beijing").count.should eq(1)
|
100
|
-
end
|
101
|
-
|
102
|
-
it "create a student with shanghai city" do
|
103
|
-
s = Student.create(city_name: "shanghai")
|
104
|
-
s.reload
|
105
|
-
s.city_name.should == "shanghai"
|
106
|
-
end
|
107
|
-
|
108
|
-
it "override default locale" do
|
109
|
-
Student.acts_as_dict_consumer :locale => :fr
|
110
|
-
stu_beijing.named_city.should == "Pékin"
|
111
|
-
end
|
112
|
-
|
113
|
-
it "create a student with new city(reproduce conflict error with Rails5.2.0)" do
|
114
|
-
s = Student.new
|
115
|
-
s.city_name = 'Sedan'
|
116
|
-
s.save
|
117
|
-
s.reload
|
118
|
-
s.city_name.should == 'Sedan'
|
119
|
-
end
|
120
|
-
end
|
121
|
-
end
|
122
|
-
|
123
|
-
describe Array do
|
124
|
-
let(:an_array) { %w[root student_school student_city admin_role] }
|
125
|
-
let(:expected_hash) { {:student => %w[school city],:admin => %w[role]} }
|
126
|
-
let(:blank_hash) { Hash.new }
|
127
|
-
it "extract to hash" do
|
128
|
-
an_array.extract_to_hash(%w[student admin]).should == expected_hash
|
129
|
-
an_array.extract_to_hash(%w[students]).should == blank_hash
|
130
|
-
end
|
131
|
-
end
|
data/spec/spec_helper.rb
DELETED
@@ -1,22 +0,0 @@
|
|
1
|
-
# copy and modify the test design of kaminari(a paginate gem for Rails3)
|
2
|
-
RAILS_ENV = 'test'
|
3
|
-
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
4
|
-
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
5
|
-
require 'rails'
|
6
|
-
require 'active_record'
|
7
|
-
require 'action_controller/railtie'
|
8
|
-
require 'action_view/railtie'
|
9
|
-
require 'rails_dictionary'
|
10
|
-
|
11
|
-
require File.join(File.dirname(__FILE__), 'fake_app')
|
12
|
-
|
13
|
-
require 'rspec/rails'
|
14
|
-
|
15
|
-
# $stdout = StringIO.new # remove the noise outputo
|
16
|
-
|
17
|
-
RSpec.configure do |config|
|
18
|
-
config.use_transactional_fixtures = true
|
19
|
-
config.expect_with(:rspec) { |c| c.syntax = [:should, :expect] }
|
20
|
-
CreateAllTables.up unless ActiveRecord::Base.connection.table_exists? 'dict_types'
|
21
|
-
require File.join(File.dirname(__FILE__), 'init_models')
|
22
|
-
end
|