single_table_globalize3 0.0.2 → 0.0.4

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 26717252049728b19cb768820301e55d01b6d0d9
4
- data.tar.gz: ddd3692c29e5a96c960150c36209df54ee1c0286
3
+ metadata.gz: 33ca5e81c9622281ce0a4674c337e6fa0399641f
4
+ data.tar.gz: 066d2a063adac33286d5597776e065b0de92e4a6
5
5
  SHA512:
6
- metadata.gz: b73332cb50bfe6a2d706a6ad4450fb471c64dc102264a2310171e12f1ee78d43dfaf59ed8fe8bc14ebddc26cbbcbea5e711b4638057c38168a06266bc90d4745
7
- data.tar.gz: 238fbd2b8c9bb11dec4c5cc3a58a8d529f734171f896d86f1109dd66d38248b31ba36c196b35d6567253ac55d49aec26dcd1ba276245f285dfc6fa906fe33475
6
+ metadata.gz: 54898f214c051bff48fb7969c4d1817da7903a0da432fa6c8b1c916c7bdacb02b00d6dee27f37824351eefc9086e3890296ddb052cd7b0f081346be5825a4d71
7
+ data.tar.gz: 2b8b6a664a08362909499b4ea9b2dba7593449101b1b1a6dd16cf2d91f9a8ebb8ac80e7c40da1c599d58fc94beed3b7ae417d45e65ed5d5c44d1cf5e7e37e8f3
data/Gemfile CHANGED
@@ -1,4 +1,3 @@
1
1
  source 'https://rubygems.org'
2
2
 
3
3
  gemspec
4
-
data/Gemfile.lock CHANGED
@@ -1,40 +1,67 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- single_table_globalize3 (0.0.1)
5
- activemodel (>= 3.0.0)
6
- activerecord (>= 3.0.0)
4
+ single_table_globalize3 (0.0.3)
5
+ activemodel (~> 3.0.0)
6
+ activerecord (~> 3.0.0)
7
+ paper_trail (~> 2)
7
8
 
8
9
  GEM
9
10
  remote: https://rubygems.org/
10
11
  specs:
11
- activemodel (3.2.12)
12
- activesupport (= 3.2.12)
13
- builder (~> 3.0.0)
14
- activerecord (3.2.12)
15
- activemodel (= 3.2.12)
16
- activesupport (= 3.2.12)
17
- arel (~> 3.0.2)
18
- tzinfo (~> 0.3.29)
19
- activesupport (3.2.12)
20
- i18n (~> 0.6)
21
- multi_json (~> 1.0)
22
- arel (3.0.2)
23
- builder (3.0.4)
12
+ abstract (1.0.0)
13
+ actionpack (3.0.20)
14
+ activemodel (= 3.0.20)
15
+ activesupport (= 3.0.20)
16
+ builder (~> 2.1.2)
17
+ erubis (~> 2.6.6)
18
+ i18n (~> 0.5.0)
19
+ rack (~> 1.2.5)
20
+ rack-mount (~> 0.6.14)
21
+ rack-test (~> 0.5.7)
22
+ tzinfo (~> 0.3.23)
23
+ activemodel (3.0.20)
24
+ activesupport (= 3.0.20)
25
+ builder (~> 2.1.2)
26
+ i18n (~> 0.5.0)
27
+ activerecord (3.0.20)
28
+ activemodel (= 3.0.20)
29
+ activesupport (= 3.0.20)
30
+ arel (~> 2.0.10)
31
+ tzinfo (~> 0.3.23)
32
+ activesupport (3.0.20)
33
+ arel (2.0.10)
34
+ builder (2.1.2)
24
35
  database_cleaner (0.6.7)
25
- i18n (0.6.1)
36
+ erubis (2.6.6)
37
+ abstract (>= 1.0.0)
38
+ i18n (0.5.0)
26
39
  json (1.7.7)
27
40
  metaclass (0.0.1)
28
41
  mocha (0.13.2)
29
42
  metaclass (~> 0.0.1)
30
- multi_json (1.6.1)
43
+ paper_trail (2.7.1)
44
+ activerecord (~> 3.0)
45
+ railties (~> 3.0)
31
46
  pathname_local (0.0.2)
47
+ rack (1.2.8)
48
+ rack-mount (0.6.14)
49
+ rack (>= 1.0.0)
50
+ rack-test (0.5.7)
51
+ rack (>= 1.0)
52
+ railties (3.0.20)
53
+ actionpack (= 3.0.20)
54
+ activesupport (= 3.0.20)
55
+ rake (>= 0.8.7)
56
+ rdoc (~> 3.4)
57
+ thor (~> 0.14.4)
32
58
  rake (10.0.3)
33
- rdoc (3.12.1)
59
+ rdoc (3.12.2)
34
60
  json (~> 1.4)
35
61
  sqlite3 (1.3.7)
36
62
  test_declarative (0.0.5)
37
- tzinfo (0.3.35)
63
+ thor (0.14.6)
64
+ tzinfo (0.3.37)
38
65
 
39
66
  PLATFORMS
40
67
  ruby
@@ -4,9 +4,12 @@ module SingleTableGlobalize3
4
4
  class MigrationGenerator < ::ActiveRecord::Generators::Base
5
5
  source_root File.expand_path("../templates", __FILE__)
6
6
  argument :name, :type => :string, :default => "application"
7
+ class_option :with_versioning, :type => :boolean, :default => false, :desc => "Generate migration for versioning"
7
8
 
8
- def copy_devise_migration
9
+ def create_migration_file
9
10
  migration_template "migration.rb", "db/migrate/create_globalize_translations"
11
+ generate "paper_trail:install #{"--force" if options.force?}" if options.with_versioning?
12
+ migration_template "add_locale_column_to_versions.rb", "db/migrate/add_locale_column_to_versions" if options.with_versioning?
10
13
  end
11
14
  end
12
15
  end
@@ -0,0 +1,9 @@
1
+ class AddLocaleColumnToVersions < ActiveRecord::Migration
2
+ def up
3
+ add_column :versions, :locale, :string
4
+ end
5
+
6
+ def down
7
+ remove_column :versions, :locale
8
+ end
9
+ end
@@ -25,10 +25,7 @@ module SingleTableGlobalize3
25
25
 
26
26
  protected
27
27
  def setup_translates!(options)
28
- class_attribute :translated_attribute_names, :translation_options, :fallbacks_for_empty_translations
29
- self.translated_attribute_names = []
30
- self.translation_options = options
31
- self.fallbacks_for_empty_translations = options[:fallbacks_for_empty_translations]
28
+ setup_class_attributes(options)
32
29
 
33
30
  include InstanceMethods
34
31
  extend ClassMethods
@@ -40,6 +37,27 @@ module SingleTableGlobalize3
40
37
 
41
38
  after_create :save_translations!
42
39
  after_update :save_translations!
40
+
41
+ setup_locale_attribute
42
+
43
+ setup_versioning if options[:versioning]
44
+ end
45
+
46
+ def setup_class_attributes(options)
47
+ class_attribute :translated_attribute_names, :translation_options, :fallbacks_for_empty_translations
48
+ self.translated_attribute_names = []
49
+ self.translation_options = options
50
+ self.fallbacks_for_empty_translations = options[:fallbacks_for_empty_translations]
51
+ end
52
+
53
+ def setup_versioning
54
+ ::ActiveRecord::Base.extend(SingleTableGlobalize3::Versioning::PaperTrail)
55
+ has_paper_trail :meta => {:locale => lambda{|record| record.locale}}
56
+ end
57
+
58
+ def setup_locale_attribute
59
+ attr_accessor :locale unless self.attribute_names.include?('locale')
60
+
43
61
  # if attr_accessible is explicitly defined in the class, add locale to it
44
62
  attr_accessible :locale if self.accessible_attributes.to_a.present?
45
63
  end
@@ -55,7 +55,7 @@ module SingleTableGlobalize3
55
55
 
56
56
  def translated_attributes
57
57
  translated_attribute_names.inject({}) do |attributes, name|
58
- attributes.merge(name.to_s => translation[name.to_s].try(:value))
58
+ attributes.merge(name.to_s => read_attribute(name))
59
59
  end
60
60
  end
61
61
 
@@ -13,6 +13,16 @@ module SingleTableGlobalize3
13
13
  end
14
14
  end
15
15
 
16
+ unless respond_to?(:attribute_names)
17
+ def attribute_names
18
+ @attribute_names ||= if !abstract_class? && table_exists?
19
+ column_names
20
+ else
21
+ []
22
+ end
23
+ end
24
+ end
25
+
16
26
  def supported_on_missing?(method_id)
17
27
  return super unless RUBY_VERSION < '1.9' || respond_to?(:translated_attribute_names)
18
28
  match = defined?(::ActiveRecord::DynamicFinderMatch) && (::ActiveRecord::DynamicFinderMatch.match(method_id) || ::ActiveRecord::DynamicScopeMatch.match(method_id))
@@ -64,6 +64,23 @@ module SingleTableGlobalize3
64
64
  return obj
65
65
  end
66
66
 
67
+ def dup
68
+ obj = super
69
+
70
+ obj.instance_variable_set(:@translations, nil)
71
+ obj.instance_variable_set(:@globalize, nil )
72
+ each_locale_and_translated_attribute do |locale, name|
73
+ obj.globalize.write(locale, name, globalize.fetch(locale, name) )
74
+ end
75
+
76
+ return obj
77
+ end
78
+
79
+ def rollback
80
+ instance_variable_set('@globalize', previous_version.try(:globalize))
81
+ self.version = versions.for_this_locale.last
82
+ end
83
+
67
84
  def translation
68
85
  translations_for_locale(::SingleTableGlobalize3.locale)
69
86
  end
@@ -124,6 +141,7 @@ module SingleTableGlobalize3
124
141
  def save_translations!
125
142
  globalize.save_translations!
126
143
  translation_caches.clear
144
+ self.locale = nil unless self.class.attribute_names.include?('locale')
127
145
  end
128
146
 
129
147
  def with_given_locale(attributes, &block)
@@ -1,3 +1,3 @@
1
1
  module SingleTableGlobalize3
2
- VERSION = '0.0.2'
2
+ VERSION = '0.0.4'
3
3
  end
@@ -0,0 +1,52 @@
1
+ require 'paper_trail'
2
+
3
+ module SingleTableGlobalize3
4
+ module Versioning
5
+ module PaperTrail
6
+ # At present this isn't used but we may use something similar in paper trail
7
+ # shortly, so leaving it around to reference easily.
8
+ #def versioned_columns
9
+ #super + self.class.translated_attribute_names
10
+ #end
11
+
12
+ def previous_version
13
+ preceding_version = source_version ? source_version.previous : send(self.class.versions_association_name).for_this_locale.last
14
+ preceding_version.reify if preceding_version
15
+ end
16
+
17
+ def source_version
18
+ version = send(self.class.version_association_name)
19
+ return version if version.try(:locale) == SingleTableGlobalize3.locale.to_s
20
+ end
21
+ end
22
+ end
23
+ end
24
+
25
+ ActiveRecord::Base.class_eval do
26
+ class << self
27
+ def has_paper_trail_with_globalize(*args)
28
+ has_paper_trail_without_globalize(*args)
29
+ include SingleTableGlobalize3::Versioning::PaperTrail
30
+ end
31
+ alias_method_chain :has_paper_trail, :globalize
32
+ end
33
+ end
34
+
35
+ Version.class_eval do
36
+
37
+ before_save do |version|
38
+ version.locale ||= SingleTableGlobalize3.locale.to_s
39
+ end
40
+
41
+ attr_accessible :locale
42
+
43
+ def self.for_this_locale
44
+ locale_str = SingleTableGlobalize3.locale.to_s
45
+ where(:locale => locale_str)
46
+ end
47
+
48
+ def sibling_versions_with_locales
49
+ sibling_versions_without_locales.for_this_locale
50
+ end
51
+ alias_method_chain :sibling_versions, :locales
52
+ end
@@ -0,0 +1,5 @@
1
+ module SingleTableGlobalize3
2
+ module Versioning
3
+ autoload :PaperTrail, 'single_table_globalize3/versioning/paper_trail'
4
+ end
5
+ end
@@ -4,6 +4,7 @@ require 'patches/active_record/uniqueness_validator'
4
4
 
5
5
  module SingleTableGlobalize3
6
6
  autoload :ActiveRecord, 'single_table_globalize3/active_record'
7
+ autoload :Versioning, 'single_table_globalize3/versioning'
7
8
 
8
9
  class << self
9
10
  def locale
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: single_table_globalize3
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sven Fuchs
@@ -14,7 +14,7 @@ authors:
14
14
  autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
- date: 2013-03-13 00:00:00.000000000 Z
17
+ date: 2013-03-15 00:00:00.000000000 Z
18
18
  dependencies:
19
19
  - !ruby/object:Gem::Dependency
20
20
  name: activerecord
@@ -44,6 +44,20 @@ dependencies:
44
44
  - - '>='
45
45
  - !ruby/object:Gem::Version
46
46
  version: 3.0.0
47
+ - !ruby/object:Gem::Dependency
48
+ name: paper_trail
49
+ requirement: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ~>
52
+ - !ruby/object:Gem::Version
53
+ version: '2'
54
+ type: :runtime
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - ~>
59
+ - !ruby/object:Gem::Version
60
+ version: '2'
47
61
  - !ruby/object:Gem::Dependency
48
62
  name: database_cleaner
49
63
  requirement: !ruby/object:Gem::Requirement
@@ -150,6 +164,7 @@ extensions: []
150
164
  extra_rdoc_files: []
151
165
  files:
152
166
  - lib/generators/single_table_globalize3/migration_generator.rb
167
+ - lib/generators/single_table_globalize3/templates/add_locale_column_to_versions.rb
153
168
  - lib/generators/single_table_globalize3/templates/migration.rb
154
169
  - lib/i18n/missing_translations_log_handler.rb
155
170
  - lib/i18n/missing_translations_raise_handler.rb
@@ -166,6 +181,8 @@ files:
166
181
  - lib/single_table_globalize3/active_record/with_translations.rb
167
182
  - lib/single_table_globalize3/active_record.rb
168
183
  - lib/single_table_globalize3/version.rb
184
+ - lib/single_table_globalize3/versioning/paper_trail.rb
185
+ - lib/single_table_globalize3/versioning.rb
169
186
  - lib/single_table_globalize3.rb
170
187
  - Gemfile
171
188
  - Gemfile.lock
@@ -190,7 +207,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
190
207
  version: '0'
191
208
  requirements: []
192
209
  rubyforge_project: '[none]'
193
- rubygems_version: 2.0.0
210
+ rubygems_version: 2.0.3
194
211
  signing_key:
195
212
  specification_version: 4
196
213
  summary: Rails I18n de-facto standard library for ActiveRecord 3 model/data translation