localized_fields 0.0.1 → 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.travis.yml +7 -0
- data/CHANGELOG.md +6 -0
- data/Gemfile +2 -0
- data/Gemfile.lock +3 -1
- data/README.md +14 -7
- data/Rakefile +1 -1
- data/lib/localized_fields/form_builder.rb +49 -0
- data/lib/localized_fields/helpers/form_helper.rb +23 -0
- data/lib/localized_fields/helpers.rb +7 -0
- data/lib/localized_fields/version.rb +1 -1
- data/lib/localized_fields.rb +4 -61
- data/spec/localized_fields_spec.rb +31 -0
- data/spec/support/post.rb +1 -1
- metadata +12 -8
    
        data/.travis.yml
    ADDED
    
    
    
        data/CHANGELOG.md
    CHANGED
    
    
    
        data/Gemfile
    CHANGED
    
    
    
        data/Gemfile.lock
    CHANGED
    
    | @@ -1,7 +1,7 @@ | |
| 1 1 | 
             
            PATH
         | 
| 2 2 | 
             
              remote: .
         | 
| 3 3 | 
             
              specs:
         | 
| 4 | 
            -
                localized_fields (0.0. | 
| 4 | 
            +
                localized_fields (0.0.2)
         | 
| 5 5 | 
             
                  actionpack (>= 3.1.0)
         | 
| 6 6 | 
             
                  mongoid (>= 2.4.0)
         | 
| 7 7 |  | 
| @@ -45,6 +45,7 @@ GEM | |
| 45 45 | 
             
                  rack (>= 1.0.0)
         | 
| 46 46 | 
             
                rack-test (0.6.1)
         | 
| 47 47 | 
             
                  rack (>= 1.0)
         | 
| 48 | 
            +
                rake (0.9.2.2)
         | 
| 48 49 | 
             
                rspec (2.8.0)
         | 
| 49 50 | 
             
                  rspec-core (~> 2.8.0)
         | 
| 50 51 | 
             
                  rspec-expectations (~> 2.8.0)
         | 
| @@ -65,4 +66,5 @@ PLATFORMS | |
| 65 66 |  | 
| 66 67 | 
             
            DEPENDENCIES
         | 
| 67 68 | 
             
              localized_fields!
         | 
| 69 | 
            +
              rake
         | 
| 68 70 | 
             
              rspec
         | 
    
        data/README.md
    CHANGED
    
    | @@ -1,18 +1,12 @@ | |
| 1 | 
            -
            # Localized Fields
         | 
| 1 | 
            +
            # Localized Fields [](http://travis-ci.org/tiagogodinho/localized_fields)
         | 
| 2 2 |  | 
| 3 3 | 
             
            Helps you to create forms with localized fields using Mongoid.
         | 
| 4 4 |  | 
| 5 | 
            -
            ## Dependencies
         | 
| 6 | 
            -
             | 
| 7 | 
            -
            - rails >= 3.1
         | 
| 8 | 
            -
            - mongoid >= 2.4
         | 
| 9 | 
            -
             | 
| 10 5 | 
             
            ## Installation
         | 
| 11 6 |  | 
| 12 7 | 
             
            Add this line to your application's Gemfile:
         | 
| 13 8 |  | 
| 14 9 | 
             
                gem 'localized_fields'
         | 
| 15 | 
            -
                gem 'mongoid', git: 'git://github.com/tiagogodinho/mongoid.git', branch: 'validates_presence'
         | 
| 16 10 |  | 
| 17 11 | 
             
            And then execute:
         | 
| 18 12 |  | 
| @@ -71,6 +65,15 @@ end | |
| 71 65 | 
             
            <% end %>
         | 
| 72 66 | 
             
            ```
         | 
| 73 67 |  | 
| 68 | 
            +
            ## Dependencies
         | 
| 69 | 
            +
             | 
| 70 | 
            +
            - rails >= 3.1
         | 
| 71 | 
            +
            - mongoid >= 2.4
         | 
| 72 | 
            +
             | 
| 73 | 
            +
            ## Compatibility
         | 
| 74 | 
            +
             | 
| 75 | 
            +
            Localized Fiedls is tested against Ruby 1.8.7, 1.9.2, 1.9.3, REE and Rubinius.
         | 
| 76 | 
            +
             | 
| 74 77 | 
             
            ## Contributing
         | 
| 75 78 |  | 
| 76 79 | 
             
            1. Fork it
         | 
| @@ -78,3 +81,7 @@ end | |
| 78 81 | 
             
            3. Commit your changes (`git commit -am 'Added some feature'`)
         | 
| 79 82 | 
             
            4. Push to the branch (`git push origin my-new-feature`)
         | 
| 80 83 | 
             
            5. Create new Pull Request
         | 
| 84 | 
            +
             | 
| 85 | 
            +
            ## License
         | 
| 86 | 
            +
             | 
| 87 | 
            +
            MIT License. Copyright 2012 Tiago Rafael Godinho
         | 
    
        data/Rakefile
    CHANGED
    
    
| @@ -0,0 +1,49 @@ | |
| 1 | 
            +
            module LocalizedFields
         | 
| 2 | 
            +
              class FormBuilder < ActionView::Helpers::FormBuilder
         | 
| 3 | 
            +
                def language
         | 
| 4 | 
            +
                  @options[:language] if @options[:language]
         | 
| 5 | 
            +
                end
         | 
| 6 | 
            +
                
         | 
| 7 | 
            +
                def label(attribute, options = {})
         | 
| 8 | 
            +
                  if @options.has_key?(:language)
         | 
| 9 | 
            +
                    language = @options[:language]
         | 
| 10 | 
            +
                    
         | 
| 11 | 
            +
                    super(attribute, :for => "#{object_name}_#{attribute}_translations_#{language}").html_safe
         | 
| 12 | 
            +
                  else
         | 
| 13 | 
            +
                    field_name = @object_name.match(/.*\[(.*)_translations\]/)[1].capitalize
         | 
| 14 | 
            +
                    super(attribute, field_name, options).html_safe
         | 
| 15 | 
            +
                  end
         | 
| 16 | 
            +
                end
         | 
| 17 | 
            +
                
         | 
| 18 | 
            +
                def text_field(attribute, options = {})
         | 
| 19 | 
            +
                  if @options.has_key?(:language)
         | 
| 20 | 
            +
                    language = @options[:language]
         | 
| 21 | 
            +
                    
         | 
| 22 | 
            +
                    translations = @object.send("#{attribute}_translations") || {}
         | 
| 23 | 
            +
                    
         | 
| 24 | 
            +
                    value = translations.has_key?(language.to_s) ? translations[language.to_s] : nil
         | 
| 25 | 
            +
                    
         | 
| 26 | 
            +
                    super(attribute, :value => value, :id => "#{object_name}_#{attribute}_translations_#{language}", :name => "#{object_name}[#{attribute}_translations][#{language}]").html_safe
         | 
| 27 | 
            +
                  else
         | 
| 28 | 
            +
                    value = @object ? @object[attribute.to_s] : nil
         | 
| 29 | 
            +
                    super(attribute, :value => value).html_safe
         | 
| 30 | 
            +
                  end
         | 
| 31 | 
            +
                end
         | 
| 32 | 
            +
                
         | 
| 33 | 
            +
                def text_area(attribute, options = {})
         | 
| 34 | 
            +
                  if @options.has_key?(:language)
         | 
| 35 | 
            +
                    language = @options[:language]
         | 
| 36 | 
            +
                    
         | 
| 37 | 
            +
                    translations = @object.send("#{attribute}_translations") || {}
         | 
| 38 | 
            +
                    
         | 
| 39 | 
            +
                    value = translations.has_key?(language.to_s) ? translations[language.to_s] : nil
         | 
| 40 | 
            +
                    
         | 
| 41 | 
            +
                    super(attribute, :value => value, :id => "#{object_name}_#{attribute}_translations_#{language}", :name => "#{object_name}[#{attribute}_translations][#{language}]").html_safe
         | 
| 42 | 
            +
                  else
         | 
| 43 | 
            +
                    value = @object ? @object[attribute.to_s] : nil
         | 
| 44 | 
            +
                    
         | 
| 45 | 
            +
                    super(attribute, options.merge(:value => value)).html_safe
         | 
| 46 | 
            +
                  end
         | 
| 47 | 
            +
                end
         | 
| 48 | 
            +
              end
         | 
| 49 | 
            +
            end
         | 
| @@ -0,0 +1,23 @@ | |
| 1 | 
            +
            module LocalizedFields
         | 
| 2 | 
            +
              module Helpers
         | 
| 3 | 
            +
                module FormHelper
         | 
| 4 | 
            +
                  def localized_fields(field = nil, options = {}, &block)
         | 
| 5 | 
            +
                    if field
         | 
| 6 | 
            +
                      field_name = "#{field}_translations"
         | 
| 7 | 
            +
                      object = @object.send(field_name)
         | 
| 8 | 
            +
                      name = "#{object_name}[#{field_name}]"
         | 
| 9 | 
            +
                    
         | 
| 10 | 
            +
                      @template.fields_for(name, object, options.merge(:builder => LocalizedFields::FormBuilder), &block).html_safe
         | 
| 11 | 
            +
                    else
         | 
| 12 | 
            +
                      output = ''
         | 
| 13 | 
            +
                      
         | 
| 14 | 
            +
                      I18n.available_locales.each do |language|
         | 
| 15 | 
            +
                        output << @template.fields_for(object_name, @object, options.merge(:builder => LocalizedFields::FormBuilder, :language => language), &block)
         | 
| 16 | 
            +
                      end
         | 
| 17 | 
            +
                      
         | 
| 18 | 
            +
                      output.html_safe
         | 
| 19 | 
            +
                    end
         | 
| 20 | 
            +
                  end
         | 
| 21 | 
            +
                end
         | 
| 22 | 
            +
              end
         | 
| 23 | 
            +
            end
         | 
    
        data/lib/localized_fields.rb
    CHANGED
    
    | @@ -4,67 +4,10 @@ require 'mongoid' | |
| 4 4 | 
             
            require 'action_view'
         | 
| 5 5 |  | 
| 6 6 | 
             
            module LocalizedFields
         | 
| 7 | 
            -
               | 
| 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
         | 
| 7 | 
            +
              extend ActiveSupport::Autoload
         | 
| 26 8 |  | 
| 27 | 
            -
               | 
| 28 | 
            -
             | 
| 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
         | 
| 9 | 
            +
              autoload :FormBuilder
         | 
| 10 | 
            +
              autoload :Helpers
         | 
| 68 11 | 
             
            end
         | 
| 69 12 |  | 
| 70 | 
            -
            ActionView::Helpers::FormBuilder.send :include, LocalizedFields:: | 
| 13 | 
            +
            ActionView::Helpers::FormBuilder.send :include, LocalizedFields::Helpers::FormHelper
         | 
| @@ -120,5 +120,36 @@ describe 'LocalizedFields' do | |
| 120 120 |  | 
| 121 121 | 
             
                  output.should eq(expected)
         | 
| 122 122 | 
             
                end
         | 
| 123 | 
            +
                
         | 
| 124 | 
            +
                context 'post with values' do
         | 
| 125 | 
            +
                  before do
         | 
| 126 | 
            +
                    @post = Post.new
         | 
| 127 | 
            +
                    @post.stub(:title_translations).and_return({ 'en' => 'title en', 'pt' => 'title pt' })
         | 
| 128 | 
            +
                    @template = ActionView::Base.new
         | 
| 129 | 
            +
                    @template.output_buffer = ''
         | 
| 130 | 
            +
                    @builder = ActionView::Helpers::FormBuilder.new(:post, @post, @template, {}, proc {})
         | 
| 131 | 
            +
                  end
         | 
| 132 | 
            +
                  
         | 
| 133 | 
            +
                  it 'should return a text_area tag for en' do
         | 
| 134 | 
            +
                    output = @builder.localized_fields(:title) do |localized_field|
         | 
| 135 | 
            +
                      localized_field.text_area :en
         | 
| 136 | 
            +
                    end
         | 
| 137 | 
            +
                    
         | 
| 138 | 
            +
                    expected = '<textarea cols="40" id="post_title_translations_en" name="post[title_translations][en]" rows="20">title en</textarea>'
         | 
| 139 | 
            +
                    
         | 
| 140 | 
            +
                    output.should eq(expected)
         | 
| 141 | 
            +
                  end
         | 
| 142 | 
            +
                  
         | 
| 143 | 
            +
                  it 'should return a text_area tag for all languages' do
         | 
| 144 | 
            +
                    output = @builder.localized_fields do |localized_field|
         | 
| 145 | 
            +
                      localized_field.text_area :title
         | 
| 146 | 
            +
                    end
         | 
| 147 | 
            +
                    
         | 
| 148 | 
            +
                    expected = '<textarea cols="40" id="post_title_translations_en" name="post[title_translations][en]" rows="20">title en</textarea>' +
         | 
| 149 | 
            +
                               '<textarea cols="40" id="post_title_translations_pt" name="post[title_translations][pt]" rows="20">title pt</textarea>'
         | 
| 150 | 
            +
                    
         | 
| 151 | 
            +
                    output.should eq(expected)
         | 
| 152 | 
            +
                  end
         | 
| 153 | 
            +
                end
         | 
| 123 154 | 
             
              end
         | 
| 124 155 | 
             
            end
         | 
    
        data/spec/support/post.rb
    CHANGED
    
    
    
        metadata
    CHANGED
    
    | @@ -1,7 +1,7 @@ | |
| 1 1 | 
             
            --- !ruby/object:Gem::Specification
         | 
| 2 2 | 
             
            name: localized_fields
         | 
| 3 3 | 
             
            version: !ruby/object:Gem::Version
         | 
| 4 | 
            -
              version: 0.0. | 
| 4 | 
            +
              version: 0.0.2
         | 
| 5 5 | 
             
              prerelease: 
         | 
| 6 6 | 
             
            platform: ruby
         | 
| 7 7 | 
             
            authors:
         | 
| @@ -9,11 +9,11 @@ authors: | |
| 9 9 | 
             
            autorequire: 
         | 
| 10 10 | 
             
            bindir: bin
         | 
| 11 11 | 
             
            cert_chain: []
         | 
| 12 | 
            -
            date: 2012-01- | 
| 12 | 
            +
            date: 2012-01-06 00:00:00.000000000 Z
         | 
| 13 13 | 
             
            dependencies:
         | 
| 14 14 | 
             
            - !ruby/object:Gem::Dependency
         | 
| 15 15 | 
             
              name: mongoid
         | 
| 16 | 
            -
              requirement: & | 
| 16 | 
            +
              requirement: &2152168120 !ruby/object:Gem::Requirement
         | 
| 17 17 | 
             
                none: false
         | 
| 18 18 | 
             
                requirements:
         | 
| 19 19 | 
             
                - - ! '>='
         | 
| @@ -21,10 +21,10 @@ dependencies: | |
| 21 21 | 
             
                    version: 2.4.0
         | 
| 22 22 | 
             
              type: :runtime
         | 
| 23 23 | 
             
              prerelease: false
         | 
| 24 | 
            -
              version_requirements: * | 
| 24 | 
            +
              version_requirements: *2152168120
         | 
| 25 25 | 
             
            - !ruby/object:Gem::Dependency
         | 
| 26 26 | 
             
              name: actionpack
         | 
| 27 | 
            -
              requirement: & | 
| 27 | 
            +
              requirement: &2152167560 !ruby/object:Gem::Requirement
         | 
| 28 28 | 
             
                none: false
         | 
| 29 29 | 
             
                requirements:
         | 
| 30 30 | 
             
                - - ! '>='
         | 
| @@ -32,10 +32,10 @@ dependencies: | |
| 32 32 | 
             
                    version: 3.1.0
         | 
| 33 33 | 
             
              type: :runtime
         | 
| 34 34 | 
             
              prerelease: false
         | 
| 35 | 
            -
              version_requirements: * | 
| 35 | 
            +
              version_requirements: *2152167560
         | 
| 36 36 | 
             
            - !ruby/object:Gem::Dependency
         | 
| 37 37 | 
             
              name: rspec
         | 
| 38 | 
            -
              requirement: & | 
| 38 | 
            +
              requirement: &2152167060 !ruby/object:Gem::Requirement
         | 
| 39 39 | 
             
                none: false
         | 
| 40 40 | 
             
                requirements:
         | 
| 41 41 | 
             
                - - ! '>='
         | 
| @@ -43,7 +43,7 @@ dependencies: | |
| 43 43 | 
             
                    version: '0'
         | 
| 44 44 | 
             
              type: :development
         | 
| 45 45 | 
             
              prerelease: false
         | 
| 46 | 
            -
              version_requirements: * | 
| 46 | 
            +
              version_requirements: *2152167060
         | 
| 47 47 | 
             
            description: Helps you to create forms with localized fields using Mongoid.
         | 
| 48 48 | 
             
            email:
         | 
| 49 49 | 
             
            - tiagogodinho3@gmail.com
         | 
| @@ -53,6 +53,7 @@ extra_rdoc_files: [] | |
| 53 53 | 
             
            files:
         | 
| 54 54 | 
             
            - .gitignore
         | 
| 55 55 | 
             
            - .rspec
         | 
| 56 | 
            +
            - .travis.yml
         | 
| 56 57 | 
             
            - CHANGELOG.md
         | 
| 57 58 | 
             
            - Gemfile
         | 
| 58 59 | 
             
            - Gemfile.lock
         | 
| @@ -60,6 +61,9 @@ files: | |
| 60 61 | 
             
            - README.md
         | 
| 61 62 | 
             
            - Rakefile
         | 
| 62 63 | 
             
            - lib/localized_fields.rb
         | 
| 64 | 
            +
            - lib/localized_fields/form_builder.rb
         | 
| 65 | 
            +
            - lib/localized_fields/helpers.rb
         | 
| 66 | 
            +
            - lib/localized_fields/helpers/form_helper.rb
         | 
| 63 67 | 
             
            - lib/localized_fields/version.rb
         | 
| 64 68 | 
             
            - localized_fields.gemspec
         | 
| 65 69 | 
             
            - spec/localized_fields_spec.rb
         |