localized_fields 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,16 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ InstalledFiles
7
+ _yardoc
8
+ coverage
9
+ doc/
10
+ lib/bundler/man
11
+ pkg
12
+ rdoc
13
+ spec/reports
14
+ test/tmp
15
+ test/version_tmp
16
+ tmp
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/CHANGELOG.md ADDED
@@ -0,0 +1,3 @@
1
+ ## 0.0.1 (2012-01-05)
2
+
3
+ Initial release.
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in localized_fields.gemspec
4
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,68 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ localized_fields (0.0.1)
5
+ actionpack (>= 3.1.0)
6
+ mongoid (>= 2.4.0)
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ actionpack (3.1.3)
12
+ activemodel (= 3.1.3)
13
+ activesupport (= 3.1.3)
14
+ builder (~> 3.0.0)
15
+ erubis (~> 2.7.0)
16
+ i18n (~> 0.6)
17
+ rack (~> 1.3.5)
18
+ rack-cache (~> 1.1)
19
+ rack-mount (~> 0.8.2)
20
+ rack-test (~> 0.6.1)
21
+ sprockets (~> 2.0.3)
22
+ activemodel (3.1.3)
23
+ activesupport (= 3.1.3)
24
+ builder (~> 3.0.0)
25
+ i18n (~> 0.6)
26
+ activesupport (3.1.3)
27
+ multi_json (~> 1.0)
28
+ bson (1.5.2)
29
+ builder (3.0.0)
30
+ diff-lcs (1.1.3)
31
+ erubis (2.7.0)
32
+ hike (1.2.1)
33
+ i18n (0.6.0)
34
+ mongo (1.5.2)
35
+ bson (= 1.5.2)
36
+ mongoid (2.4.0)
37
+ activemodel (~> 3.1)
38
+ mongo (~> 1.3)
39
+ tzinfo (~> 0.3.22)
40
+ multi_json (1.0.4)
41
+ rack (1.3.6)
42
+ rack-cache (1.1)
43
+ rack (>= 0.4)
44
+ rack-mount (0.8.3)
45
+ rack (>= 1.0.0)
46
+ rack-test (0.6.1)
47
+ rack (>= 1.0)
48
+ rspec (2.8.0)
49
+ rspec-core (~> 2.8.0)
50
+ rspec-expectations (~> 2.8.0)
51
+ rspec-mocks (~> 2.8.0)
52
+ rspec-core (2.8.0)
53
+ rspec-expectations (2.8.0)
54
+ diff-lcs (~> 1.1.2)
55
+ rspec-mocks (2.8.0)
56
+ sprockets (2.0.3)
57
+ hike (~> 1.2)
58
+ rack (~> 1.0)
59
+ tilt (~> 1.1, != 1.3.0)
60
+ tilt (1.3.3)
61
+ tzinfo (0.3.31)
62
+
63
+ PLATFORMS
64
+ ruby
65
+
66
+ DEPENDENCIES
67
+ localized_fields!
68
+ rspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Tiago Rafael Godinho
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,80 @@
1
+ # Localized Fields
2
+
3
+ Helps you to create forms with localized fields using Mongoid.
4
+
5
+ ## Dependencies
6
+
7
+ - rails >= 3.1
8
+ - mongoid >= 2.4
9
+
10
+ ## Installation
11
+
12
+ Add this line to your application's Gemfile:
13
+
14
+ gem 'localized_fields'
15
+ gem 'mongoid', git: 'git://github.com/tiagogodinho/mongoid.git', branch: 'validates_presence'
16
+
17
+ And then execute:
18
+
19
+ $ bundle
20
+
21
+ Or install it yourself as:
22
+
23
+ $ gem install localized_fields
24
+
25
+ ## Usage
26
+
27
+ Localized Fields uses
28
+
29
+ `app/models/post.rb`
30
+
31
+ ```ruby
32
+ class Post
33
+ include Mongoid::Document
34
+
35
+ field :title, localize: true
36
+ end
37
+ ```
38
+ `app/controllers/posts_controller.rb`
39
+
40
+ ```ruby
41
+ def new
42
+ @post = Post.new
43
+ end
44
+ ```
45
+
46
+ ### Automatic
47
+
48
+ `app/view/posts/_form.html.erb`
49
+
50
+ ```erb
51
+ <%= form_for @post do |f| %>
52
+ <%= f.localized_fields do |localized_fields| %>
53
+ <%= localized_fields.label :title %>
54
+ <%= localized_fields.text_field :title %>
55
+ <% end %>
56
+ <% end %>
57
+ ```
58
+ ### Detailed
59
+
60
+ `app/view/posts/_form.html.erb`
61
+
62
+ ```erb
63
+ <%= form_for @post do |f| %>
64
+ <%= f.localized_fields :title do |localized_fields| %>
65
+ <%= localized_fields.label :en %>
66
+ <%= localized_fields.text_field :en %>
67
+
68
+ <%= localized_fields.label :pt %>
69
+ <%= localized_fields.text_field :pt %>
70
+ <% end %>
71
+ <% end %>
72
+ ```
73
+
74
+ ## Contributing
75
+
76
+ 1. Fork it
77
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
78
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
79
+ 4. Push to the branch (`git push origin my-new-feature`)
80
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
3
+ require 'rspec/core/rake_task'
4
+
5
+ RSpec::Core::RakeTask.new(:spec)
6
+
7
+ task default: 'spec'
@@ -0,0 +1,70 @@
1
+ require "localized_fields/version"
2
+
3
+ require 'mongoid'
4
+ require 'action_view'
5
+
6
+ module LocalizedFields
7
+ module FormHelpers
8
+ def localized_fields(field = nil, options = {}, &block)
9
+ if field
10
+ field_name = "#{field}_translations"
11
+ object = @object.send(field_name)
12
+ name = "#{object_name}[#{field_name}]"
13
+
14
+ @template.fields_for(name, object, options.merge(:builder => LocalizedFields::FormBuilder), &block).html_safe
15
+ else
16
+ output = ''
17
+
18
+ I18n.available_locales.each do |language|
19
+ output << @template.fields_for(object_name, @object, options.merge(:builder => LocalizedFields::FormBuilder, :language => language), &block)
20
+ end
21
+
22
+ output.html_safe
23
+ end
24
+ end
25
+ end
26
+
27
+ class FormBuilder < ActionView::Helpers::FormBuilder
28
+ def language
29
+ @options[:language] if @options[:language]
30
+ end
31
+
32
+ def label(attribute, options = {})
33
+ if @options.has_key?(:language)
34
+ language = @options[:language]
35
+
36
+ super(attribute, for: "#{object_name}_#{attribute}_translations_#{language}").html_safe
37
+ else
38
+ field_name = @object_name.match(/.*\[(.*)_translations\]/)[1].capitalize
39
+ super(attribute, field_name, options).html_safe
40
+ end
41
+ end
42
+
43
+ def text_field(attribute, options = {})
44
+ if @options.has_key?(:language)
45
+ language = @options[:language]
46
+
47
+ translations = @object.send("#{attribute}_translations") || {}
48
+
49
+ value = translations.has_key?(language.to_s) ? translations[language.to_s] : nil
50
+
51
+ super(attribute, value: value, id: "#{object_name}_#{attribute}_translations_#{language}", name: "#{object_name}[#{attribute}_translations][#{language}]").html_safe
52
+ else
53
+ value = @object ? @object[attribute.to_s] : nil
54
+ super(attribute, value: value).html_safe
55
+ end
56
+ end
57
+
58
+ def text_area(attribute, options = {})
59
+ if @options.has_key?(:language)
60
+ language = @options[:language]
61
+
62
+ super(attribute, id: "#{object_name}_#{attribute}_translations_#{language}", name: "#{object_name}[#{attribute}_translations][#{language}]").html_safe
63
+ else
64
+ super(attribute, options).html_safe
65
+ end
66
+ end
67
+ end
68
+ end
69
+
70
+ ActionView::Helpers::FormBuilder.send :include, LocalizedFields::FormHelpers
@@ -0,0 +1,3 @@
1
+ module LocalizedFields
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,22 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/localized_fields/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Tiago Rafael Godinho"]
6
+ gem.email = ["tiagogodinho3@gmail.com"]
7
+ gem.description = %q{Helps you to create forms with localized fields using Mongoid.}
8
+ gem.summary = %q{Localized Fields provides form helpers to create forms with localized fields using Mongoid.}
9
+ gem.homepage = ""
10
+
11
+ gem.add_dependency 'mongoid', '>= 2.4.0'
12
+ gem.add_dependency 'actionpack', '>= 3.1.0'
13
+
14
+ gem.add_development_dependency 'rspec'
15
+
16
+ gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
17
+ gem.files = `git ls-files`.split("\n")
18
+ gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
19
+ gem.name = "localized_fields"
20
+ gem.require_paths = ["lib"]
21
+ gem.version = LocalizedFields::VERSION
22
+ end
@@ -0,0 +1,124 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'LocalizedFields' do
4
+ before do
5
+ @post = Post.new
6
+ @template = ActionView::Base.new
7
+ @template.output_buffer = ''
8
+ @builder = ActionView::Helpers::FormBuilder.new(:post, @post, @template, {}, proc {})
9
+
10
+ I18n.available_locales = [:en, :pt]
11
+ end
12
+
13
+ context 'form helpers with html tags' do
14
+ it 'should return html tags with label and text_field' do
15
+ output = @builder.localized_fields do |localized_field|
16
+ '<dl class="field">'.html_safe +
17
+ '<dt>'.html_safe + localized_field.label(:title).html_safe + '</dt>'.html_safe +
18
+ '<dd>'.html_safe + localized_field.text_field(:title).html_safe + '</dd>'.html_safe +
19
+ '</dl>'.html_safe
20
+ end.html_safe
21
+
22
+ expected = '<dl class="field">' +
23
+ '<dt><label for="post_title_translations_en">Title</label></dt>' +
24
+ '<dd><input id="post_title_translations_en" name="post[title_translations][en]" size="30" type="text" /></dd>' +
25
+ '</dl>' +
26
+ '<dl class="field">' +
27
+ '<dt><label for="post_title_translations_pt">Title</label></dt>' +
28
+ '<dd><input id="post_title_translations_pt" name="post[title_translations][pt]" size="30" type="text" /></dd>' +
29
+ '</dl>'
30
+
31
+ output.should eq(expected)
32
+ end
33
+
34
+ it 'should return html tags with text_field' do
35
+ output = @builder.localized_fields do |localized_field|
36
+ '<div>'.html_safe + localized_field.text_field(:title).html_safe + '</div>'.html_safe
37
+ end
38
+
39
+ expected = '<div><input id="post_title_translations_en" name="post[title_translations][en]" size="30" type="text" /></div>' +
40
+ '<div><input id="post_title_translations_pt" name="post[title_translations][pt]" size="30" type="text" /></div>'
41
+
42
+ output.should eq(expected)
43
+ end
44
+
45
+ it 'should return html tags with text_field for :en' do
46
+ output = @builder.localized_fields(:title) do |localized_field|
47
+ '<div>'.html_safe + localized_field.text_field(:en).html_safe + '</div>'.html_safe
48
+ end
49
+
50
+ expected = '<div><input id="post_title_translations_en" name="post[title_translations][en]" size="30" type="text" /></div>'
51
+
52
+ output.should eq(expected)
53
+ end
54
+ end
55
+
56
+ describe 'label' do
57
+ it 'should return a label tag for en' do
58
+ output = @builder.localized_fields(:title) do |localized_field|
59
+ localized_field.label :en
60
+ end
61
+
62
+ expected = '<label for="post_title_translations_en">Title</label>'
63
+
64
+ output.should eq(expected)
65
+ end
66
+
67
+ it 'should return a label tag for all languages' do
68
+ output = @builder.localized_fields do |localized_field|
69
+ localized_field.label :title
70
+ end
71
+
72
+ expected = '<label for="post_title_translations_en">Title</label>' +
73
+ '<label for="post_title_translations_pt">Title</label>'
74
+
75
+ output.should eq(expected)
76
+ end
77
+ end
78
+
79
+ describe 'text_field' do
80
+ it 'should return a text_field tag for en' do
81
+ output = @builder.localized_fields(:title) do |localized_field|
82
+ localized_field.text_field :en
83
+ end
84
+
85
+ expected = '<input id="post_title_translations_en" name="post[title_translations][en]" size="30" type="text" />'
86
+
87
+ output.should eq(expected)
88
+ end
89
+
90
+ it 'should return a text_field tag for all languages' do
91
+ output = @builder.localized_fields do |localized_field|
92
+ localized_field.text_field :title
93
+ end
94
+
95
+ expected = '<input id="post_title_translations_en" name="post[title_translations][en]" size="30" type="text" />' +
96
+ '<input id="post_title_translations_pt" name="post[title_translations][pt]" size="30" type="text" />'
97
+
98
+ output.should eq(expected)
99
+ end
100
+ end
101
+
102
+ describe 'text_area' do
103
+ it 'should return a text_area tag for en' do
104
+ output = @builder.localized_fields(:title) do |localized_field|
105
+ localized_field.text_area :en
106
+ end
107
+
108
+ expected = '<textarea cols="40" id="post_title_translations_en" name="post[title_translations][en]" rows="20"></textarea>'
109
+
110
+ output.should eq(expected)
111
+ end
112
+
113
+ it 'should return a text_area tag for all languages' do
114
+ output = @builder.localized_fields do |localized_field|
115
+ localized_field.text_area :title
116
+ end
117
+
118
+ expected = '<textarea cols="40" id="post_title_translations_en" name="post[title_translations][en]" rows="20"></textarea>' +
119
+ '<textarea cols="40" id="post_title_translations_pt" name="post[title_translations][pt]" rows="20"></textarea>'
120
+
121
+ output.should eq(expected)
122
+ end
123
+ end
124
+ end
@@ -0,0 +1,4 @@
1
+ require 'localized_fields'
2
+
3
+ Dir[File.expand_path(File.join(File.dirname(__FILE__), 'support', '**', '*.rb'))].each { |f| require f }
4
+
@@ -0,0 +1,5 @@
1
+ class Post
2
+ include Mongoid::Document
3
+
4
+ field :title, localize: true
5
+ end
metadata ADDED
@@ -0,0 +1,96 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: localized_fields
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Tiago Rafael Godinho
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-01-05 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: mongoid
16
+ requirement: &2152223900 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: 2.4.0
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *2152223900
25
+ - !ruby/object:Gem::Dependency
26
+ name: actionpack
27
+ requirement: &2152222940 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: 3.1.0
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: *2152222940
36
+ - !ruby/object:Gem::Dependency
37
+ name: rspec
38
+ requirement: &2152222260 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ type: :development
45
+ prerelease: false
46
+ version_requirements: *2152222260
47
+ description: Helps you to create forms with localized fields using Mongoid.
48
+ email:
49
+ - tiagogodinho3@gmail.com
50
+ executables: []
51
+ extensions: []
52
+ extra_rdoc_files: []
53
+ files:
54
+ - .gitignore
55
+ - .rspec
56
+ - CHANGELOG.md
57
+ - Gemfile
58
+ - Gemfile.lock
59
+ - LICENSE
60
+ - README.md
61
+ - Rakefile
62
+ - lib/localized_fields.rb
63
+ - lib/localized_fields/version.rb
64
+ - localized_fields.gemspec
65
+ - spec/localized_fields_spec.rb
66
+ - spec/spec_helper.rb
67
+ - spec/support/post.rb
68
+ homepage: ''
69
+ licenses: []
70
+ post_install_message:
71
+ rdoc_options: []
72
+ require_paths:
73
+ - lib
74
+ required_ruby_version: !ruby/object:Gem::Requirement
75
+ none: false
76
+ requirements:
77
+ - - ! '>='
78
+ - !ruby/object:Gem::Version
79
+ version: '0'
80
+ required_rubygems_version: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ requirements: []
87
+ rubyforge_project:
88
+ rubygems_version: 1.8.13
89
+ signing_key:
90
+ specification_version: 3
91
+ summary: Localized Fields provides form helpers to create forms with localized fields
92
+ using Mongoid.
93
+ test_files:
94
+ - spec/localized_fields_spec.rb
95
+ - spec/spec_helper.rb
96
+ - spec/support/post.rb