Skip to content

Commit

Permalink
Store form template is data-releaf-template attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
graudeejs committed May 28, 2014
1 parent 903f7b3 commit c843426
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 79 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -2,6 +2,8 @@

### 2014.05.28
* Removed Releaf::TemplateFilter includable module.
* Refactored how releaf stores form templates.
Form templates are now stored in containers data-releaf-template html attribute.

### 2014.05.15
* Releaf::ResourceValidator was renamed to Releaf::ErrorFormatter.
Expand Down
34 changes: 7 additions & 27 deletions releaf-core/app/assets/javascripts/releaf/include/nested_fields.js
Expand Up @@ -16,20 +16,11 @@ jQuery( document ).ready(function()
var list = block.find('.list').first();

var block_name = block.attr('data-name');
var template_selector = '.item[data-name="' + block_name + '"].template';
var item_selector = '.item[data-name="' + block_name + '"]:not(.template)';
var template_holder_selector = '[data-releaf-template]';
var item_selector = '.item[data-name="' + block_name + '"]';

var new_item_selector = '.item[data-name="' + block_name + '"].new';
var existing_item_selector = '.item[data-name="' + block_name + '"]:not(.template,.new)';

target.find(template_selector).find('button, input, select, textarea').each(function() {
var input = jQuery(this);
if (!input.is('[disabled="disabled"]'))
{
input.addClass('releaf-template-field-disabled');
input.attr('disabled', 'disabled');
}
});
var existing_item_selector = '.item[data-name="' + block_name + '"]:not(.new)';


block.click( function( event, event_params )
Expand Down Expand Up @@ -67,7 +58,7 @@ jQuery( document ).ready(function()

if (trigger.is('.add-nested-item'))
{
var template = target_block.find( template_selector ).first();
var template = jQuery(target_block.data('releaf-template'));
if (template.length != 1)
{
if (typeof console != 'undefined' && 'log' in console)
Expand All @@ -77,20 +68,10 @@ jQuery( document ).ready(function()
return null;
}

var new_item = template.clone( false );

new_item.removeClass( 'template' ).addClass('new');
new_item.find('.releaf-template-field-disabled[disabled="disabled"]').each(function() {
var input = jQuery(this);
if (input.parents('.template').length == 0)
{
// Only enable fields for inputs that are not part of template
// This is needed for nested fields with nested fields, to prevent
// enabling inputs of nested template
input.removeAttr('disabled');
input.removeClass('releaf-template-field-disabled');
}
});
var new_item = template;

new_item.addClass('new');

new_item.appendTo( list );

Expand Down Expand Up @@ -189,7 +170,6 @@ jQuery( document ).ready(function()

block.on('nestedfieldsreindex', function( e )
{

// update data-index attributes and names/ids for all fields inside a block

// in case of nested blocks, this bubbles up and gets called for each parent block also
Expand Down
Expand Up @@ -8,17 +8,17 @@
- case reflection.macro
- when :has_many
- sortable_objects = reflection.klass.column_names.include?('item_position')
.nested-wrap{:'data-name' => field}
%h3.subheader.nested-title= t(field, :scope => controller_scope_name)
.list{:'data-sortable' => sortable_objects ? '' : nil}

- template_html = capture do
.item.clearInside{ :'data-name' => field}
= f.fields_for field, reflection.klass.new, :child_index => '_template_' do |ff|
= render 'edit.fields.has_many_template', :f => ff, :sortable_objects => sortable_objects, :subfields => subfields, :view_prefix => new_view_prefix, :field => field
-# for templates always show remove button
= render 'edit.field_type_remove_nested', f: ff, name: "_destroy"

.item.clearInside.template{ :'data-name' => field}
= f.fields_for field, reflection.klass.new, :child_index => '_template_' do |ff|
= render 'edit.fields.has_many_template', :f => ff, :sortable_objects => sortable_objects, :subfields => subfields, :view_prefix => new_view_prefix, :field => field
-# for templates always show remove button
= render 'edit.field_type_remove_nested', f: ff, name: "_destroy"

.nested-wrap{data: { name: field, releaf: { template: html_escape(template_html) }}}
%h3.subheader.nested-title= t(field, :scope => controller_scope_name)
.list{:'data-sortable' => sortable_objects ? '' : nil}
- allow_destroy = reflection.active_record.nested_attributes_options.fetch(reflection.name, {}).fetch(:allow_destroy, false)
- f.object.send(field).each_with_index do |obj, i|
.item.clearInside{:'data-name' => field, :'data-index' => i}
Expand Down
33 changes: 2 additions & 31 deletions releaf-core/spec/features/edit_actions_spec.rb
Expand Up @@ -65,47 +65,18 @@
expect(page).to have_css('#resource_chapters_attributes_0_title[value="Chapter 1"]')
end

scenario "nested fields tempalte is not submitted" do
scenario "adding newsted objects" do
visit new_admin_book_path
within "form.new_resource" do
fill_in "Title", with: "Master and Margarita"
within "[data-name='chapters']" do
# make sure template fields are properly disabled
expect( page ).to have_selector('.template input[disabled="disabled"].releaf-template-field-disabled', visible: false)
expect( page ).to have_selector('.template textarea[disabled="disabled"].releaf-template-field-disabled', visible: false)

# verify that there are no visible inputs
expect( page ).to have_no_selector('input', visible: true)
expect( page ).to have_no_selector('textarea', visible: true)
expect( page ).to have_button "Add item"
end
end
save_and_check_response "Create succeeded"

new_book = Book.where(title: "Master and Margarita").first
expect( new_book.chapters.count ).to eq 0
end

scenario "new nested objects are submitted" do
visit new_admin_book_path
within "form.new_resource" do
fill_in "Title", with: "Master and Margarita"
within "[data-name='chapters']" do
# make sure template fields are properly disabled
expect( page ).to have_selector('.template input[disabled="disabled"].releaf-template-field-disabled', visible: false)
expect( page ).to have_selector('.template textarea[disabled="disabled"].releaf-template-field-disabled', visible: false)

# verify that there are no visible inputs
expect( page ).to have_no_selector('input', visible: true)
expect( page ).to have_no_selector('textarea', visible: true)

click_button "Add item"
# check that releaf-template-field-disabled class is removed from inputs
expect( page ).to have_no_selector('input.releaf-template-field-disabled', visible: true)
expect( page ).to have_no_selector('textarea.releaf-template-field-disabled', visible: true)
# check that inputs are enabled
expect( page ).to have_no_selector('input[disabled="disabled"]', visible: true)
expect( page ).to have_no_selector('textarea[disabled="disabled"]', visible: true)

fill_in "Title", with: "Chapter 1"
fill_in "Text", with: "some text"
end
Expand Down
25 changes: 14 additions & 11 deletions releaf-i18n/app/views/releaf/translations/_edit.body.html.haml
Expand Up @@ -3,7 +3,20 @@
.body
- attributes_scope = "activerecord.attributes.#{resource_class.name.underscore}"
- @translation_ids_to_destroy ||= []
.nested-wrap{"data-name" => "translations"}

- template_html = capture do
%tr.item{ data: { name: :translations }, style: 'display:none' }
%td.translation_name.codeColumn
.wrap
%input{type: "text", name: "translations[][key]"}
- locales.each_with_index do |locale, j|
%td.translationCell{data: {locale: locale}}
%input{type: "text", name: "translations[][localizations][#{locale}]"}
%td.delete_column
%button.button.danger.only-icon.remove-nested-item{type: "button", title: t("Remove item", scope: controller_scope_name)}
%i.fa.fa-times

.nested-wrap{data: {name: "translations", releaf: {template: html_escape(template_html)}}}
%table.table
%thead
%tr
Expand All @@ -26,16 +39,6 @@
%button.button.danger.only-icon.remove-nested-item{type: "button", title: t("Remove item", scope: controller_scope_name)}
%i.fa.fa-times

%tr.template.item{ data: { name: :translations }, style: 'display:none' }
%td.translation_name.codeColumn
.wrap
%input{type: "text", name: "translations[][key]"}
- locales.each_with_index do |locale, j|
%td.translationCell{data: {locale: locale}}
%input{type: "text", name: "translations[][localizations][#{locale}]"}
%td.delete_column
%button.button.danger.only-icon.remove-nested-item{type: "button", title: t("Remove item", scope: controller_scope_name)}
%i.fa.fa-times

%button.button.add-nested-item{:type => 'button'}
%i.fa.fa-plus
Expand Down
2 changes: 1 addition & 1 deletion releaf-i18n/spec/features/translations_spec.rb
Expand Up @@ -44,7 +44,7 @@
click_button "Save"
expect(page).to have_notification("Update failed", :error)

within ".table tr.item:nth-child(2)" do
within ".table tr.item:last-child" do
click_button "Remove item"
end
expect(page).to have_css(".table tr.item", count: 1) # wait for fade out to complete
Expand Down

0 comments on commit c843426

Please sign in to comment.