trasto 0.0.1 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: fdba46e380e59bcaf304a7cc645cedbd7062466d
4
+ data.tar.gz: 9b21e91148b4ca8d3ac5291382406fc61cdc7056
5
+ SHA512:
6
+ metadata.gz: 11862ebd7292a2595186f53c0f51f92e91a447d70ad4d5479d1dc4c5255e1d01c1631c0b4414fd4445f4a0c362d43920b7213dde15b33708818776ce7002bdc9
7
+ data.tar.gz: 993536f19629c433893116f6e05fe9bd5712161e4927fd27bba157e9357c4a02e4d1543ed286b1e7bc139ebe20e9f51316c10b69881ddca6ad939bbe4bb0801e
data/.gitignore CHANGED
@@ -10,7 +10,7 @@ pickle-email-*.html
10
10
  .bundle
11
11
  .config
12
12
  .yardoc
13
- Gemfile.lock
13
+ *emfile.lock
14
14
  InstalledFiles
15
15
  _yardoc
16
16
  coverage
@@ -0,0 +1,5 @@
1
+ LineLength:
2
+ Max: 132
3
+
4
+ Documentation:
5
+ Enabled: false
@@ -0,0 +1,15 @@
1
+ language: ruby
2
+ cache: bundler
3
+
4
+ rvm:
5
+ - "1.9.3"
6
+ - "2.0.0"
7
+ - "2.1.2"
8
+
9
+ gemfile:
10
+ - gemfiles/3.2.gemfile
11
+ - gemfiles/4.0.gemfile
12
+ - gemfiles/4.1.gemfile
13
+
14
+ before_script:
15
+ - psql -c 'create database "trasto-test";' -U postgres
@@ -0,0 +1,14 @@
1
+ appraise "3.2" do
2
+ gem "activerecord", "~> 3.2.19"
3
+ gem "activerecord-postgres-hstore"
4
+ end
5
+
6
+ appraise "4.0" do
7
+ gem "activerecord", "~> 4.0.0"
8
+ end
9
+
10
+ appraise "4.1" do
11
+ gem "activerecord", "~> 4.1.0"
12
+ end
13
+
14
+ # vim: set ft=ruby :
data/README.md CHANGED
@@ -45,9 +45,13 @@ You can still use your accessors for `title_i18n` and `title_i18=` in forms, val
45
45
 
46
46
  Add this to your `Gemfile` if you use Bundler 1.1+:
47
47
 
48
- gem 'traco'
48
+ gem 'trasto'
49
49
 
50
- Then run
50
+ If you're using Rails 3, add the following gem:
51
+
52
+ gem 'activerecord-postgres-hstore'
53
+
54
+ Hstore support comes out of the box in Rails 4. Then run
51
55
 
52
56
  bundle install
53
57
 
@@ -63,7 +67,7 @@ to install it.
63
67
 
64
68
  By [Morton Jonuschat](https://github.com/yabawock) under the MIT license:
65
69
 
66
- > Copyright (c) 2012 Morton Jonuschat
70
+ > Copyright (c) 2012-2014 Morton Jonuschat
67
71
  >
68
72
  > Permission is hereby granted, free of charge, to any person obtaining a copy
69
73
  > of this software and associated documentation files (the "Software"), to deal
data/Rakefile CHANGED
@@ -1,6 +1,6 @@
1
- require "bundler/gem_tasks"
2
- require "rspec/core/rake_task"
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
3
 
4
4
  RSpec::Core::RakeTask.new(:spec)
5
5
 
6
- task :default => :spec
6
+ task default: :spec
@@ -0,0 +1,8 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "activerecord", "~> 3.2.19"
6
+ gem "activerecord-postgres-hstore"
7
+
8
+ gemspec :path => "../"
@@ -0,0 +1,7 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "activerecord", "~> 4.0.0"
6
+
7
+ gemspec :path => "../"
@@ -0,0 +1,7 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "activerecord", "~> 4.1.0"
6
+
7
+ gemspec :path => "../"
@@ -1,4 +1,4 @@
1
- require "trasto/version"
2
- require "trasto/translates"
3
- require "trasto/class_methods"
4
- require "trasto/instance_methods"
1
+ require 'trasto/version'
2
+ require 'trasto/translates'
3
+ require 'trasto/class_methods'
4
+ require 'trasto/instance_methods'
@@ -1,6 +1,5 @@
1
1
  module Trasto
2
2
  module ClassMethods
3
-
4
3
  private
5
4
 
6
5
  def translates?(column)
@@ -8,8 +7,7 @@ module Trasto
8
7
  end
9
8
 
10
9
  def locale_name(locale)
11
- I18n.t(locale, :scope => :"i18n.languages", :default => locale.to_s.upcase)
10
+ I18n.t(locale, scope: :"i18n.languages", default: locale.to_s.upcase)
12
11
  end
13
-
14
12
  end
15
13
  end
@@ -1,11 +1,12 @@
1
1
  module Trasto
2
2
  module InstanceMethods
3
-
4
3
  private
5
4
 
6
5
  def read_localized_value(column)
6
+ return nil unless (column_value = send("#{column}_i18n"))
7
+
7
8
  locales_for_reading_column(column).each do |locale|
8
- value = send("#{column}_i18n")[locale]
9
+ value = column_value[locale]
9
10
  return value if value.present?
10
11
  end
11
12
  nil
@@ -13,18 +14,17 @@ module Trasto
13
14
 
14
15
  def write_localized_value(column, value)
15
16
  translations = send("#{column}_i18n") || {}
16
- send("#{column}_i18n=", translations.merge({I18n.locale => value}))
17
+ send("#{column}_i18n=", translations.merge(I18n.locale => value).with_indifferent_access)
17
18
  end
18
19
 
19
20
  def locales_for_reading_column(column)
20
- send("#{column}_i18n").keys.sort_by { |locale|
21
- case locale
22
- when I18n.locale then "0"
23
- when I18n.default_locale then "1"
21
+ send("#{column}_i18n").keys.sort_by do |locale|
22
+ case locale.to_sym
23
+ when I18n.locale then '0'
24
+ when I18n.default_locale then '1'
24
25
  else locale.to_s
25
26
  end
26
- }
27
+ end
27
28
  end
28
-
29
29
  end
30
30
  end
@@ -1,7 +1,6 @@
1
1
  module Trasto
2
2
  module Translates
3
3
  def translates(*columns)
4
-
5
4
  extend Trasto::ClassMethods
6
5
  include Trasto::InstanceMethods
7
6
 
@@ -14,16 +13,18 @@ module Trasto
14
13
 
15
14
  self.translatable_columns |= columns.map(&:to_sym)
16
15
 
17
- columns.each do |column|
16
+ columns.each { |column| define_localized_attribute(column) }
17
+ end
18
18
 
19
- define_method(column) do
20
- read_localized_value(column)
21
- end
19
+ private
22
20
 
23
- define_method("#{column}=") do |value|
24
- write_localized_value(column, value)
25
- end
21
+ def define_localized_attribute(column)
22
+ define_method(column) do
23
+ read_localized_value(column)
24
+ end
26
25
 
26
+ define_method("#{column}=") do |value|
27
+ write_localized_value(column, value)
27
28
  end
28
29
  end
29
30
  end
@@ -1,3 +1,3 @@
1
1
  module Trasto
2
- VERSION = "0.0.1"
2
+ VERSION = '0.1.0'
3
3
  end
@@ -11,22 +11,29 @@ end
11
11
  # Very much based on the test setup in
12
12
  # https://github.com/iain/translatable_columns/
13
13
 
14
- require "active_record"
15
- require "activerecord-postgres-hstore"
16
- require "activerecord-postgres-hstore/activerecord"
17
- require "app/post.rb"
14
+ require 'active_record'
18
15
 
19
- ActiveRecord::Base.establish_connection :adapter => "postgresql", :database => "test"
16
+ if ActiveRecord::VERSION::MAJOR == 3
17
+ require 'activerecord-postgres-hstore'
18
+ require 'activerecord-postgres-hstore/activerecord'
19
+ end
20
+
21
+ require 'app/post.rb'
22
+ require 'pry'
23
+
24
+ ActiveRecord::Base.establish_connection adapter: 'postgresql', database: 'trasto-test'
25
+
26
+ I18n.enforce_available_locales ||= false
20
27
 
21
28
  silence_stream(STDOUT) do
22
- ActiveRecord::Schema.define(:version => 0) do
23
- execute "CREATE EXTENSION IF NOT EXISTS hstore"
29
+ ActiveRecord::Schema.define(version: 0) do
30
+ execute 'CREATE EXTENSION IF NOT EXISTS hstore'
24
31
 
25
- create_table :posts, :force => true do |t|
32
+ create_table :posts, force: true do |t|
26
33
  t.hstore :title_i18n
27
34
  t.hstore :body_i18n
28
35
  end
29
36
  end
30
37
  end
31
38
 
32
- I18n.load_path << "spec/app/de.yml"
39
+ I18n.load_path << 'spec/app/de.yml'
@@ -1,87 +1,93 @@
1
- require "spec_helper"
2
- require "trasto"
1
+ require 'spec_helper'
2
+ require 'trasto'
3
3
 
4
- describe ActiveRecord::Base, ".translates" do
4
+ describe ActiveRecord::Base, '.translates' do
5
5
 
6
- it "should be available" do
7
- Post.should respond_to :translates
6
+ it 'should be available' do
7
+ expect(Post).to respond_to :translates
8
8
  end
9
9
 
10
- it "should add functionality" do
11
- Post.new.should_not respond_to :title
10
+ it 'should add functionality' do
11
+ expect(Post.new).not_to respond_to :title
12
12
  Post.translates :title
13
- Post.new.should respond_to :title
13
+ expect(Post.new).to respond_to :title
14
14
  end
15
15
 
16
- it "should be possible to run more than once" do
17
- Post.new.should_not respond_to :title, :body
16
+ it 'should be possible to run more than once' do
17
+ expect(Post.new).not_to respond_to :title, :body
18
18
  Post.translates :title
19
19
  Post.translates :body
20
- Post.new.should respond_to :title, :body
20
+ expect(Post.new).to respond_to :title, :body
21
21
  end
22
22
 
23
- it "inherits columns from the superclass" do
23
+ it 'inherits columns from the superclass' do
24
24
  Post.translates :title
25
25
  SubPost.translates :body
26
- SubPost.new.should respond_to :title, :body
27
- Post.new.should respond_to :title
28
- Post.new.should_not respond_to :body
26
+ expect(SubPost.new).to respond_to :title, :body
27
+ expect(Post.new).to respond_to :title
28
+ expect(Post.new).not_to respond_to :body
29
29
  end
30
30
 
31
31
  end
32
32
 
33
- describe Post, ".translatable_columns" do
33
+ describe Post, '.translatable_columns' do
34
34
 
35
35
  before do
36
36
  Post.translates :title
37
37
  end
38
38
 
39
- it "should list the translatable columns" do
40
- Post.translatable_columns.should == [ :title ]
39
+ it 'should list the translatable columns' do
40
+ expect(Post.translatable_columns).to eq([:title])
41
41
  end
42
42
 
43
43
  end
44
44
 
45
- describe Post, "#title" do
45
+ describe Post, '#title' do
46
46
 
47
- let(:post) { Post.new(:title_i18n => { :de => "Hallo", :en => "Hello", :sv => "Hej"} ) }
47
+ let(:post) { Post.new(title_i18n: { de: 'Hallo', en: 'Hello', sv: 'Hej' }) }
48
48
 
49
49
  before do
50
50
  Post.translates :title
51
51
  I18n.locale = :en
52
52
  I18n.default_locale = :de
53
+
54
+ post.title_i18n = post.title_i18n.with_indifferent_access
53
55
  end
54
56
 
55
- it "should give the title in the current locale" do
56
- post.title.should == "Hello"
57
+ it 'should give the title in the current locale' do
58
+ expect(post.title).to eq('Hello')
57
59
  end
58
60
 
59
- it "should fall back to the default locale if locale has entry" do
61
+ it 'should fall back to the default locale if locale has entry' do
60
62
  I18n.locale = :ru
61
- post.title.should == "Hallo"
63
+ expect(post.title).to eq('Hallo')
62
64
  end
63
65
 
64
- it "should fall back to the default locale if blank" do
65
- post.title_i18n[:en] = " "
66
- post.title.should == "Hallo"
66
+ it 'should fall back to the default locale if blank' do
67
+ post.title_i18n['en'] = ' '
68
+ expect(post.title).to eq('Hallo')
67
69
  end
68
70
 
69
- it "should fall back to any other locale if default locale is blank" do
70
- post.title_i18n[:en] = " "
71
- post.title_i18n[:de] = ""
72
- post.title.should == "Hej"
71
+ it 'should fall back to any other locale if default locale is blank' do
72
+ post.title_i18n['en'] = ' '
73
+ post.title_i18n['de'] = ''
74
+ expect(post.title).to eq('Hej')
73
75
  end
74
76
 
75
- it "should return nil if all are blank" do
76
- post.title_i18n[:en] = " "
77
- post.title_i18n[:de] = ""
78
- post.title_i18n[:sv] = nil
79
- post.title.should be_nil
77
+ it 'should return nil if all are blank' do
78
+ post.title_i18n['en'] = ' '
79
+ post.title_i18n['de'] = ''
80
+ post.title_i18n['sv'] = nil
81
+ expect(post.title).to be_nil
80
82
  end
81
83
 
84
+ it 'should return nil on a blank record' do
85
+ post.title_i18n = nil
86
+ expect(post.title).to be_nil
87
+ end
82
88
  end
83
89
 
84
- describe Post, "#title=" do
90
+ describe Post, '#title=' do
85
91
 
86
92
  before do
87
93
  Post.translates :title
@@ -90,10 +96,10 @@ describe Post, "#title=" do
90
96
 
91
97
  let(:post) { Post.new }
92
98
 
93
- it "should assign in the current locale" do
94
- post.title = "Hallo"
95
- post.title.should == "Hallo"
96
- post.title_i18n[:de].should == "Hallo"
99
+ it 'should assign in the current locale' do
100
+ post.title = 'Hallo'
101
+ expect(post.title).to eq('Hallo')
102
+ expect(post.title_i18n['de']).to eq('Hallo')
97
103
  end
98
104
 
99
105
  end
@@ -4,22 +4,23 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
4
  require 'trasto/version'
5
5
 
6
6
  Gem::Specification.new do |gem|
7
- gem.name = "trasto"
7
+ gem.name = 'trasto'
8
8
  gem.version = Trasto::VERSION
9
- gem.authors = ["Morton Jonuschat"]
10
- gem.email = ["yabawock@gmail.com"]
11
- gem.description = %q{Translatable columns for Rails 3, directly stored in a postgres hstore in the model table.}
12
- gem.summary = %q{Make use of PostgreSQL hstore extension to store all column translations directly in the model table without adding tons of tables or columns}
13
- gem.homepage = "https://github.com/yabawock/trasto"
9
+ gem.authors = ['Morton Jonuschat']
10
+ gem.email = ['yabawock@gmail.com']
11
+ gem.description = 'Translatable columns for Rails 3, directly stored in a postgres hstore in the model table.'
12
+ gem.summary = 'Use PostgreSQL hstore to keep column translations in the model table without adding lots of tables/columns.'
13
+ gem.homepage = 'https://github.com/yabawock/trasto'
14
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"]
15
+ gem.files = `git ls-files`.split($INPUT_RECORD_SEPARATOR)
16
+ gem.executables = gem.files.grep(/^bin\//).map { |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(/^(test|spec|features)\//)
18
+ gem.require_paths = ['lib']
19
19
 
20
- gem.add_dependency "pg", "~> 0.10"
21
- gem.add_dependency "activerecord-postgres-hstore", ">= 0.4.0"
20
+ gem.add_dependency 'pg', '~> 0.10'
22
21
 
23
- gem.add_development_dependency "rake", "~> 0.9.2"
24
- gem.add_development_dependency "rspec", "~> 2.11.0"
22
+ gem.add_development_dependency 'rake'
23
+ gem.add_development_dependency 'rspec', '~> 3.0'
24
+ gem.add_development_dependency 'pry'
25
+ gem.add_development_dependency 'appraisal'
25
26
  end
metadata CHANGED
@@ -1,80 +1,85 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: trasto
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
5
- prerelease:
4
+ version: 0.1.0
6
5
  platform: ruby
7
6
  authors:
8
7
  - Morton Jonuschat
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2012-08-26 00:00:00.000000000 Z
11
+ date: 2014-07-29 00:00:00.000000000 Z
13
12
  dependencies:
14
13
  - !ruby/object:Gem::Dependency
15
14
  name: pg
16
15
  requirement: !ruby/object:Gem::Requirement
17
- none: false
18
16
  requirements:
19
- - - ~>
17
+ - - "~>"
20
18
  - !ruby/object:Gem::Version
21
19
  version: '0.10'
22
20
  type: :runtime
23
21
  prerelease: false
24
22
  version_requirements: !ruby/object:Gem::Requirement
25
- none: false
26
23
  requirements:
27
- - - ~>
24
+ - - "~>"
28
25
  - !ruby/object:Gem::Version
29
26
  version: '0.10'
30
27
  - !ruby/object:Gem::Dependency
31
- name: activerecord-postgres-hstore
28
+ name: rake
32
29
  requirement: !ruby/object:Gem::Requirement
33
- none: false
34
30
  requirements:
35
- - - ! '>='
31
+ - - ">="
36
32
  - !ruby/object:Gem::Version
37
- version: 0.4.0
38
- type: :runtime
33
+ version: '0'
34
+ type: :development
39
35
  prerelease: false
40
36
  version_requirements: !ruby/object:Gem::Requirement
41
- none: false
42
37
  requirements:
43
- - - ! '>='
38
+ - - ">="
44
39
  - !ruby/object:Gem::Version
45
- version: 0.4.0
40
+ version: '0'
46
41
  - !ruby/object:Gem::Dependency
47
- name: rake
42
+ name: rspec
48
43
  requirement: !ruby/object:Gem::Requirement
49
- none: false
50
44
  requirements:
51
- - - ~>
45
+ - - "~>"
52
46
  - !ruby/object:Gem::Version
53
- version: 0.9.2
47
+ version: '3.0'
54
48
  type: :development
55
49
  prerelease: false
56
50
  version_requirements: !ruby/object:Gem::Requirement
57
- none: false
58
51
  requirements:
59
- - - ~>
52
+ - - "~>"
60
53
  - !ruby/object:Gem::Version
61
- version: 0.9.2
54
+ version: '3.0'
62
55
  - !ruby/object:Gem::Dependency
63
- name: rspec
56
+ name: pry
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: appraisal
64
71
  requirement: !ruby/object:Gem::Requirement
65
- none: false
66
72
  requirements:
67
- - - ~>
73
+ - - ">="
68
74
  - !ruby/object:Gem::Version
69
- version: 2.11.0
75
+ version: '0'
70
76
  type: :development
71
77
  prerelease: false
72
78
  version_requirements: !ruby/object:Gem::Requirement
73
- none: false
74
79
  requirements:
75
- - - ~>
80
+ - - ">="
76
81
  - !ruby/object:Gem::Version
77
- version: 2.11.0
82
+ version: '0'
78
83
  description: Translatable columns for Rails 3, directly stored in a postgres hstore
79
84
  in the model table.
80
85
  email:
@@ -83,11 +88,17 @@ executables: []
83
88
  extensions: []
84
89
  extra_rdoc_files: []
85
90
  files:
86
- - .gitignore
91
+ - ".gitignore"
92
+ - ".rubocop.yml"
93
+ - ".travis.yml"
94
+ - Appraisals
87
95
  - Gemfile
88
96
  - LICENSE.txt
89
97
  - README.md
90
98
  - Rakefile
99
+ - gemfiles/3.2.gemfile
100
+ - gemfiles/4.0.gemfile
101
+ - gemfiles/4.1.gemfile
91
102
  - lib/trasto.rb
92
103
  - lib/trasto/class_methods.rb
93
104
  - lib/trasto/instance_methods.rb
@@ -100,29 +111,28 @@ files:
100
111
  - trasto.gemspec
101
112
  homepage: https://github.com/yabawock/trasto
102
113
  licenses: []
114
+ metadata: {}
103
115
  post_install_message:
104
116
  rdoc_options: []
105
117
  require_paths:
106
118
  - lib
107
119
  required_ruby_version: !ruby/object:Gem::Requirement
108
- none: false
109
120
  requirements:
110
- - - ! '>='
121
+ - - ">="
111
122
  - !ruby/object:Gem::Version
112
123
  version: '0'
113
124
  required_rubygems_version: !ruby/object:Gem::Requirement
114
- none: false
115
125
  requirements:
116
- - - ! '>='
126
+ - - ">="
117
127
  - !ruby/object:Gem::Version
118
128
  version: '0'
119
129
  requirements: []
120
130
  rubyforge_project:
121
- rubygems_version: 1.8.23
131
+ rubygems_version: 2.2.2
122
132
  signing_key:
123
- specification_version: 3
124
- summary: Make use of PostgreSQL hstore extension to store all column translations
125
- directly in the model table without adding tons of tables or columns
133
+ specification_version: 4
134
+ summary: Use PostgreSQL hstore to keep column translations in the model table without
135
+ adding lots of tables/columns.
126
136
  test_files:
127
137
  - spec/app/de.yml
128
138
  - spec/app/post.rb