Skip to content
This repository has been archived by the owner on Jul 19, 2021. It is now read-only.

Commit

Permalink
Setup repeater. This configuration is not backwards compatible
Browse files Browse the repository at this point in the history
  • Loading branch information
a-barbieri committed May 5, 2017
1 parent 82f98a5 commit 5f75b0b
Show file tree
Hide file tree
Showing 24 changed files with 291 additions and 158 deletions.
58 changes: 31 additions & 27 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ PATH
specs:
binda (0.1.0)
aasm (~> 4.11)
ancestry (~> 2.1.0)
carrierwave (~> 1.0)
coffee-rails (~> 4.2.1)
colorize (~> 0.8.1)
Expand Down Expand Up @@ -60,21 +61,23 @@ GEM
i18n (~> 0.7)
minitest (~> 5.1)
tzinfo (~> 1.1)
addressable (2.5.0)
addressable (2.5.1)
public_suffix (~> 2.0, >= 2.0.2)
ancestry (2.1.0)
activerecord (>= 3.0.0)
arel (7.1.4)
autoprefixer-rails (6.5.3.1)
autoprefixer-rails (6.7.7.2)
execjs
bcrypt (3.1.11)
builder (3.2.3)
capybara (2.8.1)
capybara (2.14.0)
addressable
mime-types (>= 1.16)
nokogiri (>= 1.3.3)
rack (>= 1.0.0)
rack-test (>= 0.5.4)
xpath (~> 2.0)
carrierwave (1.0.0)
carrierwave (1.1.0)
activemodel (>= 4.0.0)
activesupport (>= 4.0.0)
mime-types (>= 1.16)
Expand Down Expand Up @@ -105,8 +108,8 @@ GEM
ffaker (2.5.0)
friendly_id (5.1.0)
activerecord (>= 4.0.0)
globalid (0.3.7)
activesupport (>= 4.1.0)
globalid (0.4.0)
activesupport (>= 4.2.0)
i18n (0.8.1)
jquery-rails (4.3.1)
rails-dom-testing (>= 1, < 3)
Expand All @@ -116,7 +119,7 @@ GEM
railties (>= 3.2.16)
loofah (2.0.3)
nokogiri (>= 1.5.9)
mail (2.6.4)
mail (2.6.5)
mime-types (>= 1.16, < 4)
method_source (0.8.2)
mime-types (3.1)
Expand All @@ -126,17 +129,17 @@ GEM
mini_portile2 (2.1.0)
minitest (5.10.1)
nio4r (2.0.0)
nokogiri (1.7.0.1)
nokogiri (1.7.1)
mini_portile2 (~> 2.1.0)
orm_adapter (0.5.0)
pg (0.20.0)
pry (0.10.4)
coderay (~> 1.1.0)
method_source (~> 0.8.1)
slop (~> 3.4)
pry-rails (0.3.5)
pry (>= 0.9.10)
public_suffix (2.0.4)
pry-rails (0.3.6)
pry (>= 0.10.4)
public_suffix (2.0.5)
rack (2.0.1)
rack-test (0.6.3)
rack (>= 1.0)
Expand Down Expand Up @@ -164,25 +167,26 @@ GEM
rake (>= 0.8.7)
thor (>= 0.18.1, < 2.0)
rake (12.0.0)
responders (2.3.0)
railties (>= 4.2.0, < 5.1)
rspec-core (3.5.4)
rspec-support (~> 3.5.0)
rspec-expectations (3.5.0)
responders (2.4.0)
actionpack (>= 4.2.0, < 5.3)
railties (>= 4.2.0, < 5.3)
rspec-core (3.6.0)
rspec-support (~> 3.6.0)
rspec-expectations (3.6.0)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.5.0)
rspec-mocks (3.5.0)
rspec-support (~> 3.6.0)
rspec-mocks (3.6.0)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.5.0)
rspec-rails (3.5.2)
rspec-support (~> 3.6.0)
rspec-rails (3.6.0)
actionpack (>= 3.0)
activesupport (>= 3.0)
railties (>= 3.0)
rspec-core (~> 3.5.0)
rspec-expectations (~> 3.5.0)
rspec-mocks (~> 3.5.0)
rspec-support (~> 3.5.0)
rspec-support (3.5.0)
rspec-core (~> 3.6.0)
rspec-expectations (~> 3.6.0)
rspec-mocks (~> 3.6.0)
rspec-support (~> 3.6.0)
rspec-support (3.6.0)
sass (3.4.23)
sass-rails (5.0.6)
railties (>= 4.0.0, < 6)
Expand All @@ -204,11 +208,11 @@ GEM
thor (0.19.4)
thread_safe (0.3.6)
tilt (2.0.7)
tinymce-rails (4.5.5)
tinymce-rails (4.5.7)
railties (>= 3.1.1)
tinymce-rails-langs (4.20160310)
tinymce-rails (~> 4.1, >= 4.1.10)
tzinfo (1.2.2)
tzinfo (1.2.3)
thread_safe (~> 0.1)
warden (1.2.7)
rack (>= 1.0)
Expand Down
28 changes: 16 additions & 12 deletions app/assets/javascripts/binda/components/form_item.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,8 @@ class FormItem {

setEvents()
{
$(document).on('click', this.target + '--add-new', function( event )
{
// Stop default behaviour
event.preventDefault()
// Get the child to clone
let id = $( event.target ).data( 'new-child-id' )
let $newChild = $( '#' + id )
// Clone child and remove id and styles from cloned child
$newChild.clone().insertAfter( $newChild )
$newChild.removeClass( 'form-item--new' ).removeAttr( 'id' )
})
$(document).on('click', this.target + '--add-new', addNewItem )
$(document).on('click', this.target + '-repeater--add-new', addNewItem )

$(document).on('click', '.form-item--remove-item-with-js', function( event )
{
Expand All @@ -38,4 +29,17 @@ class FormItem {
}
}

export let _FormItem = new FormItem()
export let _FormItem = new FormItem()


function addNewItem( event )
{
// Stop default behaviour
event.preventDefault()
// Get the child to clone
let id = $( event.target ).data( 'new-child-id' )
let $newChild = $( '#' + id )
// Clone child and remove id and styles from cloned child
$newChild.clone().insertAfter( $newChild )
$newChild.removeClass( 'form-item--new' ).removeAttr( 'id' )
}
64 changes: 33 additions & 31 deletions app/assets/javascripts/binda/dist/binda.bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,27 +67,7 @@
/******/ })
/************************************************************************/
/******/ ([
/* 0 */,
/* 1 */
/***/ (function(module, __webpack_exports__, __webpack_require__) {

"use strict";
Object.defineProperty(__webpack_exports__, "__esModule", { value: true });
/* harmony import */ var __WEBPACK_IMPORTED_MODULE_0__components_form_item__ = __webpack_require__(2);
///- - - - - - - - - - - - - - - - - - - -
/// INDEX OF BINDA'S SCRIPTS
///- - - - - - - - - - - - - - - - - - - -



$(document).ready(function () {
if (__WEBPACK_IMPORTED_MODULE_0__components_form_item__["a" /* _FormItem */].isSet()) {
__WEBPACK_IMPORTED_MODULE_0__components_form_item__["a" /* _FormItem */].setEvents();
}
});

/***/ }),
/* 2 */
/* 0 */
/***/ (function(module, __webpack_exports__, __webpack_require__) {

"use strict";
Expand Down Expand Up @@ -119,16 +99,8 @@ var FormItem = function () {
}, {
key: 'setEvents',
value: function setEvents() {
$(document).on('click', this.target + '--add-new', function (event) {
// Stop default behaviour
event.preventDefault();
// Get the child to clone
var id = $(event.target).data('new-child-id');
var $newChild = $('#' + id);
// Clone child and remove id and styles from cloned child
$newChild.clone().insertAfter($newChild);
$newChild.removeClass('form-item--new').removeAttr('id');
});
$(document).on('click', this.target + '--add-new', addNewItem);
$(document).on('click', this.target + '-repeater--add-new', addNewItem);

$(document).on('click', '.form-item--remove-item-with-js', function (event) {
// Stop default behaviour
Expand All @@ -143,5 +115,35 @@ var FormItem = function () {

var _FormItem = new FormItem();

function addNewItem(event) {
// Stop default behaviour
event.preventDefault();
// Get the child to clone
var id = $(event.target).data('new-child-id');
var $newChild = $('#' + id);
// Clone child and remove id and styles from cloned child
$newChild.clone().insertAfter($newChild);
$newChild.removeClass('form-item--new').removeAttr('id');
}

/***/ }),
/* 1 */
/***/ (function(module, __webpack_exports__, __webpack_require__) {

"use strict";
Object.defineProperty(__webpack_exports__, "__esModule", { value: true });
/* harmony import */ var __WEBPACK_IMPORTED_MODULE_0__components_form_item__ = __webpack_require__(0);
///- - - - - - - - - - - - - - - - - - - -
/// INDEX OF BINDA'S SCRIPTS
///- - - - - - - - - - - - - - - - - - - -



$(document).ready(function () {
if (__WEBPACK_IMPORTED_MODULE_0__components_form_item__["a" /* _FormItem */].isSet()) {
__WEBPACK_IMPORTED_MODULE_0__components_form_item__["a" /* _FormItem */].setEvents();
}
});

/***/ })
/******/ ]);
24 changes: 19 additions & 5 deletions app/controllers/binda/field_groups_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ def update
end
end
end

# Update the other ones
if @field_group.update(field_group_params)
redirect_to structure_field_group_path( @structure.slug, @field_group.slug ), notice: 'Field group was successfully updated.'
Expand Down Expand Up @@ -74,19 +75,32 @@ def field_group_params
:structure_id,
field_settings_attributes: [
:id,
:slug,
:field_group_id,
:field_group_id,
:field_setting_id,
:name,
:slug,
:description,
:field_type,
:position,
:required,
:default_text
])
:default_text,
:ancestry
])
end

def new_params
params.require(:field_group).permit( new_field_settings:[ :name, :field_group_id, :field_type, :description, :position ] )
params.require(:field_group).permit(
new_field_settings:[
:field_group_id,
:field_setting_id,
:name,
:slug,
:description,
:field_type,
:position,
:required,
:ancestry
])
end
end
end
2 changes: 1 addition & 1 deletion app/models/binda/field_group.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class FieldGroup < ApplicationRecord
validates :name, presence: true
validates :slug, uniqueness: true
accepts_nested_attributes_for :field_settings, allow_destroy: true, reject_if: :is_rejected

# Slug
extend FriendlyId
friendly_id :default_slug, use: [:slugged, :finders]
Expand Down
38 changes: 27 additions & 11 deletions app/models/binda/field_setting.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,32 @@ class FieldSetting < ApplicationRecord

# Associations
belongs_to :field_group
has_many :field_children, class_name: 'Binda::FieldSetting'
# has_many :field_children, class_name: ::Binda::FieldSetting, dependent: :delete_all
has_ancestry orphan_strategy: :destroy

# Fields Associations
# Fields Associations
# -------------------
# If you add a new field remember to update:
# - get_fieldables (see here below)
# - get_field_types (see here below)
# - component_params (app/controllers/binda/components_controller.rb)
has_many :texts, as: :fieldable
has_many :dates, as: :fieldable
has_many :repeaters, as: :fieldable
has_many :galleries, as: :fieldable
has_many :assets, as: :fieldable
has_many :assets, as: :fieldable

# The following direct association is used to securely delete associated fields
# Infact via `fieldable` the associated fields might not be deleted
# as the fieldable_id is related to the `component` rather than the `field_setting`
has_many :texts, dependent: :delete_all
has_many :dates, dependent: :delete_all
has_many :repeaters, dependent: :delete_all
has_many :galleries, dependent: :delete_all

# accepts_nested_attributes_for :children, allow_destroy: true, reject_if: :is_rejected


def self.get_fieldables
%w( Text Date Repeater Gallery Asset )
%w( Text Date Gallery Asset )
end

# Field types are't fieldable! watch out! They might use the same model (eg `string` and `text`)
Expand All @@ -36,7 +38,7 @@ def get_field_types

# Validations
validates :name, presence: true
validates :field_type, presence: true, inclusion: { in: :get_field_types }
# validates :field_type, presence: true, inclusion: { in: :get_field_types }
validates :field_group_id, presence: true

# Slug
Expand All @@ -52,10 +54,24 @@ def should_generate_new_friendly_id?
end

def default_slug
[ "#{ self.field_group.structure.name }-#{ self.field_group.name }-#{ self.name }",
"#{ self.field_group.structure.name }-#{ self.field_group.name }-#{ self.name }-1",
"#{ self.field_group.structure.name }-#{ self.field_group.name }-#{ self.name }-2",
"#{ self.field_group.structure.name }-#{ self.field_group.name }-#{ self.name }-3" ]
breadcrumb = self.field_group.structure.name

breadcrumb << '-'
breadcrumb << self.field_group.name

unless self.parent.nil?
breadcrumb << '-'
breadcrumb << self.parent.name
end

possible_names = [
"#{ breadcrumb }--#{ self.name }",
"#{ breadcrumb }--#{ self.name }-1",
"#{ breadcrumb }--#{ self.name }-2",
"#{ breadcrumb }--#{ self.name }-3"
]

return possible_names
end

# This will be use to overcome cascade delete on fieldable polymorphic association
Expand Down
Loading

0 comments on commit 5f75b0b

Please sign in to comment.