has_localization_table 0.2.0 → 0.3.3
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.
- data/.gitignore +1 -0
- data/lib/has_localization_table/active_record/relation.rb +26 -5
- data/lib/has_localization_table/active_record.rb +0 -1
- data/lib/has_localization_table/version.rb +1 -1
- data/spec/active_record/attributes_spec.rb +1 -1
- data/spec/active_record/relation_spec.rb +42 -4
- data/spec/spec_helper.rb +1 -0
- metadata +41 -21
data/.gitignore
CHANGED
@@ -3,7 +3,8 @@ module HasLocalizationTable
|
|
3
3
|
module Relation
|
4
4
|
def self.extended(klass)
|
5
5
|
klass.send(:include, InstanceMethods)
|
6
|
-
|
6
|
+
klass.send(:create_localization_associations!)
|
7
|
+
|
7
8
|
# Alias the scoping method to use the actual association name
|
8
9
|
alias_method :"with_#{klass.localization_association_name}", :with_localizations
|
9
10
|
end
|
@@ -17,12 +18,32 @@ module HasLocalizationTable
|
|
17
18
|
join_sql
|
18
19
|
)
|
19
20
|
end
|
20
|
-
|
21
|
-
|
21
|
+
|
22
|
+
private
|
23
|
+
|
24
|
+
def create_localization_associations!
|
25
|
+
create_has_many_association
|
26
|
+
create_has_one_association
|
27
|
+
end
|
28
|
+
|
29
|
+
# Collect the localization for the current locale
|
30
|
+
def create_has_one_association
|
31
|
+
table_name = localization_class.table_name
|
32
|
+
foreign_key = HasLocalizationTable.locale_foreign_key
|
33
|
+
|
34
|
+
has_one_options = localization_table_options.except(:association_name, :required, :optional, :dependent).
|
35
|
+
merge({ conditions: Proc.new { "#{table_name}.#{foreign_key} = #{HasLocalizationTable.current_locale.id}"} })
|
36
|
+
|
37
|
+
self.has_one localization_association_name.to_s.singularize.to_sym, has_one_options
|
38
|
+
end
|
39
|
+
|
40
|
+
# Collect all localizations for the object
|
41
|
+
def create_has_many_association
|
22
42
|
self.has_many localization_association_name, localization_table_options.except(:association_name, :required, :optional)
|
23
43
|
end
|
24
|
-
|
25
|
-
|
44
|
+
|
45
|
+
public
|
46
|
+
|
26
47
|
module InstanceMethods
|
27
48
|
# Add localization objects for any available locale that doesn't have one
|
28
49
|
def build_missing_localizations!
|
@@ -1,4 +1,5 @@
|
|
1
1
|
require 'spec_helper'
|
2
|
+
#require 'active_record/relation'
|
2
3
|
|
3
4
|
describe HasLocalizationTable do
|
4
5
|
before do
|
@@ -10,11 +11,48 @@ describe HasLocalizationTable do
|
|
10
11
|
end
|
11
12
|
|
12
13
|
Object.send(:remove_const, :Article) rescue nil
|
13
|
-
Article = Class.new(ActiveRecord::Base)
|
14
|
+
Article = Class.new(ActiveRecord::Base) do
|
15
|
+
def self.localization_association_name; :strings; end
|
16
|
+
def self.localization_table_options; {}; end
|
17
|
+
def self.localization_class; ArticleLocalization; end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
subject do
|
22
|
+
Article.send(:extend, HasLocalizationTable::ActiveRecord::Relation)
|
23
|
+
Article
|
14
24
|
end
|
15
|
-
|
25
|
+
|
16
26
|
it "should alias with_localizations with the actual association name" do
|
17
|
-
|
18
|
-
|
27
|
+
assert subject.respond_to? :with_strings
|
28
|
+
end
|
29
|
+
|
30
|
+
it "should create a has_many association" do
|
31
|
+
reflection = subject.reflect_on_association(:strings)
|
32
|
+
refute_nil reflection
|
33
|
+
reflection.macro.must_equal :has_many
|
34
|
+
end
|
35
|
+
|
36
|
+
it "should create a has_one association" do
|
37
|
+
reflection = subject.reflect_on_association(:string)
|
38
|
+
refute_nil reflection
|
39
|
+
reflection.macro.must_equal :has_one
|
40
|
+
end
|
41
|
+
|
42
|
+
it "should use the current locale for the has_one association" do
|
43
|
+
locale = MiniTest::Mock.new
|
44
|
+
locale.expect :id, 2
|
45
|
+
|
46
|
+
conditions = subject.reflect_on_association(:string).options[:conditions]
|
47
|
+
|
48
|
+
HasLocalizationTable.stub :current_locale, locale do
|
49
|
+
conditions.call.must_equal "article_localizations.locale_id = 2"
|
50
|
+
end
|
51
|
+
|
52
|
+
locale.expect :id, 3
|
53
|
+
|
54
|
+
HasLocalizationTable.stub :current_locale, locale do
|
55
|
+
conditions.call.must_equal "article_localizations.locale_id = 3"
|
56
|
+
end
|
19
57
|
end
|
20
58
|
end
|
data/spec/spec_helper.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: has_localization_table
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.3
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,52 +9,72 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
12
|
+
date: 2013-05-31 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activesupport
|
16
|
-
|
17
|
-
|
16
|
+
prerelease: false
|
17
|
+
requirement: !ruby/object:Gem::Requirement
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
20
20
|
- !ruby/object:Gem::Version
|
21
21
|
version: 3.0.0
|
22
|
+
none: false
|
22
23
|
type: :runtime
|
23
|
-
|
24
|
-
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
requirements:
|
26
|
+
- - ! '>='
|
27
|
+
- !ruby/object:Gem::Version
|
28
|
+
version: 3.0.0
|
29
|
+
none: false
|
25
30
|
- !ruby/object:Gem::Dependency
|
26
31
|
name: activerecord
|
27
|
-
|
28
|
-
|
32
|
+
prerelease: false
|
33
|
+
requirement: !ruby/object:Gem::Requirement
|
29
34
|
requirements:
|
30
35
|
- - ! '>='
|
31
36
|
- !ruby/object:Gem::Version
|
32
37
|
version: 3.0.0
|
38
|
+
none: false
|
33
39
|
type: :runtime
|
34
|
-
|
35
|
-
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
requirements:
|
42
|
+
- - ! '>='
|
43
|
+
- !ruby/object:Gem::Version
|
44
|
+
version: 3.0.0
|
45
|
+
none: false
|
36
46
|
- !ruby/object:Gem::Dependency
|
37
47
|
name: minitest
|
38
|
-
|
39
|
-
|
48
|
+
prerelease: false
|
49
|
+
requirement: !ruby/object:Gem::Requirement
|
40
50
|
requirements:
|
41
51
|
- - ! '>='
|
42
52
|
- !ruby/object:Gem::Version
|
43
53
|
version: '0'
|
54
|
+
none: false
|
44
55
|
type: :development
|
45
|
-
|
46
|
-
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
requirements:
|
58
|
+
- - ! '>='
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
version: '0'
|
61
|
+
none: false
|
47
62
|
- !ruby/object:Gem::Dependency
|
48
63
|
name: sqlite3
|
49
|
-
|
50
|
-
|
64
|
+
prerelease: false
|
65
|
+
requirement: !ruby/object:Gem::Requirement
|
51
66
|
requirements:
|
52
67
|
- - ! '>='
|
53
68
|
- !ruby/object:Gem::Version
|
54
69
|
version: '0'
|
70
|
+
none: false
|
55
71
|
type: :development
|
56
|
-
|
57
|
-
|
72
|
+
version_requirements: !ruby/object:Gem::Requirement
|
73
|
+
requirements:
|
74
|
+
- - ! '>='
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
version: '0'
|
77
|
+
none: false
|
58
78
|
description: Automatically sets up usage of a relational table to contain user-created
|
59
79
|
multi-locale string attributes
|
60
80
|
email:
|
@@ -95,20 +115,20 @@ rdoc_options: []
|
|
95
115
|
require_paths:
|
96
116
|
- lib
|
97
117
|
required_ruby_version: !ruby/object:Gem::Requirement
|
98
|
-
none: false
|
99
118
|
requirements:
|
100
119
|
- - ! '>='
|
101
120
|
- !ruby/object:Gem::Version
|
102
121
|
version: '0'
|
103
|
-
required_rubygems_version: !ruby/object:Gem::Requirement
|
104
122
|
none: false
|
123
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
105
124
|
requirements:
|
106
125
|
- - ! '>='
|
107
126
|
- !ruby/object:Gem::Version
|
108
127
|
version: '0'
|
128
|
+
none: false
|
109
129
|
requirements: []
|
110
130
|
rubyforge_project:
|
111
|
-
rubygems_version: 1.8.
|
131
|
+
rubygems_version: 1.8.24
|
112
132
|
signing_key:
|
113
133
|
specification_version: 3
|
114
134
|
summary: Sets up associations and attribute methods for AR models that have a relational
|