acts_as_localizable 0.2.0

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/.document ADDED
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
data/.gitignore ADDED
@@ -0,0 +1,21 @@
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
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 davydotcom
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README ADDED
@@ -0,0 +1,2 @@
1
+ acts_as_localizable README
2
+
data/README.rdoc ADDED
@@ -0,0 +1,17 @@
1
+ = acts_as_localizable
2
+
3
+ Description goes here.
4
+
5
+ == Note on Patches/Pull Requests
6
+
7
+ * Fork the project.
8
+ * Make your feature addition or bug fix.
9
+ * Add tests for it. This is important so I don't break it in a
10
+ future version unintentionally.
11
+ * Commit, do not mess with rakefile, version, or history.
12
+ (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
13
+ * Send me a pull request. Bonus points for topic branches.
14
+
15
+ == Copyright
16
+
17
+ Copyright (c) 2010 davydotcom. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,53 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "acts_as_localizable"
8
+ gem.summary = "Used to store localized fields in your rails database for individual tables"
9
+ gem.description = "Set ActiveRecord field values or retrieve them based on the I18n current locale or by manually specifying"
10
+ gem.email = "david.estes@just1word.com"
11
+ gem.homepage = "http://github.com/davydotcom/acts_as_localizable"
12
+ gem.authors = ["davydotcom"]
13
+ gem.add_development_dependency "thoughtbot-shoulda", ">= 0"
14
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
15
+ end
16
+ Jeweler::GemcutterTasks.new
17
+ rescue LoadError
18
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
19
+ end
20
+
21
+ require 'rake/testtask'
22
+ Rake::TestTask.new(:test) do |test|
23
+ test.libs << 'lib' << 'test'
24
+ test.pattern = 'test/**/test_*.rb'
25
+ test.verbose = true
26
+ end
27
+
28
+ begin
29
+ require 'rcov/rcovtask'
30
+ Rcov::RcovTask.new do |test|
31
+ test.libs << 'test'
32
+ test.pattern = 'test/**/test_*.rb'
33
+ test.verbose = true
34
+ end
35
+ rescue LoadError
36
+ task :rcov do
37
+ abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
38
+ end
39
+ end
40
+
41
+ task :test => :check_dependencies
42
+
43
+ task :default => :test
44
+
45
+ require 'rake/rdoctask'
46
+ Rake::RDocTask.new do |rdoc|
47
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
48
+
49
+ rdoc.rdoc_dir = 'rdoc'
50
+ rdoc.title = "acts_as_localizable #{version}"
51
+ rdoc.rdoc_files.include('README*')
52
+ rdoc.rdoc_files.include('lib/**/*.rb')
53
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.2.0
@@ -0,0 +1,61 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{acts_as_localizable}
8
+ s.version = "0.2.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["davydotcom"]
12
+ s.date = %q{2010-11-10}
13
+ s.description = %q{Set ActiveRecord field values or retrieve them based on the I18n current locale or by manually specifying}
14
+ s.email = %q{david.estes@just1word.com}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE",
17
+ "README",
18
+ "README.rdoc"
19
+ ]
20
+ s.files = [
21
+ ".document",
22
+ ".gitignore",
23
+ "LICENSE",
24
+ "README",
25
+ "README.rdoc",
26
+ "Rakefile",
27
+ "VERSION",
28
+ "acts_as_localizable.gemspec",
29
+ "app/models/localized_field.rb",
30
+ "init.rb",
31
+ "lib/acts_as_localizable.rb",
32
+ "lib/acts_as_localizable/engine.rb",
33
+ "lib/generators/localized_fields/localized_fields_generator.rb",
34
+ "lib/generators/localized_fields/templates/migration.rb",
35
+ "test/helper.rb",
36
+ "test/test_acts_as_localizable.rb"
37
+ ]
38
+ s.homepage = %q{http://github.com/davydotcom/acts_as_localizable}
39
+ s.rdoc_options = ["--charset=UTF-8"]
40
+ s.require_paths = ["lib"]
41
+ s.rubygems_version = %q{1.3.7}
42
+ s.summary = %q{Used to store localized fields in your rails database for individual tables}
43
+ s.test_files = [
44
+ "test/helper.rb",
45
+ "test/test_acts_as_localizable.rb"
46
+ ]
47
+
48
+ if s.respond_to? :specification_version then
49
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
50
+ s.specification_version = 3
51
+
52
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
53
+ s.add_development_dependency(%q<thoughtbot-shoulda>, [">= 0"])
54
+ else
55
+ s.add_dependency(%q<thoughtbot-shoulda>, [">= 0"])
56
+ end
57
+ else
58
+ s.add_dependency(%q<thoughtbot-shoulda>, [">= 0"])
59
+ end
60
+ end
61
+
@@ -0,0 +1,3 @@
1
+ class LocalizedField < ActiveRecord::Base
2
+ belongs_to :localized_object,:polymorphic => true
3
+ end
data/init.rb ADDED
@@ -0,0 +1,4 @@
1
+ # Include hook code here
2
+ ActiveRecord::Base.class_eval do
3
+ include ActsAsLocalizable
4
+ end
@@ -0,0 +1,16 @@
1
+ require 'acts_as_localizable'
2
+ require 'rails'
3
+
4
+ module ActsAsLocalizable
5
+ class Engine < Rails::Engine
6
+ initializer "acts_as_localizable.init" do |app|
7
+ ActiveRecord::Base.class_eval do
8
+ include ActsAsLocalizable::AR
9
+ end
10
+
11
+ end
12
+ end
13
+
14
+
15
+ end
16
+
@@ -0,0 +1,119 @@
1
+ require 'acts_as_localizable/engine' if defined?(Rails)
2
+
3
+ module ActsAsLocalizable::AR
4
+ def self.included(base)
5
+ base.extend(ClassMethods)
6
+ end
7
+
8
+ class Config
9
+ attr_reader :default_locale
10
+ attr_reader :model_id
11
+ def initialize(model_id,default_locale)
12
+ @model_id = model_id
13
+ @default_locale = default_locale.to_s.downcase
14
+ end
15
+ end
16
+ class LocalValues
17
+ def initialize(locale,default_values)
18
+ self.instance_variable_set("@locale",locale)
19
+ self.instance_variable_set("@modified",false)
20
+ self.instance_variable_set("@modified_attributes",[])
21
+ default_values.each_pair do |key,value|
22
+
23
+ self.instance_variable_set("@#{key}",value)
24
+ self.class_eval("attr_reader :#{key}")
25
+
26
+ self.class_eval("def #{key}=(value)
27
+ @modified = true
28
+ @modified_attributes << '#{key}'
29
+ instance_variable_set('@#{key}',value)
30
+ end")
31
+ end
32
+ end
33
+
34
+ def modified?
35
+ return self.instance_variable_get("@modified")
36
+ end
37
+ def modified_attributes
38
+ return self.instance_variable_get("@modified_attributes")
39
+ end
40
+ def locale
41
+ return self.instance_variable_get("@locale")
42
+ end
43
+ end
44
+
45
+ module ClassMethods
46
+ def acts_as_localizable(default_locale=nil)
47
+ if default_locale == nil
48
+ default_locale = I18n.default_locale
49
+ end
50
+ model_id = self.to_s.split('::').last.pluralize.singularize.underscore
51
+ default_locale = 'en' unless default_locale
52
+ @acts_as_localizable_config = ActsAsLocalizable::AR::Config.new(model_id,default_locale)
53
+ self.superclass.class_eval do
54
+ has_many :localized_fields, :as => :localized_object,:dependent => :destroy
55
+ end
56
+ self.class_eval do
57
+ has_many :localized_fields, :as => :localized_object,:dependent => :destroy
58
+ default_scope :include => [:localized_fields]
59
+ attr_accessor :localized_cache
60
+ after_save :save_localized_attributes
61
+ private
62
+
63
+ def save_localized_attributes
64
+ return if self.localized_cache.blank?
65
+ self.localized_cache.each_pair do |key,locale_object|
66
+ locale_object.modified_attributes.each do |modified_field|
67
+ fields = self.localized_fields.select {|localized_field| localized_field.locale.downcase == key.to_s.downcase && localized_field.field_name == modified_field}
68
+ if fields.blank?
69
+ field = self.localized_fields.new(:locale => key.to_s.downcase,:field_name => modified_field,:value => locale_object.instance_variable_get("@#{modified_field}"))
70
+ field.save
71
+ else
72
+ field = fields.first
73
+ field.value = locale_object.instance_variable_get("@#{modified_field}")
74
+ field.save
75
+ end
76
+ end
77
+
78
+ end
79
+ end
80
+ end
81
+ # self.superclass.send(:default_scope){{:include => [:localized_fields]}}
82
+ # self.superclass.default_scoping << {:include => [:localized_fields]}
83
+ include ActsAsLocalizable::AR::InstanceMethods
84
+ end
85
+ def acts_as_localizable_config
86
+ @acts_as_localizable_config || self.superclass.instance_variable_get('@acts_as_localizable_config')
87
+ end
88
+
89
+ end
90
+
91
+ module InstanceMethods
92
+ # has_many :localized_fields,:as => :object
93
+
94
+ def localized(locale=nil)
95
+ if locale == nil
96
+ locale = I18n.locale
97
+ end
98
+ default_values = self.attributes
99
+ if self.class.acts_as_localizable_config.default_locale.downcase != locale.to_s.downcase && (self.localized_cache.blank? == true || self.localized_cache[locale].blank? == true)
100
+ fields = self.localized_fields.select {|localized_field| localized_field.locale.downcase == locale.to_s.downcase}
101
+
102
+ if fields.blank? == false
103
+ fields.each do |field|
104
+ default_values[field.field_name] = field.value;
105
+ end
106
+ end
107
+ end
108
+ if self.localized_cache == nil
109
+ self.localized_cache = {locale => ActsAsLocalizable::AR::LocalValues.new(locale,default_values)}
110
+ elsif self.localized_cache[locale].blank?
111
+ self.localized_cache[locale] = ActsAsLocalizable::AR::LocalValues.new(locale,default_values)
112
+ end
113
+ return self.localized_cache[locale]
114
+ end
115
+
116
+
117
+
118
+ end
119
+ end
@@ -0,0 +1,20 @@
1
+ # lib/generators/localized_field/localized_field_generator.rb
2
+ require 'rails/generators'
3
+ class LocalizedFieldsGenerator < Rails::Generators::Base
4
+ include Rails::Generators::Migration
5
+ def self.source_root
6
+ @source_root ||= File.join(File.dirname(__FILE__), 'templates')
7
+ end
8
+
9
+ def self.next_migration_number(dirname)
10
+ if ActiveRecord::Base.timestamped_migrations
11
+ Time.now.utc.strftime("%Y%m%d%H%M%S")
12
+ else
13
+ "%.3d" % (current_migration_number(dirname) + 1)
14
+ end
15
+ end
16
+
17
+ def create_migration_file
18
+ migration_template 'migration.rb', 'db/migrate/create_localized_fields.rb'
19
+ end
20
+ end
@@ -0,0 +1,19 @@
1
+ class CreateLocalizedFields < ActiveRecord::Migration
2
+ def self.up
3
+ create_table :localized_fields do |t|
4
+ t.string :localized_object_type
5
+ t.integer :localized_object_id
6
+ t.string :locale
7
+ t.string :field_name
8
+ t.text :value
9
+ #Any additional fields here
10
+
11
+ t.timestamps
12
+ end
13
+ add_index :localized_fields,[:localized_object_type,:localized_object_type_id]
14
+ end
15
+
16
+ def self.down
17
+ drop_table :localized_fields
18
+ end
19
+ end
data/test/helper.rb ADDED
@@ -0,0 +1,10 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'shoulda'
4
+
5
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
6
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
7
+ require 'acts_as_localizable'
8
+
9
+ class Test::Unit::TestCase
10
+ end
@@ -0,0 +1,7 @@
1
+ require 'helper'
2
+
3
+ class TestActsAsLocalizable < Test::Unit::TestCase
4
+ should "probably rename this file and start testing for real" do
5
+ flunk "hey buddy, you should probably rename this file and start testing for real"
6
+ end
7
+ end
metadata ADDED
@@ -0,0 +1,98 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: acts_as_localizable
3
+ version: !ruby/object:Gem::Version
4
+ hash: 23
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 2
9
+ - 0
10
+ version: 0.2.0
11
+ platform: ruby
12
+ authors:
13
+ - davydotcom
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-11-10 00:00:00 -05:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: thoughtbot-shoulda
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 3
30
+ segments:
31
+ - 0
32
+ version: "0"
33
+ type: :development
34
+ version_requirements: *id001
35
+ description: Set ActiveRecord field values or retrieve them based on the I18n current locale or by manually specifying
36
+ email: david.estes@just1word.com
37
+ executables: []
38
+
39
+ extensions: []
40
+
41
+ extra_rdoc_files:
42
+ - LICENSE
43
+ - README
44
+ - README.rdoc
45
+ files:
46
+ - .document
47
+ - .gitignore
48
+ - LICENSE
49
+ - README
50
+ - README.rdoc
51
+ - Rakefile
52
+ - VERSION
53
+ - acts_as_localizable.gemspec
54
+ - app/models/localized_field.rb
55
+ - init.rb
56
+ - lib/acts_as_localizable.rb
57
+ - lib/acts_as_localizable/engine.rb
58
+ - lib/generators/localized_fields/localized_fields_generator.rb
59
+ - lib/generators/localized_fields/templates/migration.rb
60
+ - test/helper.rb
61
+ - test/test_acts_as_localizable.rb
62
+ has_rdoc: true
63
+ homepage: http://github.com/davydotcom/acts_as_localizable
64
+ licenses: []
65
+
66
+ post_install_message:
67
+ rdoc_options:
68
+ - --charset=UTF-8
69
+ require_paths:
70
+ - lib
71
+ required_ruby_version: !ruby/object:Gem::Requirement
72
+ none: false
73
+ requirements:
74
+ - - ">="
75
+ - !ruby/object:Gem::Version
76
+ hash: 3
77
+ segments:
78
+ - 0
79
+ version: "0"
80
+ required_rubygems_version: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ">="
84
+ - !ruby/object:Gem::Version
85
+ hash: 3
86
+ segments:
87
+ - 0
88
+ version: "0"
89
+ requirements: []
90
+
91
+ rubyforge_project:
92
+ rubygems_version: 1.3.7
93
+ signing_key:
94
+ specification_version: 3
95
+ summary: Used to store localized fields in your rails database for individual tables
96
+ test_files:
97
+ - test/helper.rb
98
+ - test/test_acts_as_localizable.rb