has_translatable_attributes 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.rvmrc ADDED
@@ -0,0 +1 @@
1
+ rvm use @rubygems
data/Gemfile ADDED
@@ -0,0 +1,9 @@
1
+ source 'https://rubygems.org'
2
+
3
+ group :test do
4
+ gem 'rails', '> 3.2.0'
5
+ gem 'sqlite3'
6
+ end
7
+
8
+ # Specify your gem's dependencies in has_translatable_attributes.gemspec
9
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Philip Hallstrom
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,44 @@
1
+ # HasTranslatableAttributes
2
+
3
+ [![Gem Version](https://badge.fury.io/rb/has_translatable_attributes.png)](http://badge.fury.io/rb/has_translatable_attributes)
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'has_translatable_attributes'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install has_translatable_attributes
18
+
19
+ ## Usage
20
+
21
+ # Assume fields :content_en, :content_es
22
+ class BlogPost < ActiveRecord::Base
23
+ has_translatable_attributes
24
+ end
25
+
26
+ bp = BlogPost.new(...)
27
+
28
+ I18n.locale = :en
29
+ bp.content == bp.content_en
30
+ bp.content = "Enlish"
31
+ bp.content_en == "English"
32
+
33
+ I18n.locale = :es
34
+ bp.content == bp.content_es
35
+ bp.content = "Spanish"
36
+ bp.content_es == "Spanish"
37
+
38
+ ## Contributing
39
+
40
+ 1. Fork it
41
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
42
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
43
+ 4. Push to the branch (`git push origin my-new-feature`)
44
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,9 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ require 'rake/testtask'
4
+ Rake::TestTask.new(:test) do |test|
5
+ test.libs << 'lib'
6
+ test.libs << 'test'
7
+ test.pattern = 'test/**/test_*.rb'
8
+ test.verbose = true
9
+ end
@@ -0,0 +1,19 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'has_translatable_attributes/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "has_translatable_attributes"
8
+ gem.version = HasTranslatableAttributes::VERSION
9
+ gem.authors = ["Philip Hallstrom"]
10
+ gem.email = ["philip@pjkh.com"]
11
+ gem.description = %q{Provides convenience methods for setting/getting I18n specific fields.}
12
+ gem.summary = %q{Provides convenience methods for setting/getting I18n specific fields.}
13
+ gem.homepage = "https://github.com/phallstrom/has_translatable_attributes"
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ["lib"]
19
+ end
@@ -0,0 +1,3 @@
1
+ module HasTranslatableAttributes
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,49 @@
1
+ require "has_translatable_attributes/version"
2
+
3
+ module HasTranslatableAttributes
4
+
5
+ module Extensions
6
+
7
+ def self.included(base)
8
+ base.extend(ClassMethods)
9
+ end
10
+
11
+ module ClassMethods
12
+ def has_translatable_attributes(options = {})
13
+
14
+ column_names.select{|name| name =~ /_#{I18n.default_locale}$/}.each do |name_i18n|
15
+ name = name_i18n.sub(/_#{I18n.default_locale}$/, '')
16
+ next if columns_hash.key? name
17
+
18
+ class_eval <<-"EOV"
19
+
20
+ class << self
21
+ def find_by_#{name}(*arguments)
22
+ send("find_by_#{name}_#\{I18n.locale\}", *arguments)
23
+ end
24
+ end
25
+
26
+ def #{name}
27
+ if (val = send("#{name}_#\{I18n.locale\}")).blank? && I18n.locale != I18n.default_locale
28
+ send("#{name}_#\{I18n.default_locale\}")
29
+ else
30
+ val
31
+ end
32
+ end
33
+
34
+ def #{name}=(*arguments, &block)
35
+ send("#{name}_#\{I18n.locale\}=", *arguments, &block)
36
+ end
37
+ EOV
38
+ end
39
+
40
+ end
41
+ end
42
+
43
+ module InstanceMethods
44
+ end
45
+
46
+ end
47
+ end
48
+
49
+ ActiveRecord::Base.send :include, HasTranslatableAttributes::Extensions
@@ -0,0 +1,114 @@
1
+ require 'test/unit'
2
+ require 'rubygems'
3
+ require 'active_record'
4
+ require 'action_controller'
5
+ require 'has_translatable_attributes'
6
+
7
+ ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => ":memory:")
8
+
9
+ def setup_db
10
+ silence_stream(STDOUT) do
11
+ ActiveRecord::Schema.define(:version => 1) do
12
+ create_table :widgets do |t|
13
+ t.column :title_en, :string
14
+ t.column :title_es, :string
15
+ t.column :keywords, :string
16
+ end
17
+ end
18
+ end
19
+
20
+ # We need the table to exist first
21
+ Widget.class_eval do
22
+ has_translatable_attributes
23
+ end
24
+ end
25
+
26
+ def teardown_db
27
+ silence_stream(STDOUT) do
28
+ ActiveRecord::Base.connection.tables.each do |table|
29
+ ActiveRecord::Base.connection.drop_table(table)
30
+ end
31
+ end
32
+ end
33
+
34
+ class Widget < ActiveRecord::Base
35
+ end
36
+
37
+ ################################################################################
38
+
39
+ class TestHasTranslatableAttributes < ActiveSupport::TestCase
40
+ def setup
41
+ setup_db
42
+ I18n.available_locales = [:en, :es]
43
+ I18n.default_locale = :en
44
+ I18n.locale = :en
45
+ @widget = Widget.create!(:title_en => 'English', :title_es => 'Spanish', :keywords => 'one two three')
46
+ end
47
+
48
+ def teardown
49
+ teardown_db
50
+ end
51
+
52
+ def test_responds_to_find_by_title
53
+ assert Widget.respond_to? :find_by_title
54
+ end
55
+
56
+ def test_find_by_title
57
+ assert_equal @widget, Widget.find_by_title('English')
58
+ end
59
+
60
+ def test_actual_attributes
61
+ assert_equal 'English', @widget.title_en
62
+ assert_equal 'Spanish', @widget.title_es
63
+ assert_equal 'one two three', @widget.keywords
64
+ end
65
+
66
+ def test_translated_attributes
67
+ assert_equal 'English', @widget.title
68
+ assert_equal 'one two three', @widget.keywords
69
+ I18n.locale = :es
70
+ assert_equal 'Spanish', @widget.title
71
+ assert_equal 'one two three', @widget.keywords
72
+ end
73
+
74
+ def test_title_assignment
75
+ @widget.title = 'Foo'
76
+ assert_equal 'Foo', @widget.title
77
+ I18n.locale = :es
78
+ @widget.title = 'Bar'
79
+ assert_equal 'Bar', @widget.title
80
+ end
81
+
82
+ def test_nil_es_translation
83
+ @widget.title_es = nil
84
+ assert_nil @widget.title_es
85
+ assert_equal 'English', @widget.title
86
+ I18n.locale = :es
87
+ assert_equal 'English', @widget.title
88
+ end
89
+
90
+ def test_blank_es_translation
91
+ @widget.title_es = ''
92
+ assert_equal '', @widget.title_es
93
+ assert_equal 'English', @widget.title
94
+ I18n.locale = :es
95
+ assert_equal 'English', @widget.title
96
+ end
97
+
98
+ def test_nil_en_translation
99
+ @widget.title_en = nil
100
+ assert_nil @widget.title_en
101
+ assert_nil @widget.title
102
+ I18n.locale = :es
103
+ assert_equal 'Spanish', @widget.title
104
+ end
105
+
106
+ def test_blank_en_translation
107
+ @widget.title_en = ''
108
+ assert_equal '', @widget.title_en
109
+ assert_equal '', @widget.title
110
+ I18n.locale = :es
111
+ assert_equal 'Spanish', @widget.title
112
+ end
113
+ end
114
+
metadata ADDED
@@ -0,0 +1,62 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: has_translatable_attributes
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Philip Hallstrom
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-03-04 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: Provides convenience methods for setting/getting I18n specific fields.
15
+ email:
16
+ - philip@pjkh.com
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - .gitignore
22
+ - .rvmrc
23
+ - Gemfile
24
+ - LICENSE.txt
25
+ - README.md
26
+ - Rakefile
27
+ - has_translatable_attributes.gemspec
28
+ - lib/has_translatable_attributes.rb
29
+ - lib/has_translatable_attributes/version.rb
30
+ - test/test_has_translatable_attributes.rb
31
+ homepage: https://github.com/phallstrom/has_translatable_attributes
32
+ licenses: []
33
+ post_install_message:
34
+ rdoc_options: []
35
+ require_paths:
36
+ - lib
37
+ required_ruby_version: !ruby/object:Gem::Requirement
38
+ none: false
39
+ requirements:
40
+ - - ! '>='
41
+ - !ruby/object:Gem::Version
42
+ version: '0'
43
+ segments:
44
+ - 0
45
+ hash: -1493320279983591697
46
+ required_rubygems_version: !ruby/object:Gem::Requirement
47
+ none: false
48
+ requirements:
49
+ - - ! '>='
50
+ - !ruby/object:Gem::Version
51
+ version: '0'
52
+ segments:
53
+ - 0
54
+ hash: -1493320279983591697
55
+ requirements: []
56
+ rubyforge_project:
57
+ rubygems_version: 1.8.24
58
+ signing_key:
59
+ specification_version: 3
60
+ summary: Provides convenience methods for setting/getting I18n specific fields.
61
+ test_files:
62
+ - test/test_has_translatable_attributes.rb