rails_dictionary 0.2.6 → 0.3.pre.rc1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/.gitignore +3 -3
- data/CHANGELOG +4 -2
- data/Gemfile +2 -3
- 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 +5 -3
- 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 +37 -33
- data/Gemfile.lock +0 -193
- data/lib/rails_dictionary/active_record_extension.rb +0 -48
- data/lib/rails_dictionary/acts_as_dict_slave.rb +0 -88
- data/lib/rails_dictionary/acts_as_dict_type.rb +0 -56
- data/lib/rails_dictionary/acts_as_dictionary.rb +0 -86
- 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 -41
- data/spec/rails_dictionary_spec.rb +0 -130
- data/spec/spec_helper.rb +0 -23
@@ -1,130 +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
|
-
before :each do
|
73
|
-
Student.acts_as_dict_slave
|
74
|
-
end
|
75
|
-
it "method of columns_in_dict_type and dict_columns" do
|
76
|
-
Student.columns_in_dict_type.should == %w[city school]
|
77
|
-
Student.dict_columns == %w[city school]
|
78
|
-
end
|
79
|
-
|
80
|
-
it "named_city with different locale" do
|
81
|
-
stu_shanghai.named_city(:en).should == "shanghai"
|
82
|
-
stu_shanghai.city_name(:en).should == "shanghai"
|
83
|
-
stu_shanghai.city_name.should == "shanghai"
|
84
|
-
stu_shanghai.named_city("").should == "shanghai"
|
85
|
-
stu_beijing.named_city(:fr).should == "Pékin"
|
86
|
-
end
|
87
|
-
|
88
|
-
it "update city by set city_name to a value" do
|
89
|
-
stu_shanghai.update city_name: "wuhan"
|
90
|
-
stu_shanghai.reload.city_name.should == "wuhan"
|
91
|
-
Dictionary.student_city.map(&:name_en).include?("wuhan").should be_truthy
|
92
|
-
end
|
93
|
-
|
94
|
-
it "update city by set city_name to an exist dictionary name" do
|
95
|
-
Dictionary.where(name_en: "beijing").count.should eq(1)
|
96
|
-
stu_shanghai.update city_name: "beijing"
|
97
|
-
stu_shanghai.reload.city_name.should eq('beijing')
|
98
|
-
Dictionary.where(name_en: "beijing").count.should eq(1)
|
99
|
-
end
|
100
|
-
|
101
|
-
it "create a student with shanghai city" do
|
102
|
-
s = Student.create(city_name: "shanghai")
|
103
|
-
s.reload
|
104
|
-
s.city_name.should == "shanghai"
|
105
|
-
end
|
106
|
-
|
107
|
-
it "override default locale" do
|
108
|
-
Student.acts_as_dict_slave :locale => :fr
|
109
|
-
stu_beijing.named_city.should == "Pékin"
|
110
|
-
end
|
111
|
-
|
112
|
-
it "create a student with new city(reproduce conflict error with Rails5.2.0)" do
|
113
|
-
s = Student.new
|
114
|
-
s.city_name = 'Sedan'
|
115
|
-
s.save
|
116
|
-
s.reload
|
117
|
-
s.city_name.should == 'Sedan'
|
118
|
-
end
|
119
|
-
end
|
120
|
-
end
|
121
|
-
|
122
|
-
describe Array do
|
123
|
-
let(:an_array) { %w[root student_school student_city admin_role] }
|
124
|
-
let(:expected_hash) { {:student => %w[school city],:admin => %w[role]} }
|
125
|
-
let(:blank_hash) { Hash.new }
|
126
|
-
it "extract to hash" do
|
127
|
-
an_array.extract_to_hash(%w[student admin]).should == expected_hash
|
128
|
-
an_array.extract_to_hash(%w[students]).should == blank_hash
|
129
|
-
end
|
130
|
-
end
|
data/spec/spec_helper.rb
DELETED
@@ -1,23 +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
|
-
require 'pry-byebug'
|
16
|
-
|
17
|
-
# $stdout = StringIO.new # remove the noise output. It can also disable pry debugger. Weird
|
18
|
-
|
19
|
-
RSpec.configure do |config|
|
20
|
-
config.use_transactional_fixtures = true
|
21
|
-
config.expect_with(:rspec) { |c| c.syntax = [:should, :expect] }
|
22
|
-
CreateAllTables.up unless ActiveRecord::Base.connection.table_exists? 'dict_types'
|
23
|
-
end
|