From fb27237d34933f8392c232f21e9317fe9c0372c7 Mon Sep 17 00:00:00 2001 From: Alexandr Borisov Date: Tue, 22 Jul 2014 19:39:59 +0600 Subject: [PATCH] updated READMEs --- README.md | 97 ++++++++++++++++++++++++++++++++++++++++++++++++++++ README.ru.md | 97 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 194 insertions(+) diff --git a/README.md b/README.md index e57789a..ae4df42 100644 --- a/README.md +++ b/README.md @@ -218,6 +218,103 @@ end ``` +## activerecord-sortable for nested models + +```ruby +# app/models/parent.rb + +class Parent < ActiveRecord::Base + has_many :children + + accepts_nested_attributes_for :children +end + +``` + +```ruby +# app/models/child.rb + +class Child < ActiveRecord::Base + acts_as_sortable do |config| + config[:relation] = ->(instance) {instance.parent.children} + end + + belongs_to :parent +end + +``` + +```ruby +# app/models/child.rb + +class ParentsController < ApplicationController + def new + @parent = Parent.new + 3.times do |position| + @parent.children.build( + :name => "Child #{position}", + :position => position # required + ) + end + end + + def create + @parent = Parent.new parent_params + if @parent.save + redirect_to parent_path(@parent) + else + render :new + end + end + + def show + @parent = Parent.find(params[:id]) + end + + + private + + def parent_params + params.require(:parent).permit(:children_attributes => [:name, :position]) + end +end + +``` + +```html + + +

New parent

+ +<%= render :partial => 'form' %> + +``` + +```html + + +<%= form_for @parent do |f| %> +
+ Children + +
    + <%= f.fields_for :children do |ff| %> + +
  1. + <%= ff.object.name %> + + + <%= ff.hidden_field :position, :data => { :role => 'position' } %> +
  2. + <% end %> +
+
+ + <%= f.submit %> +<% end %> + +``` + ## Note on Patches / Pull Requests * Fork the project. diff --git a/README.ru.md b/README.ru.md index 920776b..808863c 100644 --- a/README.ru.md +++ b/README.ru.md @@ -218,6 +218,103 @@ end ``` +## Функциональность activerecord-sortable для вложенных моделей + +```ruby +# app/models/parent.rb + +class Parent < ActiveRecord::Base + has_many :children + + accepts_nested_attributes_for :children +end + +``` + +```ruby +# app/models/child.rb + +class Child < ActiveRecord::Base + acts_as_sortable do |config| + config[:relation] = ->(instance) {instance.parent.children} + end + + belongs_to :parent +end + +``` + +```ruby +# app/models/child.rb + +class ParentsController < ApplicationController + def new + @parent = Parent.new + 3.times do |position| + @parent.children.build( + :name => "Child #{position}", + :position => position # не забыть указать + ) + end + end + + def create + @parent = Parent.new parent_params + if @parent.save + redirect_to parent_path(@parent) + else + render :new + end + end + + def show + @parent = Parent.find(params[:id]) + end + + + private + + def parent_params + params.require(:parent).permit(:children_attributes => [:name, :position]) + end +end + +``` + +```html + + +

New parent

+ +<%= render :partial => 'form' %> + +``` + +```html + + +<%= form_for @parent do |f| %> +
+ Children + +
    + <%= f.fields_for :children do |ff| %> + +
  1. + <%= ff.object.name %> + + + <%= ff.hidden_field :position, :data => { :role => 'position' } %> +
  2. + <% end %> +
+
+ + <%= f.submit %> +<% end %> + +``` + ## Патчи и пулл-реквесты * Сделайте форк.