<commit_before># SlimFormObject
[![Build Status](https://travis-ci.org/woodcrust/slim_form_object.svg?branch=master)](https://travis-ci.org/woodcrust/slim_form_object) [![Code Climate](https://codeclimate.com/github/woodcrust/slim_form_object/badges/gpa.svg)](https://codeclimate.com/github/woodcrust/slim_form_object)

Welcome to your new gem for fast save data of your html forms .

# Installation

Add this line to your application's Gemfile:

```ruby
gem 'slim_form_object'
```

And then execute:

    $ bundle

Or install it yourself as:

    $ gem install slim_form_object

# Usage
## e.g. Model User
/app/models/user.rb
```ruby
    class User < ActiveRecord::Base
      has_many :review_books
      has_many :ratings
      has_and_belongs_to_many :addresses
    end
```
## e.g. Model ReviewBook
/app/models/review_book.rb
```ruby
    class ReviewBook < ActiveRecord::Base
      belongs_to :user
      has_one    :rating
    end
```
## e.g. model Rating
/app/models/rating.rb
```ruby
    class Rating < ActiveRecord::Base
      belongs_to :user
      belongs_to :review_book
    end
## e.g. model Address
/app/models/address.rb
```ruby
    class Address < ActiveRecord::Base
      has_and_belongs_to_many :users
    end
```
## EXAMPLE CLASS of Form Object for review_controller
/app/forms/review_form.rb
```ruby
    class ReviewForm
      include SlimFormObject
      validate :validation_models
      
      #name of model for params.require(:model_name).permit(...) e.g. 'ReviewBook'
      set_model_name('ReviewBook')
      #models which will be updated
      init_models User, Rating, ReviewBook
    
      # create objects for models
      def initialize(params: {}, current_user: nil)
        self.user             = current_user
        self.review_book      = ReviewBook.new
        self.rating           = Rating.new
        
        #hash of attributes
        self.params           = params
      end
    end
```
## EXAMPLE CONTROLLER of review_controller
/app/forms/review_controller.rb
 ```ruby
    class ReviewController < ApplicationController
      def new
        @reviewForm = ReviewForm.new(current_user: current_user)
      end
    
      def create
        reviewForm = ReviewForm.new(params: params_review, current_user: current_user)
        reviewForm.submit
        if reviewForm.save
          render json: {status: 200}
        else
          render json: reviewForm.errors, status: 422
        end
      end
    
      private
    
      def params_review
        params.require(:review_book).permit(:rating_ratings, :review_book_theme, :review_book_text, :user_address_ids => [])
      end
    end
```


## HTML FORM (Haml)

!!! this naming should be to successfully save your models !!!

example name of attributes: 
name_model and name_attribute_of_your_model => name_model_name_attribute_of_your_model 

e.g. review_book and theme => review_book_theme  or  rating and value => rating_value
```haml
      = form_for @reviewForm, url: reviews_path, method: 'POST', html: {class: 'form-control'} do |f|
        = f.number_field :rating_value,      placeholder: "Rating", class: 'form-control input-checkout'
        = f.text_field   :review_book_theme, placeholder: "Theme",  class: 'form-control input-checkout'
        = f.text_field   :review_book_text,  placeholder: "Text",   class: 'form-control input-checkout'
```
## FOR COLLECTION you must to use _ids in your name attributes:

name_model and name_attribute_of_your_model and ids => name_model_name_attribute_of_your_model_ids 

e.g. user and address and ids => user_address_ids
```haml
        = f.collection_select(:user_address_ids, Address.all, :id, :column_name, {selected: @settings_form.user.address_ids}, {multiple: true, class: "form-control input-address"})

        = f.submit 'Create review',                                 class: 'form-control btn btn-success'
```

## Contributing

1. Fork it
2. Create your feature branch (`git checkout -b my-new-feature`)
3. Commit your changes (`git commit -am 'Add some feature'`)
4. Push to the branch (`git push origin my-new-feature`)
5. Create new Pull Request


## License

The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
<commit_msg>fix readme<commit_after># SlimFormObject
[![Build Status](https://travis-ci.org/woodcrust/slim_form_object.svg?branch=master)](https://travis-ci.org/woodcrust/slim_form_object) [![Code Climate](https://codeclimate.com/github/woodcrust/slim_form_object/badges/gpa.svg)](https://codeclimate.com/github/woodcrust/slim_form_object)

Welcome to your new gem for fast save data of your html forms .

# Installation

Add this line to your application's Gemfile:

```ruby
gem 'slim_form_object'
```

And then execute:

    $ bundle

Or install it yourself as:

    $ gem install slim_form_object

# Usage
## e.g. Model User
/app/models/user.rb
```ruby
    class User < ActiveRecord::Base
      has_many :review_books
      has_many :ratings
      has_and_belongs_to_many :addresses
    end
```
## e.g. Model ReviewBook
/app/models/review_book.rb
```ruby
    class ReviewBook < ActiveRecord::Base
      belongs_to :user
      has_one    :rating
    end
```
## e.g. model Rating
/app/models/rating.rb
```ruby
    class Rating < ActiveRecord::Base
      belongs_to :user
      belongs_to :review_book
    end
```
## e.g. model Address
/app/models/address.rb
```ruby
    class Address < ActiveRecord::Base
      has_and_belongs_to_many :users
    end
```
## EXAMPLE CLASS of Form Object for review_controller
/app/forms/review_form.rb
```ruby
    class ReviewForm
      include SlimFormObject
      validate :validation_models
      
      #name of model for params.require(:model_name).permit(...) e.g. 'ReviewBook'
      set_model_name('ReviewBook')
      #models which will be updated
      init_models User, Rating, ReviewBook
    
      # create objects for models
      def initialize(params: {}, current_user: nil)
        self.user             = current_user
        self.review_book      = ReviewBook.new
        self.rating           = Rating.new
        
        #hash of attributes
        self.params           = params
      end
    end
```
## EXAMPLE CONTROLLER of review_controller
/app/forms/review_controller.rb
 ```ruby
    class ReviewController < ApplicationController
      def new
        @reviewForm = ReviewForm.new(current_user: current_user)
      end
    
      def create
        reviewForm = ReviewForm.new(params: params_review, current_user: current_user)
        reviewForm.submit
        if reviewForm.save
          render json: {status: 200}
        else
          render json: reviewForm.errors, status: 422
        end
      end
    
      private
    
      def params_review
        params.require(:review_book).permit(:rating_ratings, :review_book_theme, :review_book_text, :user_address_ids => [])
      end
    end
```


## HTML FORM (Haml)

!!! this naming should be to successfully save your models !!!

example name of attributes: 
name_model and name_attribute_of_your_model => name_model_name_attribute_of_your_model 

e.g. *review_book* and *theme* => **review_book_theme** or *rating* and *value* => **rating_value**
```haml
      = form_for @reviewForm, url: reviews_path, method: 'POST', html: {class: 'form-control'} do |f|
        = f.number_field :rating_value,      placeholder: "Rating", class: 'form-control input-checkout'
        = f.text_field   :review_book_theme, placeholder: "Theme",  class: 'form-control input-checkout'
        = f.text_field   :review_book_text,  placeholder: "Text",   class: 'form-control input-checkout'
```
## FOR COLLECTION you must to use _ids in your name attributes:

*name_model* and *name_attribute_of_your_model* and *ids* => **name_model_name_attribute_of_your_model_ids** 

e.g. *user* and *address* and *ids* => **user_address_ids**
```haml
        = f.collection_select(:user_address_ids, Address.all, :id, :column_name, {selected: @settings_form.user.address_ids}, {multiple: true, class: "form-control input-address"})

        = f.submit 'Create review',                                 class: 'form-control btn btn-success'
```

## Contributing

1. Fork it
2. Create your feature branch (`git checkout -b my-new-feature`)
3. Commit your changes (`git commit -am 'Add some feature'`)
4. Push to the branch (`git push origin my-new-feature`)
5. Create new Pull Request


## License

The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).

{'fraction_non_alphanumeric': 0.10514593834251554, 'fraction_numerical': 0.002578830148868831, 'mean_word_length': 3.1197489135683245, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 15, 'https://': 8, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '433700', 'n_tokens_mistral': 2698, 'n_tokens_neox': 2602, 'n_words': 705}