babylonia-rails 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 1738c0ddff03c286598096590afe14d374dd3131
4
+ data.tar.gz: e060718b02decf056a932effd4df2ff08af92a16
5
+ SHA512:
6
+ metadata.gz: 81db6ab131ac20297f73280f6c1147768d9a99c0a7368bd877142158e42c1c86d884fc02eee657b063f71cdc5d72263674d156d475ef68cf687759bfe8c9966f
7
+ data.tar.gz: 1f87e602d62a067f580890d4dc94a1aaa2ff9229691def7b7ff5969ed8d8efd1a73c83d1501073edaa425b3fab7bab00901d0490b1ac09dca5347102895b3984
data/.gitignore ADDED
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ coverage
6
+ InstalledFiles
7
+ lib/bundler/man
8
+ pkg
9
+ rdoc
10
+ spec/reports
11
+ test/tmp
12
+ test/version_tmp
13
+ tmp
14
+
15
+ # YARD artifacts
16
+ .yardoc
17
+ _yardoc
18
+ doc/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --order random
data/.travis.yml ADDED
@@ -0,0 +1,11 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.2
4
+ - 1.9.3
5
+ - 2.0.0
6
+ - rbx-19mode
7
+ - rbx-20mode
8
+ - jruby-19mode
9
+ - jruby-20mode
10
+
11
+ script: bundle exec rspec
data/Gemfile ADDED
@@ -0,0 +1,13 @@
1
+ source "http://rubygems.org"
2
+
3
+ gemspec
4
+
5
+ group :development do
6
+ gem "yard", ">= 0.7.4"
7
+ gem "bundler", ">= 1.0.0"
8
+ gem "mysql2"
9
+ end
10
+
11
+ group :test do
12
+ gem "rspec"
13
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,57 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ babylonia-rails (0.0.1)
5
+ activerecord (>= 3.2.0)
6
+ babylonia (>= 0.0.2)
7
+
8
+ GEM
9
+ remote: http://rubygems.org/
10
+ specs:
11
+ activemodel (4.0.1)
12
+ activesupport (= 4.0.1)
13
+ builder (~> 3.1.0)
14
+ activerecord (4.0.1)
15
+ activemodel (= 4.0.1)
16
+ activerecord-deprecated_finders (~> 1.0.2)
17
+ activesupport (= 4.0.1)
18
+ arel (~> 4.0.0)
19
+ activerecord-deprecated_finders (1.0.3)
20
+ activesupport (4.0.1)
21
+ i18n (~> 0.6, >= 0.6.4)
22
+ minitest (~> 4.2)
23
+ multi_json (~> 1.3)
24
+ thread_safe (~> 0.1)
25
+ tzinfo (~> 0.3.37)
26
+ arel (4.0.1)
27
+ atomic (1.1.14)
28
+ babylonia (0.0.2)
29
+ i18n (>= 0.5.0)
30
+ builder (3.1.4)
31
+ diff-lcs (1.2.5)
32
+ i18n (0.6.5)
33
+ minitest (4.7.5)
34
+ multi_json (1.8.2)
35
+ mysql2 (0.3.14)
36
+ rspec (2.14.1)
37
+ rspec-core (~> 2.14.0)
38
+ rspec-expectations (~> 2.14.0)
39
+ rspec-mocks (~> 2.14.0)
40
+ rspec-core (2.14.7)
41
+ rspec-expectations (2.14.4)
42
+ diff-lcs (>= 1.1.3, < 2.0)
43
+ rspec-mocks (2.14.4)
44
+ thread_safe (0.1.3)
45
+ atomic
46
+ tzinfo (0.3.38)
47
+ yard (0.8.7.3)
48
+
49
+ PLATFORMS
50
+ ruby
51
+
52
+ DEPENDENCIES
53
+ babylonia-rails!
54
+ bundler (>= 1.0.0)
55
+ mysql2
56
+ rspec
57
+ yard (>= 0.7.4)
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2013 Beat
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of
6
+ this software and associated documentation files (the "Software"), to deal in
7
+ the Software without restriction, including without limitation the rights to
8
+ use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9
+ the Software, and to permit persons to whom the Software is furnished to do so,
10
+ subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17
+ FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18
+ COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19
+ IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20
+ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,50 @@
1
+ = babylonia-rails {<img src="https://secure.travis-ci.org/beatrichartz/babylonia-rails.png?branch=master" />}[http://travis-ci.org/beatrichartz/babylonia-rails] {<img src="https://gemnasium.com/beatrichartz/babylonia-rails.png" alt="Dependency Status" />}[https://gemnasium.com/beatrichartz/babylonia-rails] {<img src="https://codeclimate.com/github/beatrichartz/babylonia-rails.png" />}[https://codeclimate.com/github/beatrichartz/babylonia-rails]
2
+ Let there be languages for your rails users!
3
+
4
+ == Installation
5
+ In your gemfile
6
+ gem "babylonia-rails"
7
+ Or via the gem command
8
+ gem install babylonia-rails
9
+
10
+ == How to ride the rails to babylonia
11
+ Feels like babylonia, validates like rails
12
+
13
+ class BabylonianFields < ActiveRecord::Base
14
+ build_babylonian_tower_on :grass
15
+
16
+ validate :grass, languages: {present: [:en, :de, :it], length: 2..1000}
17
+ end
18
+
19
+ I18n.locale #=> :en, so it will set en
20
+
21
+ babylonian_field = BabylonianFields.new grass: 'Hello'
22
+
23
+ babylonian_field.valid? #=> false
24
+ babylonian_field.errors.full_messages #=> "Grass should also be translated in DE and IT"
25
+ babylonian_field.grass = {de: 'DEUTSCH', it: 'I'}
26
+ babylonian_field.valid? #=> false
27
+ babylonian_field.errors.full_messages #=> "Grass should be between 2 an 1000 characters in IT"
28
+ babylonian_field.grass = {it: 'IT'}
29
+ babylonian_field.valid? #=> true
30
+
31
+ == For Basic Usage
32
+ Visit {babylonia}[http://github.com/beatrichartz/babylonia]
33
+
34
+ == Contributing to babylonia-rails
35
+
36
+ Please note that only open source APIs can be accepted as contributions to this gem. Private / Premium APIs have to be written as your own extension and will not be added to the gem code.
37
+
38
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
39
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
40
+ * Fork the project.
41
+ * Start a feature/bugfix branch.
42
+ * Commit and push until you are happy with your contribution.
43
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
44
+ * Make sure to add documentation for it. This is important so everyone else can see what your code can do.
45
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
46
+
47
+ == Copyright
48
+
49
+ Copyright (c) 2013 Beat Richartz. See LICENSE.txt for
50
+ further details.
@@ -0,0 +1,23 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $LOAD_PATH << File.join(File.dirname(__FILE__), 'lib')
3
+ require 'babylonia/rails/version'
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "babylonia-rails"
7
+ s.version = Babylonia::Rails::VERSION
8
+ s.authors = ["Beat Richartz"]
9
+ s.description = "Babylonia for rails lets your rails users translate their content into their languages without additional tables or columns in your tables"
10
+ s.email = "attr_accessor@gmail.com"
11
+ s.licenses = ["MIT"]
12
+ s.require_paths = ["lib"]
13
+ s.summary = "Let there be languages for your rails users!"
14
+
15
+ s.files = `git ls-files`.split("\n")
16
+ s.test_files = `git ls-files -- spec/*`.split("\n")
17
+ s.require_paths = ["lib"]
18
+
19
+ s.add_dependency "babylonia", ">= 0.0.2"
20
+ s.add_dependency "activerecord", ">= 3.2.0"
21
+ s.add_development_dependency "bundler", ">= 1.0.0"
22
+ end
23
+
@@ -0,0 +1,20 @@
1
+ module Babylonia
2
+ module ClassMethods
3
+ def build_babylonian_tower_on_with_activerecord_patch *fields
4
+ fields.each do |field|
5
+ define_method field do
6
+ super()
7
+ end
8
+
9
+ define_method :"#{field}=" do |data|
10
+ super(data)
11
+ end
12
+ end
13
+
14
+ build_babylonian_tower_on_without_activerecord_patch *fields
15
+ end
16
+ alias_method :build_babylonian_tower_on_without_activerecord_patch, :build_babylonian_tower_on
17
+ alias_method :build_babylonian_tower_on, :build_babylonian_tower_on_with_activerecord_patch
18
+
19
+ end
20
+ end
@@ -0,0 +1,4 @@
1
+ require 'babylonia'
2
+ require 'active_record'
3
+
4
+ ActiveRecord::Base.send(:extend, Babylonia::ClassMethods)
@@ -0,0 +1,36 @@
1
+ class LanguagesValidator < ActiveModel::EachValidator
2
+ def validate_each(record, attribute, value)
3
+ validate_languages_present(record, attribute, options[:present].sort) if options[:present]
4
+ validate_length_in_range(record, attribute, options[:length]) if options[:length].is_a?(Range)
5
+ end
6
+
7
+ private
8
+
9
+ def validate_languages_present(record, attribute, languages)
10
+ is = record_attribute_languages(record, attribute)
11
+ unless is == languages
12
+ record.errors[attribute] << (options[:message] || "should also be translated in #{(languages - is).map(&:upcase).to_sentence}")
13
+ end
14
+ end
15
+
16
+ def validate_length_in_range(record, attribute, range)
17
+ all = record_attribute_languages(record, attribute)
18
+ in_range = keys_not_matching(record, attribute){|k,v| !range.include?(v.size) }
19
+ unless all.empty? || all == in_range
20
+ record.errors[attribute] << (options[:message] || "should be between #{range.first} and #{range.last} characters for #{(all - in_range).map(&:upcase).to_sentence}")
21
+ end
22
+ end
23
+
24
+ def keys_not_matching record, attribute, &block
25
+ not_matching = record_attribute_hash(record, attribute).dup.delete_if &block
26
+ not_matching.keys.sort
27
+ end
28
+
29
+ def record_attribute_hash record, attribute
30
+ record.send(:"#{attribute}_hash")
31
+ end
32
+
33
+ def record_attribute_languages record, attribute
34
+ record.send(:"#{attribute}_languages").sort
35
+ end
36
+ end
@@ -0,0 +1,5 @@
1
+ module Babylonia
2
+ module Rails
3
+ VERSION = '0.0.1'
4
+ end
5
+ end
@@ -0,0 +1,4 @@
1
+ require 'babylonia'
2
+ require "babylonia/rails/class_methods"
3
+ require "babylonia/rails/integration"
4
+ require "babylonia/rails/validator"
@@ -0,0 +1,16 @@
1
+ require 'spec_helper'
2
+
3
+ describe "Integration" do
4
+
5
+ class BabylonianField < ActiveRecord::Base
6
+
7
+ end
8
+
9
+ describe "Babylonia" do
10
+ it "should already have extended active record" do
11
+ BabylonianField.should be_respond_to(:build_babylonian_tower_on)
12
+ end
13
+ end
14
+
15
+
16
+ end
@@ -0,0 +1,21 @@
1
+ require 'spec_helper'
2
+
3
+ describe LanguagesValidator do
4
+
5
+ class BabylonianField < ActiveRecord::Base
6
+
7
+ build_babylonian_tower_on :marshes
8
+ validates :marshes, languages: { present: %i(de en it), length: 5..11 }
9
+
10
+ end
11
+
12
+ describe "persistence" do
13
+ subject { BabylonianField.new(marshes: {en: 'Hello', de: 'Hello', it: 'Hello'})}
14
+ it "should be possible to store the string value" do
15
+ subject.save!
16
+ subject.reload
17
+ subject.marshes_hash.should == {en: 'Hello', de: 'Hello', it: 'Hello'}
18
+ end
19
+ end
20
+
21
+ end
@@ -0,0 +1,65 @@
1
+ require 'spec_helper'
2
+
3
+ describe LanguagesValidator do
4
+
5
+ class BabylonianField < ActiveRecord::Base
6
+
7
+ build_babylonian_tower_on :marshes
8
+ validates :marshes, languages: { present: %i(de en it), length: 5..11 }
9
+
10
+ end
11
+
12
+ describe "validations" do
13
+ subject { BabylonianField.new(marshes: "Hello") }
14
+ before(:each) do
15
+ I18n.stub locale: :en
16
+ end
17
+ context "presence validations" do
18
+ context "when invalid" do
19
+ it "should indicate which languages are not translated" do
20
+ subject.should_not be_valid
21
+ subject.errors[:marshes].first.should == "should also be translated in DE and IT"
22
+ end
23
+ it "should indicate remaining incomplete languages if some are updated" do
24
+ subject.marshes = {it: 'SOMETHING'}
25
+ subject.should_not be_valid
26
+ subject.errors[:marshes].first.should == "should also be translated in DE"
27
+ end
28
+ it "should work when attributes are deleted" do
29
+ subject.marshes = ''
30
+ subject.should_not be_valid
31
+ subject.errors[:marshes].first.should == "should also be translated in DE, EN, and IT"
32
+ end
33
+ end
34
+ context "when valid" do
35
+ before(:each) do
36
+ subject.marshes = {en: 'Hello', de: 'Hello', it: 'Hello'}
37
+ end
38
+ it "should be valid" do
39
+ subject.should be_valid
40
+ end
41
+ end
42
+ end
43
+ context "length validations" do
44
+ before(:each) do
45
+ subject.marshes = {en: 'H', de: 'Hello', it: 'Hlo'}
46
+ end
47
+ context "when invalid" do
48
+ it "should indicate that the languages that are not in length" do
49
+ subject.should_not be_valid
50
+ subject.errors[:marshes].first.should == "should be between 5 and 11 characters for EN and IT"
51
+ end
52
+ end
53
+ context "when valid" do
54
+ before(:each) do
55
+ subject.marshes = {en: 'HelloHelloH', de: 'Hello', it: 'Hello'}
56
+ end
57
+ it "should be valid" do
58
+ subject.should be_valid
59
+ end
60
+ end
61
+ end
62
+ end
63
+
64
+
65
+ end
@@ -0,0 +1,33 @@
1
+ # -*- encoding : utf-8 -*-
2
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
4
+
5
+ require 'rubygems'
6
+ require 'bundler'
7
+ begin
8
+ Bundler.setup(:default, :test)
9
+ rescue Bundler::BundlerError => e
10
+ $stderr.puts e.message
11
+ $stderr.puts "Run `bundle install` to install missing gems"
12
+ exit e.status_code
13
+ end
14
+
15
+ require 'babylonia/rails'
16
+
17
+ module HelperMethods
18
+
19
+ end
20
+
21
+ RSpec.configure do |configuration|
22
+ include HelperMethods
23
+ configuration.before(:suite) do
24
+ ActiveRecord::Base.configurations = YAML.load_file(File.expand_path('./support/database.yml', File.dirname(__FILE__)))
25
+ ActiveRecord::Base.establish_connection(:test)
26
+ require File.expand_path('./support/tables.rb', File.dirname(__FILE__))
27
+
28
+ CreateTestTables.new.up
29
+ end
30
+ configuration.after(:suite) do
31
+ CreateTestTables.new.down
32
+ end
33
+ end
@@ -0,0 +1,15 @@
1
+ global: &global
2
+ reconnect: true
3
+ adapter: mysql2
4
+ encoding: utf8
5
+
6
+ base: &base
7
+ username: developer
8
+ password:
9
+ host: 127.0.0.1
10
+ application_timezone: :local
11
+
12
+ test:
13
+ <<: *global
14
+ <<: *base
15
+ database: babylonia_test
@@ -0,0 +1,11 @@
1
+ class CreateTestTables < ActiveRecord::Migration
2
+ def up
3
+ create_table :babylonian_fields do |t|
4
+ t.text :marshes
5
+ t.timestamps
6
+ end
7
+ end
8
+ def down
9
+ drop_table :babylonian_fields
10
+ end
11
+ end
metadata ADDED
@@ -0,0 +1,112 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: babylonia-rails
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Beat Richartz
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-11-19 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: babylonia
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: 0.0.2
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: 0.0.2
27
+ - !ruby/object:Gem::Dependency
28
+ name: activerecord
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: 3.2.0
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: 3.2.0
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: 1.0.0
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: 1.0.0
55
+ description: Babylonia for rails lets your rails users translate their content into
56
+ their languages without additional tables or columns in your tables
57
+ email: attr_accessor@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - .gitignore
63
+ - .rspec
64
+ - .travis.yml
65
+ - Gemfile
66
+ - Gemfile.lock
67
+ - LICENSE
68
+ - README.rdoc
69
+ - babylonia-rails.gemspec
70
+ - lib/babylonia/rails.rb
71
+ - lib/babylonia/rails/class_methods.rb
72
+ - lib/babylonia/rails/integration.rb
73
+ - lib/babylonia/rails/validator.rb
74
+ - lib/babylonia/rails/version.rb
75
+ - spec/babylonia-rails/integration_spec.rb
76
+ - spec/babylonia-rails/persistence_spec.rb
77
+ - spec/babylonia-rails/validator_spec.rb
78
+ - spec/spec_helper.rb
79
+ - spec/support/database.yml
80
+ - spec/support/tables.rb
81
+ homepage:
82
+ licenses:
83
+ - MIT
84
+ metadata: {}
85
+ post_install_message:
86
+ rdoc_options: []
87
+ require_paths:
88
+ - lib
89
+ required_ruby_version: !ruby/object:Gem::Requirement
90
+ requirements:
91
+ - - '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ required_rubygems_version: !ruby/object:Gem::Requirement
95
+ requirements:
96
+ - - '>='
97
+ - !ruby/object:Gem::Version
98
+ version: '0'
99
+ requirements: []
100
+ rubyforge_project:
101
+ rubygems_version: 2.0.7
102
+ signing_key:
103
+ specification_version: 4
104
+ summary: Let there be languages for your rails users!
105
+ test_files:
106
+ - spec/babylonia-rails/integration_spec.rb
107
+ - spec/babylonia-rails/persistence_spec.rb
108
+ - spec/babylonia-rails/validator_spec.rb
109
+ - spec/spec_helper.rb
110
+ - spec/support/database.yml
111
+ - spec/support/tables.rb
112
+ has_rdoc: