rails_dictionary 0.2.8 → 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
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 309d2e3ca49c9e564523d61307014d44987b7561
|
4
|
+
data.tar.gz: e6ea71c9fc377893c23883a0a0f08d001bc2f328
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0daca999ef3ad0ff8888e5e2ca7ae85fe81bbc62c5e1a7c696f51f7d904eeb5bb985200a471306efb75f5c4ce3e599b5cd2e57513f07ef7d1a95f26294b0acef
|
7
|
+
data.tar.gz: 987fdd01ad2d98bb9a211eda4aed92cedbe9469f4cfaf7a1d6b6803a52f41315ba9c4239e43826a620fb7d289fcc9c53dd206942db0f6f60ccd25ebe206f9d54
|
data/.gitignore
CHANGED
data/CHANGELOG
CHANGED
data/Gemfile
CHANGED
@@ -1,12 +1,10 @@
|
|
1
|
-
source "
|
2
|
-
|
3
|
-
ruby '> 3.3'
|
1
|
+
source "http://rubygems.org"
|
4
2
|
|
5
3
|
# Specify your gem's dependencies in rails_dictionary.gemspec
|
6
4
|
gemspec
|
7
5
|
|
8
6
|
group :development,:test do
|
9
|
-
gem '
|
10
|
-
gem
|
7
|
+
gem 'minitest'
|
8
|
+
gem 'byebug'
|
11
9
|
gem 'sqlite3'
|
12
10
|
end
|
@@ -1,6 +1,3 @@
|
|
1
|
-
= Plan
|
2
|
-
Working on sti branch will target to 0.3 version which will not compatible with 0.1 and 0.2
|
3
|
-
|
4
1
|
= Intro
|
5
2
|
There are always some static data(not static page) in application.For example product type or student diploma type.
|
6
3
|
|
@@ -29,7 +26,7 @@ See change log for brief info.
|
|
29
26
|
== Sample
|
30
27
|
Run following task will give you a simple start.Maybe you should try it in a new application first.
|
31
28
|
rake dicts:generate
|
32
|
-
rake dicts:
|
29
|
+
rake dicts:sample_consumer
|
33
30
|
rake db:migrate
|
34
31
|
rake dicts:sample_data
|
35
32
|
These task are just generate table dictionaries,dict_types,students and some sample data.The data should be
|
@@ -80,7 +77,7 @@ Here is what should be like.Student model can be other models.
|
|
80
77
|
end
|
81
78
|
|
82
79
|
class Student < ActiveRecord::Base
|
83
|
-
# use
|
80
|
+
# use acts_as_dict_consumer when your rails_dictionary version < 0.2
|
84
81
|
acts_as_dict_consumer
|
85
82
|
end
|
86
83
|
|
data/Rakefile
CHANGED
@@ -5,6 +5,7 @@ task :default => [:test]
|
|
5
5
|
|
6
6
|
desc "Running Test"
|
7
7
|
task :test do
|
8
|
-
|
9
|
-
system
|
8
|
+
system 'ruby test/rails_dictionary_test.rb'
|
9
|
+
system 'ruby test/acts_as_consumer_test.rb'
|
10
|
+
system 'ruby test/lookup_test.rb'
|
10
11
|
end
|
data/Readme.md
ADDED
@@ -0,0 +1,10 @@
|
|
1
|
+
## Usage
|
2
|
+
|
3
|
+
Version 0.3 is not backward compatible!!
|
4
|
+
Only for version 0.3.
|
5
|
+
|
6
|
+
See [README.rdoc](https://github.com/raykin/rails_dictionary/blob/sti/README.v2.0.rdoc) for version 0.2.
|
7
|
+
|
8
|
+
### Changes from 0.2 to 0.3
|
9
|
+
|
10
|
+
Locale feature removed. Please use i18n.
|
@@ -0,0 +1,38 @@
|
|
1
|
+
module RailsDictionary
|
2
|
+
module ActiveRecordExtension
|
3
|
+
def self.included(base)
|
4
|
+
base.extend(ClassMethods)
|
5
|
+
end
|
6
|
+
|
7
|
+
module ClassMethods
|
8
|
+
|
9
|
+
def acts_as_dictionary
|
10
|
+
include ActsAsDictionary
|
11
|
+
validates_uniqueness_of :name, scope: [inheritance_column]
|
12
|
+
end
|
13
|
+
|
14
|
+
# Ex: acts_as_dict_consumer on: :city
|
15
|
+
#
|
16
|
+
# on: - add dict mapping columns, can be string or array of string
|
17
|
+
# relation_type: - belongs_to/many_to_many, default is belongs_to. has_many Not supported yet
|
18
|
+
# class_name: - Dictionary class name
|
19
|
+
# locale: - add and initialize class attribute default_dict_locale
|
20
|
+
def acts_as_dict_consumer(opts={})
|
21
|
+
include ActsAsDictConsumer
|
22
|
+
case opts[:on]
|
23
|
+
when Array
|
24
|
+
opts[:on].each do |on_value|
|
25
|
+
build_dict_relation(opts.merge(on: on_value))
|
26
|
+
end
|
27
|
+
when String, Symbol
|
28
|
+
build_dict_relation(opts)
|
29
|
+
else
|
30
|
+
raise TypeError, 'Wrong value of params on'
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
::ActiveRecord::Base.send :include, RailsDictionary::ActiveRecordExtension
|
@@ -0,0 +1,77 @@
|
|
1
|
+
module RailsDictionary
|
2
|
+
module ActsAsDictConsumer
|
3
|
+
def self.included(base)
|
4
|
+
base.extend(ClassMethods)
|
5
|
+
end
|
6
|
+
|
7
|
+
module ClassMethods
|
8
|
+
|
9
|
+
# Generate dynamic instance method named_column to consumer model
|
10
|
+
# def named_city(locale=nil)
|
11
|
+
# locale = locale.presence || default_dict_locale.presence || :en
|
12
|
+
# locale = "name_#{locale}"
|
13
|
+
# self.send(city_dict).try(:send,locale)
|
14
|
+
# end
|
15
|
+
# alias_method :city_name, :named_city
|
16
|
+
# def named_dict_value(method_name)
|
17
|
+
# belongs_to_name="#{method_name}_dict".to_sym
|
18
|
+
# origin_method_name = method_name
|
19
|
+
# method_name="named_#{method_name}"
|
20
|
+
# define_method(method_name) do | locale=nil |
|
21
|
+
# locale = locale.presence || default_dict_locale.presence || :en
|
22
|
+
# locale = "name_#{locale}"
|
23
|
+
# self.send(belongs_to_name).try(:send,locale)
|
24
|
+
# end
|
25
|
+
# alias_method "#{origin_method_name}_name".to_sym, method_name.to_sym
|
26
|
+
# end
|
27
|
+
|
28
|
+
# Build dynamic method column_name= to the consumer model
|
29
|
+
#
|
30
|
+
# ex:
|
31
|
+
# def city_name=(value, options = {})
|
32
|
+
# send "city=", dictionary_obj
|
33
|
+
# end
|
34
|
+
def dict_name_equal
|
35
|
+
relation_name = @dict_relation_name
|
36
|
+
relation_type = @dict_relation_type
|
37
|
+
method_name = "#{relation_name}_name="
|
38
|
+
class_opt = @opt
|
39
|
+
define_method(method_name) do |value, options={}|
|
40
|
+
dicts = RailsDictionary.dclass.where(name: Array(value), type: class_opt[:class_name])
|
41
|
+
if dicts
|
42
|
+
if relation_type == :belongs_to
|
43
|
+
send "#{relation_name}=", dicts.first
|
44
|
+
elsif relation_type == :many_to_many
|
45
|
+
send "#{relation_name}=", dicts.map(&:id)
|
46
|
+
else
|
47
|
+
raise "Wrong relation method name: #{relation_type}"
|
48
|
+
end
|
49
|
+
else
|
50
|
+
# do nothing ?
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
# dont think instance var is a good sollution
|
56
|
+
# cause the consumer class will include other lib too
|
57
|
+
def build_dict_relation(opt)
|
58
|
+
@opt = opt
|
59
|
+
@dict_relation_name = @opt.delete :on
|
60
|
+
raise 'params on cant be nil' if @dict_relation_name.nil?
|
61
|
+
@dict_relation_type = @opt.delete(:relation_type) || :belongs_to
|
62
|
+
# @opt[:foreign_key] ||= "#{@dict_relation_name}_id"
|
63
|
+
@opt[:class_name] ||= "#{RailsDictionary.config.dictionary_klass}::#{@dict_relation_name.to_s.singularize.camelize}"
|
64
|
+
::RailsDictionary.init_dict_sti_class(@opt[:class_name])
|
65
|
+
if @dict_relation_type.to_sym == :belongs_to
|
66
|
+
send @dict_relation_type, @dict_relation_name, @opt
|
67
|
+
elsif @dict_relation_type.to_sym == :many_to_many
|
68
|
+
# no code required?
|
69
|
+
# build_many_to_many_dict_relation
|
70
|
+
end
|
71
|
+
dict_name_equal
|
72
|
+
end
|
73
|
+
|
74
|
+
end # END ClassMethods
|
75
|
+
|
76
|
+
end
|
77
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
module RailsDictionary
|
2
|
+
module ActsAsDictionary
|
3
|
+
def self.included(base)
|
4
|
+
base.extend(ClassMethods)
|
5
|
+
base.send :include, InstanceMethods
|
6
|
+
end
|
7
|
+
|
8
|
+
module ClassMethods
|
9
|
+
|
10
|
+
# override to make sure STI class init first
|
11
|
+
def new(opts)
|
12
|
+
type_opt = opts.with_indifferent_access[inheritance_column]
|
13
|
+
RailsDictionary.init_dict_sti_class(type_opt) if type_opt
|
14
|
+
super
|
15
|
+
end
|
16
|
+
|
17
|
+
end # End ClassMethods
|
18
|
+
|
19
|
+
module InstanceMethods
|
20
|
+
def type=(name)
|
21
|
+
::RailsDictionary.init_dict_sti_class(name)
|
22
|
+
super
|
23
|
+
end
|
24
|
+
end # End InstanceMethods
|
25
|
+
|
26
|
+
end
|
27
|
+
end
|
data/lib/rails_dictionary.rb
CHANGED
@@ -1,13 +1,33 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), "rails_dictionary/models/active_record_extension")
|
2
|
+
require File.join(File.dirname(__FILE__), "rails_dictionary/models/acts_as_dictionary")
|
3
|
+
require File.join(File.dirname(__FILE__), "rails_dictionary/models/acts_as_dict_consumer")
|
4
|
+
|
1
5
|
# rake tasks not autoload in Rails4
|
2
|
-
|
6
|
+
# todo: may add migration(from 0.2 to 0.3) task
|
7
|
+
# Dir[File.expand_path('../tasks/**/*.rake',__FILE__)].each { |ext| load ext } if defined?(Rake)
|
8
|
+
|
9
|
+
module RailsDictionary
|
10
|
+
|
11
|
+
def self.config
|
12
|
+
Config.instance
|
13
|
+
end
|
14
|
+
|
15
|
+
class Config < Struct.new(:dictionary_klass, :defined_sti_klass)
|
16
|
+
include Singleton
|
17
|
+
end
|
3
18
|
|
4
|
-
|
19
|
+
config.dictionary_klass = :Dictionary
|
20
|
+
config.defined_sti_klass = []
|
5
21
|
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
require File.join(File.dirname(__FILE__), "rails_dictionary/acts_as_dictionary")
|
10
|
-
require File.join(File.dirname(__FILE__), "rails_dictionary/acts_as_dict_slave")
|
22
|
+
def self.dclass
|
23
|
+
@dclass ||= config.dictionary_klass.to_s.constantize
|
24
|
+
end
|
11
25
|
|
12
|
-
|
26
|
+
def self.init_dict_sti_class(klass)
|
27
|
+
unless config.defined_sti_klass.include?(klass) || Module.const_defined?(klass)
|
28
|
+
subklass = klass.sub "#{config.dictionary_klass}::", ''
|
29
|
+
dclass.const_set subklass, Class.new(dclass)
|
30
|
+
config.defined_sti_klass.push(klass)
|
31
|
+
end
|
32
|
+
end
|
13
33
|
end
|
data/lib/tasks/dicts.rake
CHANGED
@@ -2,20 +2,22 @@
|
|
2
2
|
namespace :dicts do
|
3
3
|
desc "Generate dictionary and dict_type model"
|
4
4
|
task :generate do
|
5
|
-
system "rails g model dictionary name_en
|
6
|
-
system "rails g model dict_type name:string"
|
5
|
+
system "rails g model dictionary name_en name_zh name_fr type"
|
7
6
|
end
|
7
|
+
|
8
8
|
desc "Generate student model"
|
9
|
-
task :
|
9
|
+
task :sample_consumer do
|
10
10
|
system "rails g model student email:string city:integer school:integer"
|
11
11
|
end
|
12
|
+
|
12
13
|
desc "Generate sample data for rails_dictionary gem"
|
13
14
|
task :sample_data => [:environment] do
|
14
|
-
@dt_stu_city=DictType.create! :
|
15
|
-
@dt_stu_school=DictType.create! :
|
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
|
15
|
+
# @dt_stu_city=DictType.create! name: "student_city"
|
16
|
+
# @dt_stu_school=DictType.create! name: "student_school"
|
17
|
+
@dy_shanghai=Dictionary.create! name_en: "shanghai", name_zh: "上海", name_fr: "shanghai", dict_type_id: @dt_stu_city.id
|
18
|
+
@dy_beijing=Dictionary.create! name_en: "beijing", name_zh: "北京", name_fr: "Pékin", dict_type_id: @dt_stu_city.id
|
19
|
+
@stu_beijing=Student.create! email: "beijing@dict.com", city: @dy_beijing.id
|
20
|
+
@stu_shanghai=Student.create! email: "shanghai@dict.com", city: @dy_shanghai.id
|
20
21
|
end
|
22
|
+
|
21
23
|
end
|
data/rails_dictionary.gemspec
CHANGED
@@ -7,17 +7,17 @@ Gem::Specification.new do |s|
|
|
7
7
|
s.platform = Gem::Platform::RUBY
|
8
8
|
s.authors = ["Raykin Lee"]
|
9
9
|
s.email = ["raykincoldxiao@campus.com"]
|
10
|
-
s.licenses = ['MIT']
|
11
10
|
s.homepage = "https://github.com/raykin/rails_dictionary"
|
12
11
|
s.summary = %q{dictionary data for web application}
|
13
12
|
s.description = %q{Rails plugin for mapping static data of web application to Dictionary class}
|
14
13
|
|
14
|
+
s.rubyforge_project = "rails_dictionary"
|
15
15
|
|
16
|
-
s.add_runtime_dependency 'rails', '
|
16
|
+
s.add_runtime_dependency 'rails', '> 4.0'
|
17
|
+
s.add_runtime_dependency 'database_cleaner'
|
17
18
|
|
18
19
|
s.files = `git ls-files`.split("\n")
|
19
|
-
s.test_files = `git ls-files -- {test
|
20
|
+
s.test_files = `git ls-files -- {test}/*`.split("\n")
|
20
21
|
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
21
22
|
s.require_paths = ["lib"]
|
22
|
-
s.required_ruby_version = '>= 2.7.0'
|
23
23
|
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
require File.expand_path('test_helper', File.dirname(__FILE__))
|
2
|
+
|
3
|
+
class TestConsumer < TestSupporter
|
4
|
+
|
5
|
+
end
|
6
|
+
|
7
|
+
class TestConsumeOneColumn < TestConsumer
|
8
|
+
def setup
|
9
|
+
super
|
10
|
+
Student.acts_as_dict_consumer on: :city
|
11
|
+
end
|
12
|
+
|
13
|
+
def test_student_should_has_method_city
|
14
|
+
assert Student.new.respond_to?(:city), 'student should has method city'
|
15
|
+
assert Student.new.respond_to?(:create_city), 'student should has method create_city'
|
16
|
+
assert_includes RailsDictionary.config.defined_sti_klass, 'Dictionary::City'
|
17
|
+
end
|
18
|
+
|
19
|
+
def test_dictionary_city_class_exist
|
20
|
+
assert Dictionary.const_defined?('City'), 'Dictionary::City should be defined'
|
21
|
+
assert_includes RailsDictionary.config.defined_sti_klass, 'Dictionary::City'
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
class TestConsumeMultipleColumns < TestConsumer
|
26
|
+
|
27
|
+
def setup
|
28
|
+
super
|
29
|
+
Student.acts_as_dict_consumer on: [:city, :school]
|
30
|
+
end
|
31
|
+
|
32
|
+
def test_student_should_has_method_city
|
33
|
+
assert Student.new.respond_to?(:city), 'student should has method city'
|
34
|
+
assert Student.new.respond_to?(:create_city), 'student should has method create_city'
|
35
|
+
assert Student.new.respond_to?(:school), 'student should has method city'
|
36
|
+
assert Student.new.respond_to?(:create_school), 'student should has method city'
|
37
|
+
end
|
38
|
+
|
39
|
+
def test_dictionary_city_and_schoold_exist
|
40
|
+
assert Dictionary.const_defined?('City'), 'Dictionary::City should be defined'
|
41
|
+
assert Dictionary.const_defined?('School'), 'Dictionary::City should be defined'
|
42
|
+
assert_equal(['Dictionary::City', 'Dictionary::School'], RailsDictionary.config.defined_sti_klass)
|
43
|
+
end
|
44
|
+
end
|
data/test/fake_app.rb
ADDED
@@ -0,0 +1,40 @@
|
|
1
|
+
# database
|
2
|
+
ActiveRecord::Base.configurations = {'test' => {:adapter => 'sqlite3', :database => ':memory:'}}
|
3
|
+
ActiveRecord::Base.establish_connection(:test)
|
4
|
+
|
5
|
+
# config
|
6
|
+
app = Class.new(Rails::Application)
|
7
|
+
app.config.active_support.deprecation = :log
|
8
|
+
app.config.eager_load = false
|
9
|
+
app.initialize!
|
10
|
+
|
11
|
+
class Dictionary < ActiveRecord::Base
|
12
|
+
acts_as_dictionary
|
13
|
+
end
|
14
|
+
|
15
|
+
class Student < ActiveRecord::Base
|
16
|
+
end
|
17
|
+
|
18
|
+
class Lookup < ActiveRecord::Base
|
19
|
+
end
|
20
|
+
|
21
|
+
#migrations
|
22
|
+
class CreateAllTables < ActiveRecord::Migration
|
23
|
+
def self.up
|
24
|
+
create_table(:dictionaries) {|t| t.string :name; t.string :type}
|
25
|
+
create_table(:students) do |t|
|
26
|
+
t.string :email
|
27
|
+
t.integer :city_id
|
28
|
+
t.integer :school_id
|
29
|
+
t.text :major_array
|
30
|
+
t.text :majors
|
31
|
+
end
|
32
|
+
create_table(:lookups) { |t| t.string :name; t.string :type }
|
33
|
+
end
|
34
|
+
|
35
|
+
def self.down
|
36
|
+
drop_table :dictionaries
|
37
|
+
drop_table :students
|
38
|
+
drop_table :lookups
|
39
|
+
end
|
40
|
+
end
|
data/test/lookup_test.rb
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
require File.expand_path('test_helper', File.dirname(__FILE__))
|
2
|
+
|
3
|
+
module TestLookup
|
4
|
+
|
5
|
+
class TestAsDictionary < TestSupporter
|
6
|
+
|
7
|
+
def prepare_data
|
8
|
+
@art = Lookup.create!(name: 'art', type: 'Lookup::Major')
|
9
|
+
@math = Lookup.create!(name: 'math', type: 'Lookup::Major')
|
10
|
+
end
|
11
|
+
|
12
|
+
def setup
|
13
|
+
RailsDictionary.config.dictionary_klass = :Lookup
|
14
|
+
Lookup.acts_as_dictionary
|
15
|
+
super
|
16
|
+
end
|
17
|
+
|
18
|
+
def test_lookup
|
19
|
+
prepare_data
|
20
|
+
assert Lookup, Lookup::Major.superclass
|
21
|
+
end
|
22
|
+
|
23
|
+
def test_student_major_array
|
24
|
+
Student.acts_as_dict_consumer on: :major_array, relation_type: :many_to_many, class_name: 'Lookup::Major'
|
25
|
+
prepare_data
|
26
|
+
s = Student.new(major_array_name: ['art', 'math'])
|
27
|
+
s.save!; s.reload
|
28
|
+
assert_equal([@art.id, @math.id], s.major_array)
|
29
|
+
end
|
30
|
+
|
31
|
+
def test_student_with_majors
|
32
|
+
Student.acts_as_dict_consumer on: :majors, relation_type: :many_to_many
|
33
|
+
prepare_data
|
34
|
+
s = Student.new(majors_name: ['art', 'math'])
|
35
|
+
s.save!; s.reload
|
36
|
+
assert_equal([@art.id, @math.id], s.majors)
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
@@ -0,0 +1,81 @@
|
|
1
|
+
require File.expand_path('test_helper', File.dirname(__FILE__))
|
2
|
+
|
3
|
+
class TestRailsDictionary < TestSupporter
|
4
|
+
|
5
|
+
class TestActiveRecordExtension < TestRailsDictionary
|
6
|
+
[:acts_as_dictionary, :acts_as_dict_consumer].each do |method_name|
|
7
|
+
define_method "test_#{method_name}_exists_ar" do
|
8
|
+
assert_includes ActiveRecord::Base.methods, method_name
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
class PreTestDatabase < TestRailsDictionary
|
14
|
+
|
15
|
+
def test_no_dictionary_data_exist_before
|
16
|
+
assert_equal 0, Dictionary.count, 'dicionaries table should be blank'
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
class TestInitSubDictClass < TestRailsDictionary
|
21
|
+
def setup
|
22
|
+
super
|
23
|
+
if Dictionary.const_defined? 'City'
|
24
|
+
Dictionary.send :remove_const, 'City'
|
25
|
+
RailsDictionary.config.defined_sti_klass.delete('Dictionary::City')
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
def test_dictionary_city_class_when_create_dictionary
|
30
|
+
@shanghai = Dictionary.create!(name: "shanghai", type: 'Dictionary::City')
|
31
|
+
assert Dictionary, Dictionary::City.superclass
|
32
|
+
end
|
33
|
+
|
34
|
+
def test_dictionary_city_class_when_assign_type
|
35
|
+
beijing = Dictionary.new(name: 'beijing')
|
36
|
+
beijing.type = 'Dictionary::City'
|
37
|
+
beijing.save!
|
38
|
+
assert Dictionary, Dictionary::City.superclass
|
39
|
+
end
|
40
|
+
|
41
|
+
def test_dictionary_city_class_when_assign_type_in_new
|
42
|
+
beijing = Dictionary.new(name: 'beijing', type: 'Dictionary::City')
|
43
|
+
beijing.save!
|
44
|
+
assert Dictionary, Dictionary::City.superclass
|
45
|
+
end
|
46
|
+
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
class TestWithDB < TestRailsDictionary
|
51
|
+
def setup
|
52
|
+
super
|
53
|
+
Dictionary.acts_as_dictionary
|
54
|
+
@beijing, @shanghai = prepare_city_data
|
55
|
+
|
56
|
+
@stu_beijing = Student.create! email: "beijing@dict.com", city_id: @shanghai.id
|
57
|
+
@stu_shanghai = Student.create! email: "shanghai@dict.com", city_id: @shanghai.id
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
class TestStudent < TestWithDB
|
62
|
+
def setup
|
63
|
+
super
|
64
|
+
Student.acts_as_dict_consumer on: [:city]
|
65
|
+
end
|
66
|
+
|
67
|
+
def test_city_name_equal_to_exist_dictionary_name
|
68
|
+
assert_equal 1, Dictionary.where(name: "beijing").count
|
69
|
+
@stu_shanghai.update_attributes city_name: "beijing"
|
70
|
+
assert_equal 'beijing', @stu_shanghai.reload.city.name
|
71
|
+
assert_equal 1, Dictionary.where(name: "beijing").count
|
72
|
+
end
|
73
|
+
|
74
|
+
def create_a_student_with_shanghai_city
|
75
|
+
s = Student.create(city_name: "shanghai")
|
76
|
+
s.reload
|
77
|
+
s.city.name.should == "shanghai"
|
78
|
+
assert_equal Dictionary.find_by(name: 'shanghai').id, s.city_id
|
79
|
+
assert_equal 1, Dictionary.where(name: "shanghai").count
|
80
|
+
end
|
81
|
+
end
|
data/test/test_helper.rb
ADDED
@@ -0,0 +1,36 @@
|
|
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 'byebug'
|
6
|
+
require 'rack/test'
|
7
|
+
require 'rails'
|
8
|
+
require 'active_record'
|
9
|
+
require 'rails_dictionary'
|
10
|
+
|
11
|
+
ActiveRecord::Migration.verbose = false
|
12
|
+
|
13
|
+
require File.join(File.dirname(__FILE__), 'fake_app')
|
14
|
+
|
15
|
+
CreateAllTables.up unless ActiveRecord::Base.connection.table_exists? 'dictionaries'
|
16
|
+
|
17
|
+
Student.serialize :major_array, Array
|
18
|
+
Student.serialize :majors, Array
|
19
|
+
|
20
|
+
require 'minitest/autorun'
|
21
|
+
require 'database_cleaner'
|
22
|
+
|
23
|
+
DatabaseCleaner.strategy = :truncation
|
24
|
+
|
25
|
+
class TestSupporter < Minitest::Test
|
26
|
+
def setup
|
27
|
+
DatabaseCleaner.clean
|
28
|
+
end
|
29
|
+
|
30
|
+
def prepare_city_data
|
31
|
+
[
|
32
|
+
Dictionary.create!(name: 'beijing', type: 'Dictionary::City'),
|
33
|
+
Dictionary.create!(name: 'shanghai', type: 'Dictionary::City')
|
34
|
+
]
|
35
|
+
end
|
36
|
+
end
|