rails_dictionary 0.2 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 29704f4fcfc7b1a4a58b808a271d6121d790ed0c
4
+ data.tar.gz: 9f58740b71f9b150134c87b44e3d112c62b83c5d
5
+ SHA512:
6
+ metadata.gz: 2c1fd60db8d10a661fe7ab9f0925e51c874685816af149a57e9a914778ee88ea55c6f9137cf0929d924c6921cdb932398116f70752f1a22ee385641e6722af45
7
+ data.tar.gz: 4cf139cf52ad990394c9686365e639a1f81803f6503460cf3fac17f716a9ad903c7243ab1facd0a5b6fbe159f3190cffa636bb6c8b29144fc22a0876789cf207
data/Gemfile CHANGED
@@ -4,8 +4,8 @@ source "http://rubygems.org"
4
4
  gemspec
5
5
 
6
6
  group :development,:test do
7
- gem 'rspec', '>= 2.5.0'
8
- gem 'debugger'
9
- gem "rspec-rails"
7
+ gem 'rspec', '~> 2.14.0'
8
+ gem 'byebug'
9
+ gem "rspec-rails", '2.14.2'
10
10
  gem 'sqlite3'
11
11
  end
data/README.rdoc CHANGED
@@ -1,3 +1,6 @@
1
+ = Plan
2
+ I plan wrote a new gem to replace it. The new gem will use rails STI. so we dont need maintenance dict_types table. It should be a better solution.
3
+
1
4
  = Intro
2
5
  There are always some static data(not static page) in application.For example product type or student diploma type.
3
6
 
@@ -35,27 +35,54 @@ module RailsDictionary
35
35
 
36
36
  # add a belongs_to(Dictionary) association and a named_{column} method
37
37
  def add_dynamic_column_method
38
- dict_mapping_columns.each { |e| belongs_to "#{e.to_s}_dict".to_sym,class_name: "Dictionary",foreign_key: e.to_sym }
38
+ dict_mapping_columns.each { |e| belongs_to "#{e}_dict".to_sym, class_name: "Dictionary", foreign_key: e.to_sym }
39
39
  dict_mapping_columns.each { |ele| named_dict_value ele.to_sym }
40
+ dict_mapping_columns.each { |ele| dict_name_equal ele.to_sym }
40
41
  end
41
42
 
42
- # Generate dynamic instance method named_column to slave model
43
+ # Generate dynamic instance method named_column to consumer model
43
44
  # def named_city(locale=nil)
44
45
  # locale = locale.presence || default_dict_locale.presence || :en
45
46
  # locale = "name_#{locale}"
46
47
  # self.send(city_dict).try(:send,locale)
47
48
  # end
49
+ # alias_method :city_name, :named_city
48
50
  def named_dict_value(method_name)
49
- belongs_to_name="#{method_name.to_s}_dict".to_sym
50
- method_name="named_#{method_name.to_s}"
51
+ belongs_to_name="#{method_name}_dict".to_sym
52
+ origin_method_name = method_name
53
+ method_name="named_#{method_name}"
51
54
  define_method(method_name) do | locale=nil |
52
55
  locale = locale.presence || default_dict_locale.presence || :en
53
56
  locale = "name_#{locale}"
54
57
  self.send(belongs_to_name).try(:send,locale)
55
58
  end
59
+ alias_method "#{origin_method_name}_name".to_sym, method_name.to_sym
56
60
  end
57
61
 
58
- end
62
+ # Build dynamic method column_name= to the consumer model
63
+ #
64
+ # def city_name=(value, options = {})
65
+ #
66
+ #
67
+ # end
68
+ def dict_name_equal(colname)
69
+ method_name = "#{colname}_name="
70
+ belongs_to_name="#{colname}_dict".to_sym
71
+ define_method(method_name) do |value, options={}|
72
+ options.merge!(name_en: value)
73
+ dict_type_id = DictType.revert("#{self.class.table_name.singularize}_#{colname}")
74
+ options.merge!(dict_type_id: dict_type_id)
75
+ exist_dictionary = Dictionary.where(options)
76
+ if exist_dictionary.present?
77
+ exist_id = exist_dictionary.first.id
78
+ else
79
+ exist_id = send("create_#{belongs_to_name}!", options).id
80
+ end
81
+ send "#{colname}=", exist_id
82
+ end
83
+ end
84
+
85
+ end # END ClassMethods
59
86
 
60
87
  end
61
88
  end
@@ -2,20 +2,19 @@ module RailsDictionary
2
2
  module ActsAsDictType
3
3
  def self.included(base)
4
4
  base.extend(ClassMethods)
5
- base.send :include,InstanceMethods
5
+ base.send :include, InstanceMethods
6
+ base.mattr_accessor :whole_types
6
7
  end
7
8
 
8
9
  module ClassMethods
9
10
 
10
11
  def all_types
11
- Rails.cache.fetch("DictType.all_types") { all.map(&:name).map(&:to_sym) }.dup
12
- end
13
-
14
- def cached_all
15
- Rails.cache.fetch("DictType.cached_all") { all }.dup
12
+ whole_types ||
13
+ self.whole_types = all.map(&:name).map(&:to_sym)
16
14
  end
17
15
 
18
16
  # short method to transfer id to name or name to id
17
+ # TODO: cache it
19
18
  def revert(arg)
20
19
  if arg.is_a?(String)
21
20
  DictType.where(name: arg).first.try(:id)
@@ -48,8 +47,7 @@ module RailsDictionary
48
47
 
49
48
  module InstanceMethods
50
49
  def delete_all_caches
51
- Rails.cache.delete("DictType.all_types")
52
- Rails.cache.delete("DictType.cached_all")
50
+ self.whole_types = nil
53
51
  return true
54
52
  end
55
53
  end
@@ -25,6 +25,7 @@ module RailsDictionary
25
25
  def method_missing(method_id,options={})
26
26
  if DictType.all_types.include? method_id
27
27
  method_name=method_id.to_s.downcase
28
+ # TODO: If cache engine is failed, then the code will failed with null cant dup
28
29
  Rails.cache.fetch("Dictionary.#{method_name}") { dict_type_name_eq(method_name).to_a }
29
30
  listed_attr=Rails.cache.read("Dictionary.#{method_name}").dup # Instance of ActiveRecord::Relation can not be dup?
30
31
  build_scope_method(method_id)
@@ -64,7 +65,9 @@ module RailsDictionary
64
65
  scope_method_name = "scoped_#{name}".to_sym
65
66
  unless respond_to? scope_method_name
66
67
  define_singleton_method scope_method_name do
67
- dict_type_name_eq(name).scoped
68
+ # see http://stackoverflow.com/questions/18198963/with-rails-4-model-scoped-is-deprecated-but-model-all-cant-replace-it
69
+ # for usage of where(nil)
70
+ dict_type_name_eq(name).where(nil)
68
71
  end
69
72
  end
70
73
  end
@@ -1,3 +1,3 @@
1
1
  module RailsDictionary
2
- VERSION = "0.2"
2
+ VERSION = "0.2.1"
3
3
  end
@@ -15,7 +15,6 @@ Gem::Specification.new do |s|
15
15
  s.rubyforge_project = "rails_dictionary"
16
16
 
17
17
  s.add_runtime_dependency 'rails', '> 3.0'
18
- s.add_development_dependency 'rails', '> 3.0'
19
18
 
20
19
  s.files = `git ls-files`.split("\n")
21
20
  s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
data/spec/fake_app.rb CHANGED
@@ -1,11 +1,12 @@
1
1
 
2
2
  # database
3
3
  ActiveRecord::Base.configurations = {'test' => {:adapter => 'sqlite3', :database => ':memory:'}}
4
- ActiveRecord::Base.establish_connection('test')
4
+ ActiveRecord::Base.establish_connection(:test)
5
5
 
6
6
  # config
7
7
  app = Class.new(Rails::Application)
8
8
  app.config.active_support.deprecation = :log
9
+ app.config.eager_load = false
9
10
  app.initialize!
10
11
 
11
12
  # models
@@ -1,5 +1,4 @@
1
1
  # -*- coding: utf-8 -*-
2
- require 'debugger'
3
2
  require File.expand_path('spec_helper', File.dirname(__FILE__))
4
3
 
5
4
  describe RailsDictionary::ActiveRecordExtension do
@@ -34,6 +33,8 @@ describe RailsDictionary do
34
33
  end
35
34
 
36
35
  it "after one record removed" do
36
+ DictType.all_types
37
+ DictType.whole_types.should == [:student_city, :student_school]
37
38
  dt_stu_school.destroy
38
39
  DictType.all_types.should == [:student_city]
39
40
  DictType.tab_and_column.should == Hash[:student,["city"]]
@@ -78,10 +79,31 @@ describe RailsDictionary do
78
79
 
79
80
  it "named_city with different locale" do
80
81
  stu_shanghai.named_city(:en).should == "shanghai"
82
+ stu_shanghai.city_name(:en).should == "shanghai"
83
+ stu_shanghai.city_name.should == "shanghai"
81
84
  stu_shanghai.named_city("").should == "shanghai"
82
85
  stu_beijing.named_city(:fr).should == "Pékin"
83
86
  end
84
87
 
88
+ it "update city by set city_name to a value" do
89
+ stu_shanghai.update_attributes city_name: "wuhan"
90
+ stu_shanghai.reload.city_name.should == "wuhan"
91
+ Dictionary.student_city.map(&:name_en).include?("wuhan").should be_true
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_attributes 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
+
85
107
  it "override default locale" do
86
108
  Student.acts_as_dict_slave :locale => :fr
87
109
  stu_beijing.named_city.should == "Pékin"
data/spec/spec_helper.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  # copy and modify the test design of kaminari(a paginate gem for Rails3)
2
+ RAILS_ENV = 'test'
2
3
  $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3
4
  $LOAD_PATH.unshift(File.dirname(__FILE__))
4
5
  require 'rails'
@@ -17,3 +18,5 @@ RSpec.configure do |config|
17
18
  config.use_transactional_fixtures = true
18
19
  CreateAllTables.up unless ActiveRecord::Base.connection.table_exists? 'dict_types'
19
20
  end
21
+
22
+ require 'byebug'
metadata CHANGED
@@ -1,46 +1,27 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rails_dictionary
3
3
  version: !ruby/object:Gem::Version
4
- version: '0.2'
5
- prerelease:
4
+ version: 0.2.1
6
5
  platform: ruby
7
6
  authors:
8
7
  - Raykin Lee
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2013-07-08 00:00:00.000000000 Z
11
+ date: 2015-03-11 00:00:00.000000000 Z
13
12
  dependencies:
14
13
  - !ruby/object:Gem::Dependency
15
14
  name: rails
16
15
  requirement: !ruby/object:Gem::Requirement
17
- none: false
18
16
  requirements:
19
- - - ! '>'
17
+ - - ">"
20
18
  - !ruby/object:Gem::Version
21
19
  version: '3.0'
22
20
  type: :runtime
23
21
  prerelease: false
24
22
  version_requirements: !ruby/object:Gem::Requirement
25
- none: false
26
23
  requirements:
27
- - - ! '>'
28
- - !ruby/object:Gem::Version
29
- version: '3.0'
30
- - !ruby/object:Gem::Dependency
31
- name: rails
32
- requirement: !ruby/object:Gem::Requirement
33
- none: false
34
- requirements:
35
- - - ! '>'
36
- - !ruby/object:Gem::Version
37
- version: '3.0'
38
- type: :development
39
- prerelease: false
40
- version_requirements: !ruby/object:Gem::Requirement
41
- none: false
42
- requirements:
43
- - - ! '>'
24
+ - - ">"
44
25
  - !ruby/object:Gem::Version
45
26
  version: '3.0'
46
27
  description: Rails plugin for mapping static data of web application to Dictionary
@@ -51,7 +32,7 @@ executables: []
51
32
  extensions: []
52
33
  extra_rdoc_files: []
53
34
  files:
54
- - .gitignore
35
+ - ".gitignore"
55
36
  - CHANGELOG
56
37
  - Gemfile
57
38
  - README.rdoc
@@ -70,26 +51,26 @@ files:
70
51
  - spec/spec_helper.rb
71
52
  homepage: https://github.com/raykin/rails_dictionary
72
53
  licenses: []
54
+ metadata: {}
73
55
  post_install_message:
74
56
  rdoc_options: []
75
57
  require_paths:
76
58
  - lib
77
59
  required_ruby_version: !ruby/object:Gem::Requirement
78
- none: false
79
60
  requirements:
80
- - - ! '>='
61
+ - - ">="
81
62
  - !ruby/object:Gem::Version
82
63
  version: '0'
83
64
  required_rubygems_version: !ruby/object:Gem::Requirement
84
- none: false
85
65
  requirements:
86
- - - ! '>='
66
+ - - ">="
87
67
  - !ruby/object:Gem::Version
88
68
  version: '0'
89
69
  requirements: []
90
70
  rubyforge_project: rails_dictionary
91
- rubygems_version: 1.8.23
71
+ rubygems_version: 2.2.2
92
72
  signing_key:
93
- specification_version: 3
73
+ specification_version: 4
94
74
  summary: dictionary data for web application
95
75
  test_files: []
76
+ has_rdoc: