Skip to content

Commit

Permalink
Fix rubocop offenses
Browse files Browse the repository at this point in the history
  • Loading branch information
mjonuschat committed Jul 29, 2014
1 parent 25d2200 commit 6e43868
Show file tree
Hide file tree
Showing 10 changed files with 87 additions and 85 deletions.
5 changes: 5 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
LineLength:
Max: 132

Documentation:
Enabled: false
6 changes: 3 additions & 3 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require "bundler/gem_tasks"
require "rspec/core/rake_task"
require 'bundler/gem_tasks'
require 'rspec/core/rake_task'

RSpec::Core::RakeTask.new(:spec)

task :default => :spec
task default: :spec
8 changes: 4 additions & 4 deletions lib/trasto.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
require "trasto/version"
require "trasto/translates"
require "trasto/class_methods"
require "trasto/instance_methods"
require 'trasto/version'
require 'trasto/translates'
require 'trasto/class_methods'
require 'trasto/instance_methods'
4 changes: 1 addition & 3 deletions lib/trasto/class_methods.rb
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
module Trasto
module ClassMethods

private

def translates?(column)
translatable_columns.include?(column.to_sym)
end

def locale_name(locale)
I18n.t(locale, :scope => :"i18n.languages", :default => locale.to_s.upcase)
I18n.t(locale, scope: :"i18n.languages", default: locale.to_s.upcase)
end

end
end
14 changes: 6 additions & 8 deletions lib/trasto/instance_methods.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
module Trasto
module InstanceMethods

private

def read_localized_value(column)
return nil unless column_value = send("#{column}_i18n")
return nil unless (column_value = send("#{column}_i18n"))

locales_for_reading_column(column).each do |locale|
value = column_value[locale]
Expand All @@ -15,18 +14,17 @@ def read_localized_value(column)

def write_localized_value(column, value)
translations = send("#{column}_i18n") || {}
send("#{column}_i18n=", translations.merge({I18n.locale => value}).with_indifferent_access)
send("#{column}_i18n=", translations.merge(I18n.locale => value).with_indifferent_access)
end

def locales_for_reading_column(column)
send("#{column}_i18n").keys.sort_by { |locale|
send("#{column}_i18n").keys.sort_by do |locale|
case locale.to_sym
when I18n.locale then "0"
when I18n.default_locale then "1"
when I18n.locale then '0'
when I18n.default_locale then '1'
else locale.to_s
end
}
end
end

end
end
17 changes: 9 additions & 8 deletions lib/trasto/translates.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
module Trasto
module Translates
def translates(*columns)

extend Trasto::ClassMethods
include Trasto::InstanceMethods

Expand All @@ -14,16 +13,18 @@ def translates(*columns)

self.translatable_columns |= columns.map(&:to_sym)

columns.each do |column|
columns.each { |column| define_localized_attribute(column) }
end

define_method(column) do
read_localized_value(column)
end
private

define_method("#{column}=") do |value|
write_localized_value(column, value)
end
def define_localized_attribute(column)
define_method(column) do
read_localized_value(column)
end

define_method("#{column}=") do |value|
write_localized_value(column, value)
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/trasto/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Trasto
VERSION = "0.0.1"
VERSION = '0.0.1'
end
20 changes: 10 additions & 10 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,29 +11,29 @@
# Very much based on the test setup in
# https://github.com/iain/translatable_columns/

require "active_record"
require 'active_record'

if ActiveRecord::VERSION::MAJOR == 3
require "activerecord-postgres-hstore"
require "activerecord-postgres-hstore/activerecord"
require 'activerecord-postgres-hstore'
require 'activerecord-postgres-hstore/activerecord'
end

require "app/post.rb"
require "pry"
require 'app/post.rb'
require 'pry'

ActiveRecord::Base.establish_connection :adapter => "postgresql", :database => "trasto-test"
ActiveRecord::Base.establish_connection adapter: 'postgresql', database: 'trasto-test'

I18n.enforce_available_locales ||= false

silence_stream(STDOUT) do
ActiveRecord::Schema.define(:version => 0) do
execute "CREATE EXTENSION IF NOT EXISTS hstore"
ActiveRecord::Schema.define(version: 0) do
execute 'CREATE EXTENSION IF NOT EXISTS hstore'

create_table :posts, :force => true do |t|
create_table :posts, force: true do |t|
t.hstore :title_i18n
t.hstore :body_i18n
end
end
end

I18n.load_path << "spec/app/de.yml"
I18n.load_path << 'spec/app/de.yml'
66 changes: 33 additions & 33 deletions spec/trasto_spec.rb
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
require "spec_helper"
require "trasto"
require 'spec_helper'
require 'trasto'

describe ActiveRecord::Base, ".translates" do
describe ActiveRecord::Base, '.translates' do

it "should be available" do
it 'should be available' do
Post.should respond_to :translates
end

it "should add functionality" do
it 'should add functionality' do
Post.new.should_not respond_to :title
Post.translates :title
Post.new.should respond_to :title
end

it "should be possible to run more than once" do
it 'should be possible to run more than once' do
Post.new.should_not respond_to :title, :body
Post.translates :title
Post.translates :body
Post.new.should respond_to :title, :body
end

it "inherits columns from the superclass" do
it 'inherits columns from the superclass' do
Post.translates :title
SubPost.translates :body
SubPost.new.should respond_to :title, :body
Expand All @@ -30,21 +30,21 @@

end

describe Post, ".translatable_columns" do
describe Post, '.translatable_columns' do

before do
Post.translates :title
end

it "should list the translatable columns" do
Post.translatable_columns.should == [ :title ]
it 'should list the translatable columns' do
Post.translatable_columns.should == [:title]
end

end

describe Post, "#title" do
describe Post, '#title' do

let(:post) { Post.new(:title_i18n => { :de => "Hallo", :en => "Hello", :sv => "Hej"} ) }
let(:post) { Post.new(title_i18n: { de: 'Hallo', en: 'Hello', sv: 'Hej' }) }

before do
Post.translates :title
Expand All @@ -54,40 +54,40 @@
post.title_i18n = post.title_i18n.with_indifferent_access
end

it "should give the title in the current locale" do
post.title.should == "Hello"
it 'should give the title in the current locale' do
post.title.should == 'Hello'
end

it "should fall back to the default locale if locale has entry" do
it 'should fall back to the default locale if locale has entry' do
I18n.locale = :ru
post.title.should == "Hallo"
post.title.should == 'Hallo'
end

it "should fall back to the default locale if blank" do
post.title_i18n["en"] = " "
post.title.should == "Hallo"
it 'should fall back to the default locale if blank' do
post.title_i18n['en'] = ' '
post.title.should == 'Hallo'
end

it "should fall back to any other locale if default locale is blank" do
post.title_i18n["en"] = " "
post.title_i18n["de"] = ""
post.title.should == "Hej"
it 'should fall back to any other locale if default locale is blank' do
post.title_i18n['en'] = ' '
post.title_i18n['de'] = ''
post.title.should == 'Hej'
end

it "should return nil if all are blank" do
post.title_i18n["en"] = " "
post.title_i18n["de"] = ""
post.title_i18n["sv"] = nil
it 'should return nil if all are blank' do
post.title_i18n['en'] = ' '
post.title_i18n['de'] = ''
post.title_i18n['sv'] = nil
post.title.should be_nil
end

it "should return nil on a blank record" do
it 'should return nil on a blank record' do
post.title_i18n = nil
post.title.should be_nil
end
end

describe Post, "#title=" do
describe Post, '#title=' do

before do
Post.translates :title
Expand All @@ -96,10 +96,10 @@

let(:post) { Post.new }

it "should assign in the current locale" do
post.title = "Hallo"
post.title.should == "Hallo"
post.title_i18n["de"].should == "Hallo"
it 'should assign in the current locale' do
post.title = 'Hallo'
post.title.should == 'Hallo'
post.title_i18n['de'].should == 'Hallo'
end

end
30 changes: 15 additions & 15 deletions trasto.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,23 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
require 'trasto/version'

Gem::Specification.new do |gem|
gem.name = "trasto"
gem.name = 'trasto'
gem.version = Trasto::VERSION
gem.authors = ["Morton Jonuschat"]
gem.email = ["yabawock@gmail.com"]
gem.description = %q{Translatable columns for Rails 3, directly stored in a postgres hstore in the model table.}
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}
gem.homepage = "https://github.com/yabawock/trasto"
gem.authors = ['Morton Jonuschat']
gem.email = ['yabawock@gmail.com']
gem.description = 'Translatable columns for Rails 3, directly stored in a postgres hstore in the model table.'
gem.summary = 'Use PostgreSQL hstore to keep column translations in the model table without adding lots of tables/columns.'
gem.homepage = 'https://github.com/yabawock/trasto'

gem.files = `git ls-files`.split($/)
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
gem.require_paths = ["lib"]
gem.files = `git ls-files`.split($INPUT_RECORD_SEPARATOR)
gem.executables = gem.files.grep(/^bin\//).map { |f| File.basename(f) }
gem.test_files = gem.files.grep(/^(test|spec|features)\//)
gem.require_paths = ['lib']

gem.add_dependency "pg", "~> 0.10"
gem.add_dependency 'pg', '~> 0.10'

gem.add_development_dependency "rake", "~> 0.9.2"
gem.add_development_dependency "rspec", "~> 2.11.0"
gem.add_development_dependency "pry"
gem.add_development_dependency "appraisal"
gem.add_development_dependency 'rake'
gem.add_development_dependency 'rspec', '~> 2.11.0'
gem.add_development_dependency 'pry'
gem.add_development_dependency 'appraisal'
end

0 comments on commit 6e43868

Please sign in to comment.