From 4ae68c15f9ad9fcf84d775eedfab3aa2d97bdb67 Mon Sep 17 00:00:00 2001 From: Florent FERRY Date: Tue, 5 Dec 2017 17:23:21 +0100 Subject: [PATCH 01/17] Drop affix --- app/assets/javascripts/ouvrages_block.js | 61 +++++-------------- app/views/admin/blocks/_blocks_form.html.haml | 37 +++-------- app/views/admin/blocks/_buttons.html.haml | 8 +++ lib/ouvrages_block.rb | 2 - lib/ouvrages_block/block_field.rb | 4 +- ouvrages_block.gemspec | 2 +- 6 files changed, 34 insertions(+), 80 deletions(-) create mode 100644 app/views/admin/blocks/_buttons.html.haml diff --git a/app/assets/javascripts/ouvrages_block.js b/app/assets/javascripts/ouvrages_block.js index 21b73dd..963f6cf 100644 --- a/app/assets/javascripts/ouvrages_block.js +++ b/app/assets/javascripts/ouvrages_block.js @@ -1,33 +1,24 @@ -//= require scrollTo - -$(document).on("click", ".collapse-block-buttons", function(e) { - e.preventDefault(); - - var $button = $(this); - - var $buttons = $button.closest(".block-buttons-group").find(".block-button"); - $button.closest(".list-group").find(".block-button").not($buttons).addClass("hide"); - $buttons.toggleClass("hide"); -}); - -$(document).on("click", ".block-buttons .block-button", function(e) { +$(document).on("click", ".block-button", function(e) { e.preventDefault(); var $button = $(this); + var $buttonWrapper = $button.closest(".block-form"); + var $blockForms = $button.closest(".ouvrages-blocks").find(".block-forms"); + var buttonsHTML = $blockForms.data("buttons").html; - var $newBlock = generateBlockForm($button); - $(".block-forms").append($newBlock); + var $newBlock = generateBlockForm($button, buttonsHTML); + + if ($buttonWrapper.length === 0) { + $blockForms.append($newBlock); + } else { + $newBlock.insertBefore($buttonWrapper); + } updateBlockFormPositions(); $(document).trigger("blocks:add", $newBlock); - - $(document).scrollTo($newBlock, { - offset: $newBlock.outerHeight(), - duration: 500 - }); }); -generateBlockForm = function(button) { +generateBlockForm = function(button, buttonsHTML) { var $button = $(button); var formData = $button.data("form").html; var blocksCount = $(".block-form").length; @@ -35,7 +26,9 @@ generateBlockForm = function(button) { formData = formData.replace(/\[__.+?__\]/g, "[" + blocksCount + "]"); formData = formData.replace(/___NEW__.+?___/g, "_" + blocksCount + "_"); - return $("
" + formData + "
"); + buttonsHTML = buttonsHTML.replace(/__ID__/g, "_" + blocksCount + "_"); + + return $("
" + buttonsHTML + "
" + formData + "
"); }; updateBlockFormPositions = function() { @@ -46,17 +39,6 @@ updateBlockFormPositions = function() { }; createSortable = function() { - $(".block-buttons .list-group .block-buttons-group").sortable({ - group: { - name: "forms", - pull: 'clone', - put: false, - }, - filter: ".collapse-block-buttons", - sort: false, - animation: 150, - }); - $(".block-forms").sortable({ group: { name: "forms", @@ -106,19 +88,13 @@ $(document).on("blocks:move", function(e, block) { $(block).initRichTextareas(); }); -$(document).on('turbolinks:before-render', function() { +$(document).on('turbolinks:before-cache', function() { tinyMCE.remove(); }); $(document).on('turbolinks:load', function() { $(document.body).initRichTextareas(); createSortable(); - - $("#block-buttons-inner-affix").affix({ - bottom: function() { - return (this.bottom = $(".block-forms").outerHeight() - $("#block-buttons-inner-affix").height()); - }, - }) }); $(document).on("click", ".remove-block-form-button", function(e) { @@ -150,8 +126,3 @@ $(document).on("click", ".collapse-block-form-button", function(e) { $blockFormIcon.addClass('glyphicon-minus'); } }); - - -$(window).on("scroll", function(e) { - $("#block-buttons-inner-affix.affix").css("top", $(window).height() - $("#block-buttons-inner-affix").outerHeight() + "px"); -}); diff --git a/app/views/admin/blocks/_blocks_form.html.haml b/app/views/admin/blocks/_blocks_form.html.haml index a0eac88..b16f50a 100644 --- a/app/views/admin/blocks/_blocks_form.html.haml +++ b/app/views/admin/blocks/_blocks_form.html.haml @@ -1,33 +1,10 @@ %h3= form.object.class.human_attribute_name(:blocks) -.row - .block-forms{class: options[:block_left_class] } +.ouvrages-blocks + - buttons = render partial: "admin/blocks/buttons", locals: { id: "___ID___", form: form } + .block-forms{data: { buttons: { html: buttons }.to_json }} - form.object.blocks.each do |block| - .block-form.panel.panel-primary= form.block_form block - - .block-buttons{class: options[:block_right_class] } - #block-buttons-inner-affix.affix-top - - if form.object.block_buttons.is_a?(Hash) - .list-group - - form.object.block_buttons.each do |group_name, block_names| - .block-buttons-group - %button.list-group-item.collapse-block-buttons= group_name - - block_names.each do |block_name| - - new_block = form.block_form block_name.to_s.singularize.classify.constantize.new, template: true - - new_block = new_block.html_safe - %button.block-button.list-group-item{data: { form: { html: new_block }.to_json }, type: "button", class: form.object.block_buttons.keys.size > 1 ? "hide" : nil} - .pull-left - %span.glyphicon.glyphicon-plus - = block_name.to_s.classify.constantize.model_name.human - - - else - .list-group - .block-buttons-group - - form.object.block_buttons.each do |block_name| - - new_block = form.block_form block_name.to_s.singularize.classify.constantize.new, template: true - - new_block = new_block.html_safe - %button.block-button.list-group-item{data: { form: { html: new_block }.to_json }, type: "button"} - .pull-left - %span.glyphicon.glyphicon-plus - = block_name.to_s.classify.constantize.model_name.human - + .block-form + = render partial: "admin/blocks/buttons", locals: { id: block.id, form: form } + .panel.panel-primary= form.block_form block + = render partial: "admin/blocks/buttons", locals: { id: "all", form: form } diff --git a/app/views/admin/blocks/_buttons.html.haml b/app/views/admin/blocks/_buttons.html.haml new file mode 100644 index 0000000..26ee1e4 --- /dev/null +++ b/app/views/admin/blocks/_buttons.html.haml @@ -0,0 +1,8 @@ +.block-buttons{data: { target: id } } + %button{type: "button", data: { toggle: "collapse", target: "#buttons-#{id}" }} Ajouter un bloc + .collapse{id: "buttons-#{id}"} + - form.object.block_buttons.each do |block_name| + - new_block = form.block_form block_name.to_s.singularize.classify.constantize.new, template: true + - new_block = new_block.html_safe + %button{data: { form: { html: new_block }.to_json, id: id }, type: "button", class: "block-button"} + = block_name.to_s.classify.constantize.model_name.human diff --git a/lib/ouvrages_block.rb b/lib/ouvrages_block.rb index d3434f7..e3661bb 100644 --- a/lib/ouvrages_block.rb +++ b/lib/ouvrages_block.rb @@ -4,8 +4,6 @@ require "ouvrages_block/version" require "ouvrages_block/block_field" -require "scrollto-rails" - module OuvragesBlock module Rails diff --git a/lib/ouvrages_block/block_field.rb b/lib/ouvrages_block/block_field.rb index 30914cc..a778479 100644 --- a/lib/ouvrages_block/block_field.rb +++ b/lib/ouvrages_block/block_field.rb @@ -1,6 +1,6 @@ module BlockField - def blocks_form(method = nil, options = { block_left_class: "col-xs-10", block_right_class: "col-xs-2" }) - @template.render partial: "admin/blocks/blocks_form", locals: { form: self, options: options} + def blocks_form(method = nil) + @template.render partial: "admin/blocks/blocks_form", locals: { form: self } end def block_form(block, options = {}) diff --git a/ouvrages_block.gemspec b/ouvrages_block.gemspec index fadfba5..eaf1def 100644 --- a/ouvrages_block.gemspec +++ b/ouvrages_block.gemspec @@ -32,7 +32,7 @@ Gem::Specification.new do |spec| spec.add_runtime_dependency "activerecord", "~> 5.0" spec.add_runtime_dependency "bootstrap_form", '>= 2.5.2' - spec.add_runtime_dependency "scrollto-rails" + spec.add_runtime_dependency "sortable-rails" spec.add_runtime_dependency "tinymce-rails" spec.add_runtime_dependency "jquery-fileupload-rails" From 94c2b2a84990ef7a06be2cfbe22132e31752ce25 Mon Sep 17 00:00:00 2001 From: Florent FERRY Date: Fri, 8 Dec 2017 11:06:03 +0100 Subject: [PATCH 02/17] Convert to slim --- lib/generators/block/block_generator.rb | 4 ++-- .../templates/views/{admin.html.haml => admin.html.slim} | 0 .../block/templates/views/{show.html.haml => show.html.slim} | 0 3 files changed, 2 insertions(+), 2 deletions(-) rename lib/generators/block/templates/views/{admin.html.haml => admin.html.slim} (100%) rename lib/generators/block/templates/views/{show.html.haml => show.html.slim} (100%) diff --git a/lib/generators/block/block_generator.rb b/lib/generators/block/block_generator.rb index 5bdd50c..b35622e 100644 --- a/lib/generators/block/block_generator.rb +++ b/lib/generators/block/block_generator.rb @@ -31,8 +31,8 @@ def create_locale_files end def create_view_files - template "views/admin.html.haml", File.join("app", "views", "admin", standard_block_name_plural, "_block_form.html.haml") - template "views/show.html.haml", File.join("app", "views", standard_block_name_plural, "_#{standard_block_name_singular}.html.haml") + template "views/admin.html.slim", File.join("app", "views", "admin", standard_block_name_plural, "_block_form.html.slim") + template "views/show.html.slim", File.join("app", "views", standard_block_name_plural, "_#{standard_block_name_singular}.html.slim") end private diff --git a/lib/generators/block/templates/views/admin.html.haml b/lib/generators/block/templates/views/admin.html.slim similarity index 100% rename from lib/generators/block/templates/views/admin.html.haml rename to lib/generators/block/templates/views/admin.html.slim diff --git a/lib/generators/block/templates/views/show.html.haml b/lib/generators/block/templates/views/show.html.slim similarity index 100% rename from lib/generators/block/templates/views/show.html.haml rename to lib/generators/block/templates/views/show.html.slim From 55a6db0f21e10d25f2cb804b7931cbfe113808c8 Mon Sep 17 00:00:00 2001 From: Florent FERRY Date: Fri, 8 Dec 2017 11:51:58 +0100 Subject: [PATCH 03/17] Handle Rails versions > 5 --- lib/generators/block/block_generator.rb | 8 ++++++++ lib/generators/block/templates/migrations/migration.rb | 2 +- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/lib/generators/block/block_generator.rb b/lib/generators/block/block_generator.rb index b35622e..ace4ad3 100644 --- a/lib/generators/block/block_generator.rb +++ b/lib/generators/block/block_generator.rb @@ -52,4 +52,12 @@ def standard_block_name_singular def standard_block_name_plural standard_block_name_singular.pluralize end + + def migration_class_name + if Rails::VERSION::MAJOR >= 5 + "ActiveRecord::Migration[#{Rails::VERSION::MAJOR}.#{Rails::VERSION::MINOR}]" + else + 'ActiveRecord::Migration' + end + end end diff --git a/lib/generators/block/templates/migrations/migration.rb b/lib/generators/block/templates/migrations/migration.rb index 020dfa2..96d3399 100644 --- a/lib/generators/block/templates/migrations/migration.rb +++ b/lib/generators/block/templates/migrations/migration.rb @@ -1,4 +1,4 @@ -class Create<%= standard_block_name_plural.capitalize %> < ActiveRecord::Migration[<%= ActiveRecord::Migration.current_version %>] +class Create<%= standard_block_name_plural.capitalize %> < <%= migration_class_name %> def up create_table :<%= standard_block_name_plural %> do |t| t.references :parent, polymorphic: true From a1fc605360153fc540c5250efe282e3b5b61e434 Mon Sep 17 00:00:00 2001 From: Florent FERRY Date: Fri, 8 Dec 2017 11:54:06 +0100 Subject: [PATCH 04/17] Change depencies --- ouvrages_block.gemspec | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ouvrages_block.gemspec b/ouvrages_block.gemspec index eaf1def..337aecf 100644 --- a/ouvrages_block.gemspec +++ b/ouvrages_block.gemspec @@ -30,8 +30,8 @@ Gem::Specification.new do |spec| spec.add_development_dependency "bundler", "~> 1.11" spec.add_development_dependency "rake", "~> 10.0" - spec.add_runtime_dependency "activerecord", "~> 5.0" - spec.add_runtime_dependency "bootstrap_form", '>= 2.5.2' + spec.add_runtime_dependency "activerecord", ">= 4.0" + spec.add_runtime_dependency "bootstrap_form", '>= 2.3.0' spec.add_runtime_dependency "sortable-rails" spec.add_runtime_dependency "tinymce-rails" From d0df8b8cb99d37c915c2219cec0d04114f49be96 Mon Sep 17 00:00:00 2001 From: Florent FERRY Date: Fri, 8 Dec 2017 12:12:53 +0100 Subject: [PATCH 05/17] Fix engine --- lib/ouvrages_block.rb | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/lib/ouvrages_block.rb b/lib/ouvrages_block.rb index e3661bb..fb4d8fe 100644 --- a/lib/ouvrages_block.rb +++ b/lib/ouvrages_block.rb @@ -5,10 +5,7 @@ require "ouvrages_block/block_field" module OuvragesBlock - - module Rails - class Engine < ::Rails::Engine - end + class Engine < ::Rails::Engine end extend ActiveSupport::Concern From 6ae0bdad709d19a94387d9314dba508798324b02 Mon Sep 17 00:00:00 2001 From: Florent FERRY Date: Mon, 11 Dec 2017 12:07:54 +0100 Subject: [PATCH 06/17] Migrate block partial from haml to slim --- .../{_block_form.html.haml => _block_form.html.slim} | 6 +++--- .../{_blocks_form.html.haml => _blocks_form.html.slim} | 2 +- app/views/admin/blocks/_buttons.html.haml | 8 -------- app/views/admin/blocks/_buttons.html.slim | 8 ++++++++ 4 files changed, 12 insertions(+), 12 deletions(-) rename app/views/admin/blocks/{_block_form.html.haml => _block_form.html.slim} (73%) rename app/views/admin/blocks/{_blocks_form.html.haml => _blocks_form.html.slim} (89%) delete mode 100644 app/views/admin/blocks/_buttons.html.haml create mode 100644 app/views/admin/blocks/_buttons.html.slim diff --git a/app/views/admin/blocks/_block_form.html.haml b/app/views/admin/blocks/_block_form.html.slim similarity index 73% rename from app/views/admin/blocks/_block_form.html.haml rename to app/views/admin/blocks/_block_form.html.slim index 6ecf4d3..dac32a9 100644 --- a/app/views/admin/blocks/_block_form.html.haml +++ b/app/views/admin/blocks/_block_form.html.slim @@ -4,9 +4,9 @@ .panel-title = block_name.to_s.classify.constantize.model_name.human .pull-right - %span.glyphicon.glyphicon-minus.collapse-block-form-button - %span.glyphicon.glyphicon-trash.remove-block-form-button - %span.glyphicon.glyphicon-resize-vertical.handler + span.glyphicon.glyphicon-minus.collapse-block-form-button + span.glyphicon.glyphicon-trash.remove-block-form-button + span.glyphicon.glyphicon-resize-vertical.handler .panel-body.collapse.in = form.fields_for block_name, block, child_index: index do |t| = t.hidden_field :id, value: block.id diff --git a/app/views/admin/blocks/_blocks_form.html.haml b/app/views/admin/blocks/_blocks_form.html.slim similarity index 89% rename from app/views/admin/blocks/_blocks_form.html.haml rename to app/views/admin/blocks/_blocks_form.html.slim index b16f50a..634b6cb 100644 --- a/app/views/admin/blocks/_blocks_form.html.haml +++ b/app/views/admin/blocks/_blocks_form.html.slim @@ -1,4 +1,4 @@ -%h3= form.object.class.human_attribute_name(:blocks) +h3= form.object.class.human_attribute_name(:blocks) .ouvrages-blocks - buttons = render partial: "admin/blocks/buttons", locals: { id: "___ID___", form: form } diff --git a/app/views/admin/blocks/_buttons.html.haml b/app/views/admin/blocks/_buttons.html.haml deleted file mode 100644 index 26ee1e4..0000000 --- a/app/views/admin/blocks/_buttons.html.haml +++ /dev/null @@ -1,8 +0,0 @@ -.block-buttons{data: { target: id } } - %button{type: "button", data: { toggle: "collapse", target: "#buttons-#{id}" }} Ajouter un bloc - .collapse{id: "buttons-#{id}"} - - form.object.block_buttons.each do |block_name| - - new_block = form.block_form block_name.to_s.singularize.classify.constantize.new, template: true - - new_block = new_block.html_safe - %button{data: { form: { html: new_block }.to_json, id: id }, type: "button", class: "block-button"} - = block_name.to_s.classify.constantize.model_name.human diff --git a/app/views/admin/blocks/_buttons.html.slim b/app/views/admin/blocks/_buttons.html.slim new file mode 100644 index 0000000..5ee1bb4 --- /dev/null +++ b/app/views/admin/blocks/_buttons.html.slim @@ -0,0 +1,8 @@ +.block-buttons(data={ target: id }) + button(type="button" data={ toggle: "collapse", target: "#buttons-#{id}" }) Ajouter un bloc + .collapse(id="buttons-#{id}") + - form.object.block_buttons.each do |block_name| + - new_block = form.block_form block_name.to_s.singularize.classify.constantize.new, template: true + - new_block = new_block.html_safe + button(data={ form: { html: new_block }.to_json, id: id } type="button" class="block-button") + = block_name.to_s.classify.constantize.model_name.human From bae9810308a58b0c7e0766fe0c8b2a921d9bbccc Mon Sep 17 00:00:00 2001 From: Florent FERRY Date: Mon, 11 Dec 2017 12:11:02 +0100 Subject: [PATCH 07/17] Fix missing tag --- app/views/admin/blocks/_blocks_form.html.slim | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/views/admin/blocks/_blocks_form.html.slim b/app/views/admin/blocks/_blocks_form.html.slim index 634b6cb..3187931 100644 --- a/app/views/admin/blocks/_blocks_form.html.slim +++ b/app/views/admin/blocks/_blocks_form.html.slim @@ -2,7 +2,7 @@ h3= form.object.class.human_attribute_name(:blocks) .ouvrages-blocks - buttons = render partial: "admin/blocks/buttons", locals: { id: "___ID___", form: form } - .block-forms{data: { buttons: { html: buttons }.to_json }} + .block-forms(data={ buttons: { html: buttons }.to_json }) - form.object.blocks.each do |block| .block-form = render partial: "admin/blocks/buttons", locals: { id: block.id, form: form } From e1173fc3902aeb076ab91865ffe7e39235e6cefe Mon Sep 17 00:00:00 2001 From: Florent Ferry Date: Mon, 11 Dec 2017 20:06:34 +0100 Subject: [PATCH 08/17] Fix naming on block generation --- lib/generators/block/block_generator.rb | 8 ++++++++ lib/generators/block/templates/migrations/migration.rb | 2 +- lib/generators/block/templates/models/model.rb.erb | 2 +- 3 files changed, 10 insertions(+), 2 deletions(-) diff --git a/lib/generators/block/block_generator.rb b/lib/generators/block/block_generator.rb index ace4ad3..b28dcac 100644 --- a/lib/generators/block/block_generator.rb +++ b/lib/generators/block/block_generator.rb @@ -53,6 +53,14 @@ def standard_block_name_plural standard_block_name_singular.pluralize end + def standard_block_class_name + standard_block_name_singular.camelize + end + + def standard_block_migration_name + standard_block_name_plural.camelize + end + def migration_class_name if Rails::VERSION::MAJOR >= 5 "ActiveRecord::Migration[#{Rails::VERSION::MAJOR}.#{Rails::VERSION::MINOR}]" diff --git a/lib/generators/block/templates/migrations/migration.rb b/lib/generators/block/templates/migrations/migration.rb index 96d3399..7bb42e7 100644 --- a/lib/generators/block/templates/migrations/migration.rb +++ b/lib/generators/block/templates/migrations/migration.rb @@ -1,4 +1,4 @@ -class Create<%= standard_block_name_plural.capitalize %> < <%= migration_class_name %> +class Create<%= standard_block_migration_name %> < <%= migration_class_name %> def up create_table :<%= standard_block_name_plural %> do |t| t.references :parent, polymorphic: true diff --git a/lib/generators/block/templates/models/model.rb.erb b/lib/generators/block/templates/models/model.rb.erb index be805b4..49fe067 100644 --- a/lib/generators/block/templates/models/model.rb.erb +++ b/lib/generators/block/templates/models/model.rb.erb @@ -1,4 +1,4 @@ -class <%= standard_block_name_singular.capitalize %> < ApplicationRecord +class <%= standard_block_class_name %> < ApplicationRecord belongs_to :parent, polymorphic: true def self.permitted_attributes From 16a4918d9338b771c0fc120014a3042b67f6159d Mon Sep 17 00:00:00 2001 From: Florent Ferry Date: Mon, 11 Dec 2017 21:05:10 +0100 Subject: [PATCH 09/17] Prepare v2 --- app/assets/javascripts/ouvrages_block.js | 6 ++ app/assets/stylesheets/ouvrages_block.css | 4 -- app/assets/stylesheets/ouvrages_block.scss | 7 +++ app/views/admin/blocks/_blocks_form.html.slim | 10 ---- .../{admin => }/blocks/_block_form.html.slim | 0 app/views/blocks/_blocks_form.html.slim | 10 ++++ .../{admin => }/blocks/_buttons.html.slim | 0 lib/ouvrages_block.rb | 59 +------------------ lib/ouvrages_block/block.rb | 45 ++++++++++++++ lib/ouvrages_block/block_field.rb | 4 +- lib/ouvrages_block/engine.rb | 25 ++++++++ lib/ouvrages_block/version.rb | 2 +- ouvrages_block.gemspec | 5 +- 13 files changed, 101 insertions(+), 76 deletions(-) delete mode 100644 app/assets/stylesheets/ouvrages_block.css create mode 100644 app/assets/stylesheets/ouvrages_block.scss delete mode 100644 app/views/admin/blocks/_blocks_form.html.slim rename app/views/{admin => }/blocks/_block_form.html.slim (100%) create mode 100644 app/views/blocks/_blocks_form.html.slim rename app/views/{admin => }/blocks/_buttons.html.slim (100%) create mode 100644 lib/ouvrages_block/block.rb create mode 100644 lib/ouvrages_block/engine.rb diff --git a/app/assets/javascripts/ouvrages_block.js b/app/assets/javascripts/ouvrages_block.js index 963f6cf..de4292a 100644 --- a/app/assets/javascripts/ouvrages_block.js +++ b/app/assets/javascripts/ouvrages_block.js @@ -1,3 +1,9 @@ +//= require jquery +//= require bootstrap-sprockets +//= require sortable-rails-jquery +//= require tinymce-jquery +//= require jquery-fileupload + $(document).on("click", ".block-button", function(e) { e.preventDefault(); diff --git a/app/assets/stylesheets/ouvrages_block.css b/app/assets/stylesheets/ouvrages_block.css deleted file mode 100644 index 0245f01..0000000 --- a/app/assets/stylesheets/ouvrages_block.css +++ /dev/null @@ -1,4 +0,0 @@ -.block-deleted, -.sortable-ghost { - opacity: 0.6; -} diff --git a/app/assets/stylesheets/ouvrages_block.scss b/app/assets/stylesheets/ouvrages_block.scss new file mode 100644 index 0000000..fa5c369 --- /dev/null +++ b/app/assets/stylesheets/ouvrages_block.scss @@ -0,0 +1,7 @@ +@import 'bootstrap-sprockets'; +@import 'bootstrap'; + +.block-deleted, +.sortable-ghost { + opacity: 0.6; +} diff --git a/app/views/admin/blocks/_blocks_form.html.slim b/app/views/admin/blocks/_blocks_form.html.slim deleted file mode 100644 index 3187931..0000000 --- a/app/views/admin/blocks/_blocks_form.html.slim +++ /dev/null @@ -1,10 +0,0 @@ -h3= form.object.class.human_attribute_name(:blocks) - -.ouvrages-blocks - - buttons = render partial: "admin/blocks/buttons", locals: { id: "___ID___", form: form } - .block-forms(data={ buttons: { html: buttons }.to_json }) - - form.object.blocks.each do |block| - .block-form - = render partial: "admin/blocks/buttons", locals: { id: block.id, form: form } - .panel.panel-primary= form.block_form block - = render partial: "admin/blocks/buttons", locals: { id: "all", form: form } diff --git a/app/views/admin/blocks/_block_form.html.slim b/app/views/blocks/_block_form.html.slim similarity index 100% rename from app/views/admin/blocks/_block_form.html.slim rename to app/views/blocks/_block_form.html.slim diff --git a/app/views/blocks/_blocks_form.html.slim b/app/views/blocks/_blocks_form.html.slim new file mode 100644 index 0000000..f965e00 --- /dev/null +++ b/app/views/blocks/_blocks_form.html.slim @@ -0,0 +1,10 @@ +h3= form.object.class.human_attribute_name(:blocks) + +.ouvrages-blocks + - buttons = render partial: "blocks/buttons", locals: { id: "___ID___", form: form } + .block-forms(data={ buttons: { html: buttons }.to_json }) + - form.object.blocks.each do |block| + .block-form + = render partial: "blocks/buttons", locals: { id: block.id, form: form } + .panel.panel-primary= form.block_form block + = render partial: "blocks/buttons", locals: { id: "all", form: form } diff --git a/app/views/admin/blocks/_buttons.html.slim b/app/views/blocks/_buttons.html.slim similarity index 100% rename from app/views/admin/blocks/_buttons.html.slim rename to app/views/blocks/_buttons.html.slim diff --git a/lib/ouvrages_block.rb b/lib/ouvrages_block.rb index fb4d8fe..b4d9873 100644 --- a/lib/ouvrages_block.rb +++ b/lib/ouvrages_block.rb @@ -1,62 +1,5 @@ -require 'active_record' -require "bootstrap_form/form_builder" - require "ouvrages_block/version" -require "ouvrages_block/block_field" module OuvragesBlock - class Engine < ::Rails::Engine - end - - extend ActiveSupport::Concern - class_methods do - def has_blocks(block_names) - if block_names.is_a?(Hash) - names = block_names.values.flatten - else - names = block_names - end - - names.each do |block_name| - has_many block_name, dependent: :destroy, as: :parent, inverse_of: :parent - accepts_nested_attributes_for block_name, allow_destroy: true - end - - define_singleton_method "block_permitted_attributes" do - names.map do |block_name| - { "#{block_name}_attributes" => block_name.to_s.singularize.classify.constantize.permitted_attributes } - end - end - - define_method "blocks" do - names.map do |block_name| - send(block_name) - end.flatten.sort_by { |block| block.position || 0 } - end - - define_method "block_names" do - names - end - - define_method "block_buttons" do - if block_names.is_a?(Hash) - block_names - else - names - end - end - end - end -end - -ActiveRecord::Base.send(:include, OuvragesBlock) - -module BootstrapFormBlockField - include BlockField -end - -module BootstrapForm - class FormBuilder - include BootstrapFormBlockField - end + require "ouvrages_block/engine" if defined?(Rails) end diff --git a/lib/ouvrages_block/block.rb b/lib/ouvrages_block/block.rb new file mode 100644 index 0000000..d509492 --- /dev/null +++ b/lib/ouvrages_block/block.rb @@ -0,0 +1,45 @@ +module OuvragesBlock + module Block + extend ActiveSupport::Concern + class_methods do + def has_blocks(block_names) + if block_names.is_a?(Hash) + names = block_names.values.flatten + else + names = block_names + end + + names.each do |block_name| + has_many block_name, dependent: :destroy, as: :parent, inverse_of: :parent + accepts_nested_attributes_for block_name, allow_destroy: true + end + + define_singleton_method "block_permitted_attributes" do + names.map do |block_name| + { "#{block_name}_attributes" => block_name.to_s.singularize.classify.constantize.permitted_attributes } + end + end + + define_method "blocks" do + names.map do |block_name| + send(block_name) + end.flatten.sort_by { |block| block.position || 0 } + end + + define_method "block_names" do + names + end + + define_method "block_buttons" do + if block_names.is_a?(Hash) + block_names + else + names + end + end + end + end + end +end + + diff --git a/lib/ouvrages_block/block_field.rb b/lib/ouvrages_block/block_field.rb index a778479..a6e0ac2 100644 --- a/lib/ouvrages_block/block_field.rb +++ b/lib/ouvrages_block/block_field.rb @@ -1,6 +1,6 @@ module BlockField def blocks_form(method = nil) - @template.render partial: "admin/blocks/blocks_form", locals: { form: self } + @template.render partial: "blocks/blocks_form", locals: { form: self } end def block_form(block, options = {}) @@ -12,6 +12,6 @@ def block_form(block, options = {}) index = nil end - @template.render partial: "admin/blocks/block_form", locals: { form: self, block: block, index: index } + @template.render partial: "blocks/block_form", locals: { form: self, block: block, index: index } end end diff --git a/lib/ouvrages_block/engine.rb b/lib/ouvrages_block/engine.rb new file mode 100644 index 0000000..fa3baff --- /dev/null +++ b/lib/ouvrages_block/engine.rb @@ -0,0 +1,25 @@ +require "sass-rails" +require "jquery-rails" +require "bootstrap-sass" +require "sortable/rails" +require "tinymce-rails" +require "tinymce-rails-langs" +require "jquery-fileupload-rails" + +module OuvragesBlock + class Engine < Rails::Engine + ActiveSupport.on_load(:active_record) do + require 'ouvrages_block/block' + include OuvragesBlock::Block + end + end +end + +require "bootstrap_form/form_builder" +require "ouvrages_block/block_field" + +module BootstrapForm + class FormBuilder + include BlockField + end +end diff --git a/lib/ouvrages_block/version.rb b/lib/ouvrages_block/version.rb index e5928ef..50cc3bd 100644 --- a/lib/ouvrages_block/version.rb +++ b/lib/ouvrages_block/version.rb @@ -1,3 +1,3 @@ module OuvragesBlock - VERSION = "0.1.1" + VERSION = "2.0.0" end diff --git a/ouvrages_block.gemspec b/ouvrages_block.gemspec index 337aecf..78eaa03 100644 --- a/ouvrages_block.gemspec +++ b/ouvrages_block.gemspec @@ -31,9 +31,12 @@ Gem::Specification.new do |spec| spec.add_development_dependency "rake", "~> 10.0" spec.add_runtime_dependency "activerecord", ">= 4.0" + spec.add_runtime_dependency "sass-rails" + spec.add_runtime_dependency "bootstrap-sass" spec.add_runtime_dependency "bootstrap_form", '>= 2.3.0' - + spec.add_runtime_dependency "jquery-rails" spec.add_runtime_dependency "sortable-rails" spec.add_runtime_dependency "tinymce-rails" + spec.add_runtime_dependency "tinymce-rails-langs" spec.add_runtime_dependency "jquery-fileupload-rails" end From dfec626b2f1353248880a0313938a2a7a548055d Mon Sep 17 00:00:00 2001 From: Florent Ferry Date: Mon, 11 Dec 2017 21:20:41 +0100 Subject: [PATCH 10/17] Require missing dependency --- lib/ouvrages_block/engine.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/ouvrages_block/engine.rb b/lib/ouvrages_block/engine.rb index fa3baff..c9eab39 100644 --- a/lib/ouvrages_block/engine.rb +++ b/lib/ouvrages_block/engine.rb @@ -15,6 +15,7 @@ class Engine < Rails::Engine end end +require "bootstrap_form" require "bootstrap_form/form_builder" require "ouvrages_block/block_field" From 401ed88b47b78c3b8278affb29e9e30f2af60d5e Mon Sep 17 00:00:00 2001 From: Florent Ferry Date: Mon, 11 Dec 2017 21:29:58 +0100 Subject: [PATCH 11/17] Add documentation --- README.md | 44 +++++++++++++++++++++++++++++++++----------- 1 file changed, 33 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index b5bc082..cfb5a96 100644 --- a/README.md +++ b/README.md @@ -2,19 +2,46 @@ ## Installation -Add this line to your application's Gemfile: - ```ruby gem 'ouvrages_block' ``` -And then execute: +Append your `application.js` with: + +```js +# app/assets/javascripts/application.js - $ bundle +//= require ouvrages_block +``` -Or install it yourself as: +And import css too. - $ gem install ouvrages_block +```css +# app/assets/stylesheets.scss + +@import 'ouvrages_block'; +``` + +In model you want use `block`: + +```rb +# model.rb + +has_blocks([:block]) +``` + +In form: + +```slim += bootstrap_form_for ... do |f| + = f.blocks_form +``` + +You can generate new block with: + +```sh +rails generate block Name +``` ## Standard blocks @@ -23,8 +50,3 @@ Or install it yourself as: - standard_block_rich_text - standard_block_medium_collection -## Development - -## Contributing - -## License From 2285f0316aa971734f7fec2990fe65348ea7a8a9 Mon Sep 17 00:00:00 2001 From: Vincent Chabredier Date: Tue, 12 Dec 2017 17:52:36 +0100 Subject: [PATCH 12/17] some style + toggle target fixed --- app/assets/javascripts/ouvrages_block.js | 11 +++++++++++ app/assets/stylesheets/ouvrages_block.scss | 23 ++++++++++++++++++++++ app/views/blocks/_blocks_form.html.slim | 4 ++-- app/views/blocks/_buttons.html.slim | 7 ++++--- 4 files changed, 40 insertions(+), 5 deletions(-) diff --git a/app/assets/javascripts/ouvrages_block.js b/app/assets/javascripts/ouvrages_block.js index de4292a..161b448 100644 --- a/app/assets/javascripts/ouvrages_block.js +++ b/app/assets/javascripts/ouvrages_block.js @@ -14,6 +14,8 @@ $(document).on("click", ".block-button", function(e) { var $newBlock = generateBlockForm($button, buttonsHTML); + $(this).closest(".collapse").collapse('hide'); + if ($buttonWrapper.length === 0) { $blockForms.append($newBlock); } else { @@ -132,3 +134,12 @@ $(document).on("click", ".collapse-block-form-button", function(e) { $blockFormIcon.addClass('glyphicon-minus'); } }); + +$(document).on("show.bs.collapse", ".block-buttons", function(e) { + $(e.currentTarget).find(".blocks-toggle i").removeClass("glyphicon-plus").addClass("glyphicon-chevron-up"); +}); + +$(document).on("hide.bs.collapse", ".block-buttons", function(e) { + $(e.currentTarget).find(".blocks-toggle i").removeClass("glyphicon-chevron-up").addClass("glyphicon-plus"); +}); + diff --git a/app/assets/stylesheets/ouvrages_block.scss b/app/assets/stylesheets/ouvrages_block.scss index fa5c369..15be8ba 100644 --- a/app/assets/stylesheets/ouvrages_block.scss +++ b/app/assets/stylesheets/ouvrages_block.scss @@ -5,3 +5,26 @@ .sortable-ghost { opacity: 0.6; } + +.btn-circle { + width: 30px; + height: 30px; + text-align: center; + padding: 6px 0; + font-size: 12px; + line-height: 30px; + border-radius: 15px; +} + +.blocks-toggle { + margin-bottom: 10px; +} + +.block-buttons { + padding-bottom: 10px; +} + +.block-panel { + margin-bottom: 10px; +} + diff --git a/app/views/blocks/_blocks_form.html.slim b/app/views/blocks/_blocks_form.html.slim index f965e00..b1076fd 100644 --- a/app/views/blocks/_blocks_form.html.slim +++ b/app/views/blocks/_blocks_form.html.slim @@ -5,6 +5,6 @@ h3= form.object.class.human_attribute_name(:blocks) .block-forms(data={ buttons: { html: buttons }.to_json }) - form.object.blocks.each do |block| .block-form - = render partial: "blocks/buttons", locals: { id: block.id, form: form } - .panel.panel-primary= form.block_form block + = render partial: "blocks/buttons", locals: { id: "#{block.class.name.parameterize}-#{block.id}", form: form } + .panel.panel-primary.block-panel= form.block_form block = render partial: "blocks/buttons", locals: { id: "all", form: form } diff --git a/app/views/blocks/_buttons.html.slim b/app/views/blocks/_buttons.html.slim index 5ee1bb4..7520ddc 100644 --- a/app/views/blocks/_buttons.html.slim +++ b/app/views/blocks/_buttons.html.slim @@ -1,8 +1,9 @@ -.block-buttons(data={ target: id }) - button(type="button" data={ toggle: "collapse", target: "#buttons-#{id}" }) Ajouter un bloc +.block-buttons.text-center(data={ target: id }) + button.blocks-toggle.btn.btn-default.btn-circle(type="button" data={ toggle: "collapse", target: "#buttons-#{id}" } title="Ajouter un block") + i.glyphicon.glyphicon-plus .collapse(id="buttons-#{id}") - form.object.block_buttons.each do |block_name| - new_block = form.block_form block_name.to_s.singularize.classify.constantize.new, template: true - new_block = new_block.html_safe - button(data={ form: { html: new_block }.to_json, id: id } type="button" class="block-button") + button.btn.btn-default(data={ form: { html: new_block }.to_json, id: id } type="button" class="block-button") = block_name.to_s.classify.constantize.model_name.human From c6c71bb0866179efae6b58f452180003c1588091 Mon Sep 17 00:00:00 2001 From: Florent Ferry Date: Tue, 12 Dec 2017 20:03:31 +0100 Subject: [PATCH 13/17] Fix model class inheritance for Rails > 5 --- lib/generators/block/block_generator.rb | 8 ++++++++ lib/generators/block/templates/models/model.rb.erb | 2 +- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/lib/generators/block/block_generator.rb b/lib/generators/block/block_generator.rb index b28dcac..bb7a485 100644 --- a/lib/generators/block/block_generator.rb +++ b/lib/generators/block/block_generator.rb @@ -68,4 +68,12 @@ def migration_class_name 'ActiveRecord::Migration' end end + + def model_class_name + if Rails::VERSION::MAJOR >= 5 + "ApplicationRecord" + else + "ActiveRecord::Base" + end + end end diff --git a/lib/generators/block/templates/models/model.rb.erb b/lib/generators/block/templates/models/model.rb.erb index 49fe067..f7cafa6 100644 --- a/lib/generators/block/templates/models/model.rb.erb +++ b/lib/generators/block/templates/models/model.rb.erb @@ -1,4 +1,4 @@ -class <%= standard_block_class_name %> < ApplicationRecord +class <%= standard_block_class_name %> < <%= model_class_name %> belongs_to :parent, polymorphic: true def self.permitted_attributes From c1bca8bd6a8d2a7c512b438d7013105230e35fb1 Mon Sep 17 00:00:00 2001 From: Florent FERRY Date: Thu, 14 Dec 2017 16:59:27 +0100 Subject: [PATCH 14/17] Convert rich text standard block to slim --- .../templates/views/{admin.html.haml => admin.html.slim} | 0 .../templates/views/{show.html.haml => show.html.slim} | 0 2 files changed, 0 insertions(+), 0 deletions(-) rename lib/generators/standard_block_rich_text/templates/views/{admin.html.haml => admin.html.slim} (100%) rename lib/generators/standard_block_rich_text/templates/views/{show.html.haml => show.html.slim} (100%) diff --git a/lib/generators/standard_block_rich_text/templates/views/admin.html.haml b/lib/generators/standard_block_rich_text/templates/views/admin.html.slim similarity index 100% rename from lib/generators/standard_block_rich_text/templates/views/admin.html.haml rename to lib/generators/standard_block_rich_text/templates/views/admin.html.slim diff --git a/lib/generators/standard_block_rich_text/templates/views/show.html.haml b/lib/generators/standard_block_rich_text/templates/views/show.html.slim similarity index 100% rename from lib/generators/standard_block_rich_text/templates/views/show.html.haml rename to lib/generators/standard_block_rich_text/templates/views/show.html.slim From 447fd519afcd61c39cdb8f0dcd5be11e47e75f51 Mon Sep 17 00:00:00 2001 From: Florent FERRY Date: Thu, 14 Dec 2017 17:04:44 +0100 Subject: [PATCH 15/17] Adapt standard block class name --- .../standard_block_rich_text_generator.rb | 24 +++++++++++++++++++ .../templates/migrations/migration.rb | 6 ++++- .../templates/models/rich_text.rb.erb | 2 +- 3 files changed, 30 insertions(+), 2 deletions(-) diff --git a/lib/generators/standard_block_rich_text/standard_block_rich_text_generator.rb b/lib/generators/standard_block_rich_text/standard_block_rich_text_generator.rb index 85fd510..b2025e2 100644 --- a/lib/generators/standard_block_rich_text/standard_block_rich_text_generator.rb +++ b/lib/generators/standard_block_rich_text/standard_block_rich_text_generator.rb @@ -51,4 +51,28 @@ def standard_block_name_singular def standard_block_name_plural "rich_texts" end + + def standard_block_class_name + standard_block_name_singular.camelize + end + + def standard_block_migration_name + standard_block_name_plural.camelize + end + + def migration_class_name + if Rails::VERSION::MAJOR >= 5 + "ActiveRecord::Migration[#{Rails::VERSION::MAJOR}.#{Rails::VERSION::MINOR}]" + else + 'ActiveRecord::Migration' + end + end + + def model_class_name + if Rails::VERSION::MAJOR >= 5 + "ApplicationRecord" + else + "ActiveRecord::Base" + end + end end diff --git a/lib/generators/standard_block_rich_text/templates/migrations/migration.rb b/lib/generators/standard_block_rich_text/templates/migrations/migration.rb index 970bd2f..146f3e0 100644 --- a/lib/generators/standard_block_rich_text/templates/migrations/migration.rb +++ b/lib/generators/standard_block_rich_text/templates/migrations/migration.rb @@ -1,4 +1,4 @@ -class CreateRichTexts < ActiveRecord::Migration[<%= ActiveRecord::Migration.current_version %>] +class Create<%= standard_block_migration_name %> < <%= migration_class_name %> def up create_table :rich_texts do |t| t.references :parent, polymorphic: true @@ -8,4 +8,8 @@ def up t.timestamps end end + + def down + drop_table :<%= standard_block_name_plural %> + end end diff --git a/lib/generators/standard_block_rich_text/templates/models/rich_text.rb.erb b/lib/generators/standard_block_rich_text/templates/models/rich_text.rb.erb index 3f4a58e..4e0486f 100644 --- a/lib/generators/standard_block_rich_text/templates/models/rich_text.rb.erb +++ b/lib/generators/standard_block_rich_text/templates/models/rich_text.rb.erb @@ -1,4 +1,4 @@ -class RichText < ApplicationRecord +class <%= standard_block_class_name %> < <%= model_class_name %> belongs_to :parent, polymorphic: true def self.permitted_attributes From 8661e8342c441ba9c58a2c07b195e5c2e855b02f Mon Sep 17 00:00:00 2001 From: Florent FERRY Date: Thu, 14 Dec 2017 17:08:34 +0100 Subject: [PATCH 16/17] Change view extension for rich text standard block --- .../standard_block_rich_text_generator.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/generators/standard_block_rich_text/standard_block_rich_text_generator.rb b/lib/generators/standard_block_rich_text/standard_block_rich_text_generator.rb index b2025e2..7d9c0e6 100644 --- a/lib/generators/standard_block_rich_text/standard_block_rich_text_generator.rb +++ b/lib/generators/standard_block_rich_text/standard_block_rich_text_generator.rb @@ -30,8 +30,8 @@ def create_locale_files end def create_view_files - template "views/admin.html.haml", File.join("app", "views", "admin", standard_block_name_plural, "_block_form.html.haml") - template "views/show.html.haml", File.join("app", "views", standard_block_name_plural, "_#{standard_block_name_singular}.html.haml") + template "views/admin.html.slim", File.join("app", "views", "admin", standard_block_name_plural, "_block_form.html.slim") + template "views/show.html.slim", File.join("app", "views", standard_block_name_plural, "_#{standard_block_name_singular}.html.slim") end private From da0cdf556252c17929c90ffe8ecc55c929bfab17 Mon Sep 17 00:00:00 2001 From: Florent FERRY Date: Fri, 15 Dec 2017 13:03:12 +0100 Subject: [PATCH 17/17] Enable address picker --- app/assets/javascripts/ouvrages_block.js | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/app/assets/javascripts/ouvrages_block.js b/app/assets/javascripts/ouvrages_block.js index 161b448..bd85fdc 100644 --- a/app/assets/javascripts/ouvrages_block.js +++ b/app/assets/javascripts/ouvrages_block.js @@ -85,6 +85,9 @@ $.fn.initRichTextareas = function() { $(document).on("blocks:add", function(e, block) { $(block).initRichTextareas(); + $(block).find(".address_picker").each(function(index, element) { + $(element).addressPickerField(); + }); createSortable(); }); @@ -97,6 +100,9 @@ $(document).on("blocks:move", function(e, block) { }); $(document).on('turbolinks:before-cache', function() { + $(".address_picker").each(function(index, element) { + $(this).find(".address").typeahead("destroy"); + }); tinyMCE.remove(); });