simple_model_translations 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile CHANGED
@@ -3,10 +3,12 @@ source :rubygems
3
3
  gem 'activerecord', '>= 3.0.0'
4
4
 
5
5
  group :test do
6
- gem 'database_cleaner'
7
- gem 'ruby-debug'
8
- gem 'sqlite3-ruby'
9
- gem 'shoulda'
10
- gem 'factory_girl'
6
+ gem 'database_cleaner', '>= 0.5.2'
7
+ gem 'sqlite3-ruby', '>= 1.3.1'
8
+ gem 'shoulda', '>= 2.11.3'
11
9
  gem 'rspec', '>= 2.0.0'
12
10
  end
11
+
12
+ group :console do
13
+ gem 'jeweler', '>= 1.5.0.pre5'
14
+ end
data/Gemfile.lock CHANGED
@@ -14,12 +14,15 @@ GEM
14
14
  arel (1.0.1)
15
15
  activesupport (~> 3.0.0)
16
16
  builder (2.1.2)
17
- columnize (0.3.1)
18
17
  database_cleaner (0.5.2)
19
18
  diff-lcs (1.1.2)
20
- factory_girl (1.3.2)
19
+ git (1.2.5)
21
20
  i18n (0.4.1)
22
- linecache (0.43)
21
+ jeweler (1.5.0.pre5)
22
+ bundler (~> 1.0.0)
23
+ git (>= 1.2.5)
24
+ rake
25
+ rake (0.8.7)
23
26
  rspec (2.0.0)
24
27
  rspec-core (= 2.0.0)
25
28
  rspec-expectations (= 2.0.0)
@@ -30,11 +33,6 @@ GEM
30
33
  rspec-mocks (2.0.0)
31
34
  rspec-core (= 2.0.0)
32
35
  rspec-expectations (= 2.0.0)
33
- ruby-debug (0.10.3)
34
- columnize (>= 0.1)
35
- ruby-debug-base (~> 0.10.3.0)
36
- ruby-debug-base (0.10.3)
37
- linecache (>= 0.3)
38
36
  shoulda (2.11.3)
39
37
  sqlite3-ruby (1.3.1)
40
38
  tzinfo (0.3.23)
@@ -44,9 +42,8 @@ PLATFORMS
44
42
 
45
43
  DEPENDENCIES
46
44
  activerecord (>= 3.0.0)
47
- database_cleaner
48
- factory_girl
45
+ database_cleaner (>= 0.5.2)
46
+ jeweler (>= 1.5.0.pre5)
49
47
  rspec (>= 2.0.0)
50
- ruby-debug
51
- shoulda
52
- sqlite3-ruby
48
+ shoulda (>= 2.11.3)
49
+ sqlite3-ruby (>= 1.3.1)
data/README.rdoc CHANGED
@@ -1,6 +1,7 @@
1
1
  = SimpleModelTranslations
2
2
 
3
- Yet another implementation of ActiveRecord translations. Created for zn.ua, because globalize3 was too unconfortable to use.
3
+ Yet another implementation of ActiveRecord translations. Created for zn.ua, because globalize3 was too uncomfortable to use.
4
+ It borrows some things from globalize3[http://github.com/svenfuchs/globalize3] and puret[http://github.com/jo/puret].
4
5
 
5
6
  == Basic Usage
6
7
 
data/Rakefile CHANGED
@@ -10,18 +10,16 @@ begin
10
10
  gem.email = "fxposter@gmail.com"
11
11
  gem.homepage = "http://github.com/fxposter/simple_model_translations"
12
12
  gem.authors = ["Pavel Forkert"]
13
- gem.add_dependency "activerecord", '>= 3.0.0'
14
13
 
15
- gem.add_development_dependency 'database_cleaner'
16
- gem.add_development_dependency 'ruby-debug'
17
- gem.add_development_dependency 'sqlite3-ruby'
18
- gem.add_development_dependency 'shoulda'
19
- gem.add_development_dependency 'factory_girl'
14
+ gem.add_development_dependency 'database_cleaner', '>= 0.5.2'
15
+ gem.add_development_dependency 'sqlite3-ruby', '>= 1.3.1'
16
+ gem.add_development_dependency 'shoulda', '>= 2.11.3'
20
17
  gem.add_development_dependency 'rspec', '>= 2.0.0'
18
+ gem.add_development_dependency 'jeweler', '>= 1.5.0.pre5'
21
19
  end
22
20
  Jeweler::GemcutterTasks.new
23
21
  rescue LoadError
24
- puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
22
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler --pre"
25
23
  end
26
24
 
27
25
  require 'rspec/core/rake_task'
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.0
1
+ 0.1.1
@@ -42,15 +42,19 @@ module SimpleModelTranslations
42
42
  def configure_translations(options)
43
43
  raise 'You can call #translates method only once per model!' if included_modules.include?(SimpleModelTranslations::InstanceMethods)
44
44
 
45
+ (class << self; self; end).send :define_method, :translation_options do
46
+ options
47
+ end
48
+
45
49
  include SimpleModelTranslations::InstanceMethods
46
50
  include SimpleModelTranslations::Attributes
51
+ extend SimpleModelTranslations::ClassMethods
47
52
 
48
- class_name = options[:class_name] || "#{self.name}Translation"
49
53
  # unless Kernel.const_get(class_name)
50
54
  # klass = Kernel.const_set(class_name.to_sym, Class.new(ActiveRecord::Base))
51
55
  # klass.translation_for(self.name.underscore.to_sym)
52
56
  # end
53
- has_many :translations, :class_name => class_name, :dependent => :destroy, :order => "created_at DESC", :autosave => true
57
+ has_many :translations, :class_name => translation_class_name, :dependent => :destroy, :autosave => true
54
58
 
55
59
  if options[:accepts_nested_attributes]
56
60
  accepts_nested_attributes_for :translations, :allow_destroy => true
@@ -1,2 +1,26 @@
1
1
  module SimpleModelTranslations
2
+ module ClassMethods
3
+ def with_translations
4
+ includes(:translations).where("#{translated_column_name(:id)} IS NOT NULL")
5
+ end
6
+
7
+ def translation_class
8
+ Module.const_get(translation_class_name)
9
+ end
10
+
11
+ def translation_class_name
12
+ class_name = translation_options[:class_name] || "#{self.name}Translation"
13
+ end
14
+
15
+ def translated_column_name(name)
16
+ "#{translation_class.table_name}.#{name}".to_sym
17
+ end
18
+
19
+ def with_translated_attribute(name, value, locales = nil)
20
+ with_translations.where(
21
+ translated_column_name(name) => value,
22
+ translated_column_name(:locale) => Array(locales).map(&:to_s)
23
+ )
24
+ end
25
+ end
2
26
  end
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{simple_model_translations}
8
- s.version = "0.1.0"
8
+ s.version = "0.1.1"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Pavel Forkert"]
@@ -14,43 +14,43 @@ Gem::Specification.new do |s|
14
14
  s.email = %q{fxposter@gmail.com}
15
15
  s.extra_rdoc_files = [
16
16
  "LICENSE",
17
- "README.rdoc"
17
+ "README.rdoc"
18
18
  ]
19
19
  s.files = [
20
20
  ".document",
21
- ".gitignore",
22
- "Gemfile",
23
- "Gemfile.lock",
24
- "LICENSE",
25
- "README.rdoc",
26
- "Rakefile",
27
- "VERSION",
28
- "lib/simple_model_translations.rb",
29
- "lib/simple_model_translations/attributes.rb",
30
- "lib/simple_model_translations/base.rb",
31
- "lib/simple_model_translations/class_methods.rb",
32
- "lib/simple_model_translations/instance_methods.rb",
33
- "lib/simple_model_translations/validations.rb",
34
- "simple_model_translations.gemspec",
35
- "spec/attributes_spec.rb",
36
- "spec/data/models.rb",
37
- "spec/data/schema.rb",
38
- "spec/simple_model_translations_spec.rb",
39
- "spec/spec_helper.rb",
40
- "spec/validations_spec.rb"
21
+ "Gemfile",
22
+ "Gemfile.lock",
23
+ "LICENSE",
24
+ "README.rdoc",
25
+ "Rakefile",
26
+ "VERSION",
27
+ "lib/simple_model_translations.rb",
28
+ "lib/simple_model_translations/attributes.rb",
29
+ "lib/simple_model_translations/base.rb",
30
+ "lib/simple_model_translations/class_methods.rb",
31
+ "lib/simple_model_translations/instance_methods.rb",
32
+ "lib/simple_model_translations/validations.rb",
33
+ "simple_model_translations.gemspec",
34
+ "spec/attributes_spec.rb",
35
+ "spec/class_methods_spec.rb",
36
+ "spec/data/models.rb",
37
+ "spec/data/schema.rb",
38
+ "spec/simple_model_translations_spec.rb",
39
+ "spec/spec_helper.rb",
40
+ "spec/validations_spec.rb"
41
41
  ]
42
42
  s.homepage = %q{http://github.com/fxposter/simple_model_translations}
43
- s.rdoc_options = ["--charset=UTF-8"]
44
43
  s.require_paths = ["lib"]
45
44
  s.rubygems_version = %q{1.3.7}
46
45
  s.summary = %q{Simple ActiveRecord translations for Rails 3}
47
46
  s.test_files = [
48
47
  "spec/attributes_spec.rb",
49
- "spec/data/models.rb",
50
- "spec/data/schema.rb",
51
- "spec/simple_model_translations_spec.rb",
52
- "spec/spec_helper.rb",
53
- "spec/validations_spec.rb"
48
+ "spec/class_methods_spec.rb",
49
+ "spec/data/models.rb",
50
+ "spec/data/schema.rb",
51
+ "spec/simple_model_translations_spec.rb",
52
+ "spec/spec_helper.rb",
53
+ "spec/validations_spec.rb"
54
54
  ]
55
55
 
56
56
  if s.respond_to? :specification_version then
@@ -59,29 +59,26 @@ Gem::Specification.new do |s|
59
59
 
60
60
  if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
61
61
  s.add_runtime_dependency(%q<activerecord>, [">= 3.0.0"])
62
- s.add_development_dependency(%q<database_cleaner>, [">= 0"])
63
- s.add_development_dependency(%q<ruby-debug>, [">= 0"])
64
- s.add_development_dependency(%q<sqlite3-ruby>, [">= 0"])
65
- s.add_development_dependency(%q<shoulda>, [">= 0"])
66
- s.add_development_dependency(%q<factory_girl>, [">= 0"])
62
+ s.add_development_dependency(%q<database_cleaner>, [">= 0.5.2"])
63
+ s.add_development_dependency(%q<sqlite3-ruby>, [">= 1.3.1"])
64
+ s.add_development_dependency(%q<shoulda>, [">= 2.11.3"])
67
65
  s.add_development_dependency(%q<rspec>, [">= 2.0.0"])
66
+ s.add_development_dependency(%q<jeweler>, [">= 1.5.0.pre5"])
68
67
  else
69
68
  s.add_dependency(%q<activerecord>, [">= 3.0.0"])
70
- s.add_dependency(%q<database_cleaner>, [">= 0"])
71
- s.add_dependency(%q<ruby-debug>, [">= 0"])
72
- s.add_dependency(%q<sqlite3-ruby>, [">= 0"])
73
- s.add_dependency(%q<shoulda>, [">= 0"])
74
- s.add_dependency(%q<factory_girl>, [">= 0"])
69
+ s.add_dependency(%q<database_cleaner>, [">= 0.5.2"])
70
+ s.add_dependency(%q<sqlite3-ruby>, [">= 1.3.1"])
71
+ s.add_dependency(%q<shoulda>, [">= 2.11.3"])
75
72
  s.add_dependency(%q<rspec>, [">= 2.0.0"])
73
+ s.add_dependency(%q<jeweler>, [">= 1.5.0.pre5"])
76
74
  end
77
75
  else
78
76
  s.add_dependency(%q<activerecord>, [">= 3.0.0"])
79
- s.add_dependency(%q<database_cleaner>, [">= 0"])
80
- s.add_dependency(%q<ruby-debug>, [">= 0"])
81
- s.add_dependency(%q<sqlite3-ruby>, [">= 0"])
82
- s.add_dependency(%q<shoulda>, [">= 0"])
83
- s.add_dependency(%q<factory_girl>, [">= 0"])
77
+ s.add_dependency(%q<database_cleaner>, [">= 0.5.2"])
78
+ s.add_dependency(%q<sqlite3-ruby>, [">= 1.3.1"])
79
+ s.add_dependency(%q<shoulda>, [">= 2.11.3"])
84
80
  s.add_dependency(%q<rspec>, [">= 2.0.0"])
81
+ s.add_dependency(%q<jeweler>, [">= 1.5.0.pre5"])
85
82
  end
86
83
  end
87
84
 
@@ -0,0 +1,25 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe Article do
4
+ it 'should be able to return only records which have translations' do
5
+ first_article = Article.create!(:slug => 'hello')
6
+ second_article = Article.create!(:slug => 'world', :name => 'WORLD')
7
+ Article.with_translations.to_a.should == [second_article]
8
+ end
9
+
10
+ it 'should be able to return translation class' do
11
+ Article.translation_class.should == ArticleTranslation
12
+ end
13
+
14
+ it 'should be able to return translated column name' do
15
+ Article.translated_column_name(:name).should == :'article_translations.name'
16
+ end
17
+
18
+ it 'should be able to search by specific column names and locales' do
19
+ first_article = Article.create!(:slug => 'hello', :locale => :en, :name => 'hello')
20
+ second_article = Article.create!(:slug => 'world', :name => 'WORLD')
21
+ third_article = Article.create!(:slug => 'hello')
22
+ fourth_article = Article.create!(:slug => 'world', :name => 'WORLD', :locale => :en)
23
+ Article.with_translated_attribute(:name, ['hello'], :en).should == [first_article]
24
+ end
25
+ end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: simple_model_translations
3
3
  version: !ruby/object:Gem::Version
4
- hash: 27
4
+ hash: 25
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 1
9
- - 0
10
- version: 0.1.0
9
+ - 1
10
+ version: 0.1.1
11
11
  platform: ruby
12
12
  authors:
13
13
  - Pavel Forkert
@@ -19,9 +19,10 @@ date: 2010-10-14 00:00:00 +03:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
22
- name: activerecord
23
22
  prerelease: false
24
- requirement: &id001 !ruby/object:Gem::Requirement
23
+ type: :runtime
24
+ name: activerecord
25
+ version_requirements: &id001 !ruby/object:Gem::Requirement
25
26
  none: false
26
27
  requirements:
27
28
  - - ">="
@@ -32,94 +33,88 @@ dependencies:
32
33
  - 0
33
34
  - 0
34
35
  version: 3.0.0
35
- type: :runtime
36
- version_requirements: *id001
36
+ requirement: *id001
37
37
  - !ruby/object:Gem::Dependency
38
- name: database_cleaner
39
38
  prerelease: false
40
- requirement: &id002 !ruby/object:Gem::Requirement
39
+ type: :development
40
+ name: database_cleaner
41
+ version_requirements: &id002 !ruby/object:Gem::Requirement
41
42
  none: false
42
43
  requirements:
43
44
  - - ">="
44
45
  - !ruby/object:Gem::Version
45
- hash: 3
46
+ hash: 15
46
47
  segments:
47
48
  - 0
48
- version: "0"
49
- type: :development
50
- version_requirements: *id002
49
+ - 5
50
+ - 2
51
+ version: 0.5.2
52
+ requirement: *id002
51
53
  - !ruby/object:Gem::Dependency
52
- name: ruby-debug
53
54
  prerelease: false
54
- requirement: &id003 !ruby/object:Gem::Requirement
55
- none: false
56
- requirements:
57
- - - ">="
58
- - !ruby/object:Gem::Version
59
- hash: 3
60
- segments:
61
- - 0
62
- version: "0"
63
55
  type: :development
64
- version_requirements: *id003
65
- - !ruby/object:Gem::Dependency
66
56
  name: sqlite3-ruby
67
- prerelease: false
68
- requirement: &id004 !ruby/object:Gem::Requirement
57
+ version_requirements: &id003 !ruby/object:Gem::Requirement
69
58
  none: false
70
59
  requirements:
71
60
  - - ">="
72
61
  - !ruby/object:Gem::Version
73
- hash: 3
62
+ hash: 25
74
63
  segments:
75
- - 0
76
- version: "0"
77
- type: :development
78
- version_requirements: *id004
64
+ - 1
65
+ - 3
66
+ - 1
67
+ version: 1.3.1
68
+ requirement: *id003
79
69
  - !ruby/object:Gem::Dependency
80
- name: shoulda
81
70
  prerelease: false
82
- requirement: &id005 !ruby/object:Gem::Requirement
71
+ type: :development
72
+ name: shoulda
73
+ version_requirements: &id004 !ruby/object:Gem::Requirement
83
74
  none: false
84
75
  requirements:
85
76
  - - ">="
86
77
  - !ruby/object:Gem::Version
87
- hash: 3
78
+ hash: 37
88
79
  segments:
89
- - 0
90
- version: "0"
91
- type: :development
92
- version_requirements: *id005
80
+ - 2
81
+ - 11
82
+ - 3
83
+ version: 2.11.3
84
+ requirement: *id004
93
85
  - !ruby/object:Gem::Dependency
94
- name: factory_girl
95
86
  prerelease: false
96
- requirement: &id006 !ruby/object:Gem::Requirement
87
+ type: :development
88
+ name: rspec
89
+ version_requirements: &id005 !ruby/object:Gem::Requirement
97
90
  none: false
98
91
  requirements:
99
92
  - - ">="
100
93
  - !ruby/object:Gem::Version
101
- hash: 3
94
+ hash: 15
102
95
  segments:
96
+ - 2
103
97
  - 0
104
- version: "0"
105
- type: :development
106
- version_requirements: *id006
98
+ - 0
99
+ version: 2.0.0
100
+ requirement: *id005
107
101
  - !ruby/object:Gem::Dependency
108
- name: rspec
109
102
  prerelease: false
110
- requirement: &id007 !ruby/object:Gem::Requirement
103
+ type: :development
104
+ name: jeweler
105
+ version_requirements: &id006 !ruby/object:Gem::Requirement
111
106
  none: false
112
107
  requirements:
113
108
  - - ">="
114
109
  - !ruby/object:Gem::Version
115
- hash: 15
110
+ hash: -1876988220
116
111
  segments:
117
- - 2
118
- - 0
112
+ - 1
113
+ - 5
119
114
  - 0
120
- version: 2.0.0
121
- type: :development
122
- version_requirements: *id007
115
+ - pre5
116
+ version: 1.5.0.pre5
117
+ requirement: *id006
123
118
  description: Simple ActiveRecord translations for Rails 3
124
119
  email: fxposter@gmail.com
125
120
  executables: []
@@ -131,7 +126,6 @@ extra_rdoc_files:
131
126
  - README.rdoc
132
127
  files:
133
128
  - .document
134
- - .gitignore
135
129
  - Gemfile
136
130
  - Gemfile.lock
137
131
  - LICENSE
@@ -146,6 +140,7 @@ files:
146
140
  - lib/simple_model_translations/validations.rb
147
141
  - simple_model_translations.gemspec
148
142
  - spec/attributes_spec.rb
143
+ - spec/class_methods_spec.rb
149
144
  - spec/data/models.rb
150
145
  - spec/data/schema.rb
151
146
  - spec/simple_model_translations_spec.rb
@@ -156,8 +151,8 @@ homepage: http://github.com/fxposter/simple_model_translations
156
151
  licenses: []
157
152
 
158
153
  post_install_message:
159
- rdoc_options:
160
- - --charset=UTF-8
154
+ rdoc_options: []
155
+
161
156
  require_paths:
162
157
  - lib
163
158
  required_ruby_version: !ruby/object:Gem::Requirement
@@ -187,6 +182,7 @@ specification_version: 3
187
182
  summary: Simple ActiveRecord translations for Rails 3
188
183
  test_files:
189
184
  - spec/attributes_spec.rb
185
+ - spec/class_methods_spec.rb
190
186
  - spec/data/models.rb
191
187
  - spec/data/schema.rb
192
188
  - spec/simple_model_translations_spec.rb
data/.gitignore DELETED
@@ -1,24 +0,0 @@
1
- ## MAC OS
2
- .DS_Store
3
-
4
- ## TEXTMATE
5
- *.tmproj
6
- tmtags
7
-
8
- ## EMACS
9
- *~
10
- \#*
11
- .\#*
12
-
13
- ## VIM
14
- *.swp
15
-
16
- ## PROJECT::GENERAL
17
- coverage
18
- rdoc
19
- pkg
20
-
21
- ## PROJECT::SPECIFIC
22
- .rspec
23
- .rvmrc
24
- .bundle