dimarzio-model_translations 0.1.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/.gitignore ADDED
@@ -0,0 +1,3 @@
1
+ *.*~
2
+ *.gem
3
+ mytodo
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 [name of plugin creator]
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,28 @@
1
+ Model Translations
2
+ ==================
3
+
4
+ Plugin for translating data of your models.
5
+
6
+ features:
7
+ * dynamic additions of the languages
8
+ * all translations goes in one 'translations' table (version with the child translations tables for each model is in plans)
9
+
10
+ Currently it uses language locale set for to retrieve and set data.
11
+ It uses I18n.locale to set and retrieve data.
12
+ Currently there is no way to dynamically switch the language except set I18n.locale (will be done in the future version).
13
+
14
+ Example
15
+ =======
16
+
17
+ class Post < ActiveRecord::Base
18
+ translates :title, :content, :author
19
+
20
+ validates_presence_of :title,
21
+ :content
22
+ end
23
+
24
+
25
+ Example goes here.
26
+
27
+
28
+ Copyright (c) 2009 Dmitry Afansyev, released under the MIT license
data/Rakefile ADDED
@@ -0,0 +1,40 @@
1
+ require 'rake'
2
+ require 'rake/testtask'
3
+ require 'rake/rdoctask'
4
+
5
+ desc 'Default: run unit tests.'
6
+ task :default => :test
7
+
8
+ begin
9
+ require 'jeweler'
10
+ Jeweler::Tasks.new do |gem|
11
+ gem.name = "model_translations"
12
+ gem.summary = %Q{Rails plugin for translating data of your models.}
13
+ gem.description = gem.summary
14
+ gem.email = "dimarzio1986@gmail.com"
15
+ gem.homepage = "http://github.com/dimarzio/model_translations"
16
+ gem.authors = ["Dmitry Afanasyev"]
17
+ gem.add_development_dependency "activerecord"
18
+ gem.add_development_dependency "thoughtbot-shoulda"
19
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
20
+ end
21
+ rescue LoadError
22
+ puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
23
+ end
24
+
25
+ desc 'Test the model_translations plugin.'
26
+ Rake::TestTask.new(:test) do |t|
27
+ t.libs << 'lib'
28
+ t.libs << 'test'
29
+ t.pattern = 'test/**/*_test.rb'
30
+ t.verbose = true
31
+ end
32
+
33
+ desc 'Generate documentation for the model_translations plugin.'
34
+ Rake::RDocTask.new(:rdoc) do |rdoc|
35
+ rdoc.rdoc_dir = 'rdoc'
36
+ rdoc.title = 'ModelTranslations'
37
+ rdoc.options << '--line-numbers' << '--inline-source'
38
+ rdoc.rdoc_files.include('README')
39
+ rdoc.rdoc_files.include('lib/**/*.rb')
40
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,8 @@
1
+ Description:
2
+ Explain the generator
3
+
4
+ Example:
5
+ ./script/generate model_translations Thing
6
+
7
+ This will create:
8
+ what/will/it/create
@@ -0,0 +1,8 @@
1
+ class ModelTranslationsGenerator < Rails::Generator::NamedBase
2
+ def manifest
3
+ record do |m|
4
+ # m.directory "lib"
5
+ # m.template 'README', "README"
6
+ end
7
+ end
8
+ end
data/init.rb ADDED
@@ -0,0 +1 @@
1
+ ActiveRecord::Base.send(:include, Dimarzio::ModelTranslations)
@@ -0,0 +1,97 @@
1
+ # option:
2
+ # with (for future use)
3
+ # choices:
4
+ # translations_table - translations for all models in one table
5
+ # same_table - translations in the same table as model
6
+ # child_table - translations in child translations table
7
+
8
+ module Dimarzio
9
+ module ModelTranslations
10
+ def self.included(base)
11
+ base.extend(ClassMethods)
12
+ base.send(:include, InstanceMethods)
13
+ end
14
+
15
+ module ClassMethods
16
+ def translates(*args)
17
+ options = args.extract_options!
18
+ options[:with] ||= :translations_table
19
+
20
+ after_save :save_translated_fields
21
+
22
+ has_many :translations,
23
+ :dependent => :destroy,
24
+ :as => :owner
25
+
26
+ args.each do |field|
27
+ field = field.to_s
28
+
29
+ define_method(field) do
30
+ locale = I18n.locale.to_s
31
+ mem_str = "mem_#{field}_#{locale}"
32
+ mem_sym = mem_str.to_sym
33
+
34
+ make_mem_attr(mem_str)
35
+
36
+ unless self[mem_sym].nil?
37
+ self[mem_sym]
38
+ else
39
+ t = translations.first(:conditions => {
40
+ :field => field,
41
+ :locale => locale
42
+ })
43
+ unless t.nil?
44
+ self[mem_sym] = t.value
45
+ else
46
+ t = translations.find_by_field(field)
47
+ !t.nil? ? t.value : nil
48
+ end
49
+ end
50
+ end
51
+
52
+ define_method("#{field}=") do |value|
53
+ make_mem_attr("mem_#{field}_#{I18n.locale}")
54
+ self["mem_#{field}_#{I18n.locale}".to_sym] = value
55
+ end
56
+
57
+ end
58
+ end
59
+ end
60
+
61
+ module InstanceMethods
62
+ private
63
+ def make_mem_attr(mem_str)
64
+ unless respond_to?(mem_str)
65
+ self.class.class_eval do
66
+ attr_accessor mem_str.to_sym
67
+ end
68
+ end
69
+ end
70
+
71
+ def save_translated_fields
72
+ methods.grep(/^mem_\w+[^=]$/).map do |m|
73
+ m.sub(/^mem_(\w+)$/, '\1')
74
+ end.each do |attr|
75
+ field = attr[0..-4]
76
+ locale = attr[-2..-1]
77
+ value = self["mem_#{field}_#{locale}".to_sym]
78
+
79
+ t = translations.first(:conditions => {
80
+ :field => field,
81
+ :locale => locale
82
+ })
83
+ unless t.nil?
84
+ t.update_attribute :value, value
85
+ else
86
+ translations.create(
87
+ :field => field,
88
+ :locale => locale,
89
+ :value => value
90
+ )
91
+ end
92
+ end
93
+ end
94
+ end
95
+
96
+ end
97
+ end
@@ -0,0 +1,58 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run `rake gemspec`
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{model_translations}
8
+ s.version = "0.1.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Dmitry Afanasyev"]
12
+ s.date = %q{2009-08-16}
13
+ s.description = %q{Rails plugin for translating data of your models.}
14
+ s.email = %q{dimarzio1986@gmail.com}
15
+ s.extra_rdoc_files = [
16
+ "README"
17
+ ]
18
+ s.files = [
19
+ ".gitignore",
20
+ "MIT-LICENSE",
21
+ "README",
22
+ "Rakefile",
23
+ "VERSION",
24
+ "generators/model_translations/USAGE",
25
+ "generators/model_translations/model_translations_generator.rb",
26
+ "init.rb",
27
+ "lib/dimarzio/model_translations.rb",
28
+ "model_translations.gemspec",
29
+ "tasks/model_translations_tasks.rake",
30
+ "test/model_translations_test.rb",
31
+ "test/test_helper.rb"
32
+ ]
33
+ s.homepage = %q{http://github.com/dimarzio/model_translations}
34
+ s.rdoc_options = ["--charset=UTF-8"]
35
+ s.require_paths = ["lib"]
36
+ s.rubygems_version = %q{1.3.5}
37
+ s.summary = %q{Rails plugin for translating data of your models.}
38
+ s.test_files = [
39
+ "test/model_translations_test.rb",
40
+ "test/test_helper.rb"
41
+ ]
42
+
43
+ if s.respond_to? :specification_version then
44
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
45
+ s.specification_version = 3
46
+
47
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
48
+ s.add_development_dependency(%q<activerecord>, [">= 0"])
49
+ s.add_development_dependency(%q<thoughtbot-shoulda>, [">= 0"])
50
+ else
51
+ s.add_dependency(%q<activerecord>, [">= 0"])
52
+ s.add_dependency(%q<thoughtbot-shoulda>, [">= 0"])
53
+ end
54
+ else
55
+ s.add_dependency(%q<activerecord>, [">= 0"])
56
+ s.add_dependency(%q<thoughtbot-shoulda>, [">= 0"])
57
+ end
58
+ end
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :model_translations do
3
+ # # Task goes here
4
+ # end
@@ -0,0 +1,8 @@
1
+ require 'test_helper'
2
+
3
+ class ModelTranslationsTest < ActiveSupport::TestCase
4
+ # Replace this with your real tests.
5
+ test "the truth" do
6
+ assert true
7
+ end
8
+ end
@@ -0,0 +1,3 @@
1
+ require 'rubygems'
2
+ require 'active_support'
3
+ require 'active_support/test_case'
metadata ADDED
@@ -0,0 +1,86 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: dimarzio-model_translations
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Dmitry Afanasyev
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-08-16 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: activerecord
17
+ type: :development
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: "0"
24
+ version:
25
+ - !ruby/object:Gem::Dependency
26
+ name: thoughtbot-shoulda
27
+ type: :development
28
+ version_requirement:
29
+ version_requirements: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: "0"
34
+ version:
35
+ description: Rails plugin for translating data of your models.
36
+ email: dimarzio1986@gmail.com
37
+ executables: []
38
+
39
+ extensions: []
40
+
41
+ extra_rdoc_files:
42
+ - README
43
+ files:
44
+ - .gitignore
45
+ - MIT-LICENSE
46
+ - README
47
+ - Rakefile
48
+ - VERSION
49
+ - generators/model_translations/USAGE
50
+ - generators/model_translations/model_translations_generator.rb
51
+ - init.rb
52
+ - lib/dimarzio/model_translations.rb
53
+ - model_translations.gemspec
54
+ - tasks/model_translations_tasks.rake
55
+ - test/model_translations_test.rb
56
+ - test/test_helper.rb
57
+ has_rdoc: false
58
+ homepage: http://github.com/dimarzio/model_translations
59
+ licenses:
60
+ post_install_message:
61
+ rdoc_options:
62
+ - --charset=UTF-8
63
+ require_paths:
64
+ - lib
65
+ required_ruby_version: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ version: "0"
70
+ version:
71
+ required_rubygems_version: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: "0"
76
+ version:
77
+ requirements: []
78
+
79
+ rubyforge_project:
80
+ rubygems_version: 1.3.5
81
+ signing_key:
82
+ specification_version: 3
83
+ summary: Rails plugin for translating data of your models.
84
+ test_files:
85
+ - test/model_translations_test.rb
86
+ - test/test_helper.rb