binda 0.0.6 → 0.0.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +4 -2
- data/Rakefile +2 -1
- data/app/assets/javascripts/binda/dist/binda.bundle.js +75 -75
- data/app/assets/stylesheets/binda/settings/common.scss +4 -0
- data/app/controllers/binda/application_controller.rb +1 -1
- data/app/controllers/binda/boards_controller.rb +73 -0
- data/app/controllers/binda/components_controller.rb +18 -44
- data/app/controllers/binda/field_groups_controller.rb +36 -24
- data/app/controllers/binda/field_settings_controller.rb +5 -5
- data/app/controllers/binda/manage/users_controller.rb +2 -9
- data/app/controllers/binda/structures_controller.rb +6 -13
- data/app/controllers/concerns/binda/default_helpers.rb +226 -0
- data/app/controllers/concerns/binda/fieldable_helpers.rb +44 -0
- data/app/helpers/binda/boards_helper.rb +13 -0
- data/app/helpers/binda/components_helper.rb +0 -5
- data/app/helpers/binda/field_settings_helper.rb +1 -1
- data/app/models/binda/asset.rb +0 -8
- data/app/models/binda/binda.rb +7 -0
- data/app/models/binda/binding.rb +4 -4
- data/app/models/binda/board.rb +41 -0
- data/app/models/binda/category.rb +7 -3
- data/app/models/binda/checkbox.rb +1 -3
- data/app/models/binda/choice.rb +31 -1
- data/app/models/binda/component.rb +7 -22
- data/app/models/binda/field_group.rb +13 -7
- data/app/models/binda/field_setting.rb +40 -29
- data/app/models/binda/gallery.rb +0 -7
- data/app/models/binda/radio.rb +1 -1
- data/app/models/binda/repeater.rb +13 -20
- data/app/models/binda/selection.rb +17 -0
- data/app/models/binda/string.rb +4 -0
- data/app/models/binda/structure.rb +65 -7
- data/app/models/concerns/binda/{component_model_helper.rb → fieldable_associations.rb} +102 -31
- data/app/views/binda/boards/edit.html.erb +7 -0
- data/app/views/binda/components/edit.html.erb +4 -1
- data/app/views/binda/components/index.html.erb +2 -0
- data/app/views/binda/components/new.html.erb +1 -1
- data/app/views/binda/field_groups/_form_body.html.erb +8 -8
- data/app/views/binda/field_groups/_form_item.html.erb +3 -3
- data/app/views/binda/field_groups/_form_item_choice.erb +11 -8
- data/app/views/binda/field_groups/_form_section.html.erb +5 -5
- data/app/views/binda/field_groups/_form_section_repeater.html.erb +3 -3
- data/app/views/binda/field_groups/edit.html.erb +1 -1
- data/app/views/binda/field_groups/new.html.erb +1 -1
- data/app/views/binda/{components → fieldable}/_form_body.html.erb +9 -9
- data/app/views/binda/{components → fieldable}/_form_item_asset.html.erb +0 -0
- data/app/views/binda/{components → fieldable}/_form_item_date.html.erb +0 -0
- data/app/views/binda/{components → fieldable}/_form_item_gallery.html.erb +0 -0
- data/app/views/binda/{components → fieldable}/_form_item_new_repeater.html.erb +5 -5
- data/app/views/binda/{components → fieldable}/_form_item_repeater.html.erb +8 -9
- data/app/views/binda/fieldable/_form_item_selections.html.erb +99 -0
- data/app/views/binda/fieldable/_form_item_string.html.erb +19 -0
- data/app/views/binda/{components → fieldable}/_form_item_text.html.erb +2 -11
- data/app/views/binda/fieldable/_form_section.html.erb +52 -0
- data/app/views/binda/fieldable/_form_section_repeater.html.erb +48 -0
- data/app/views/binda/fieldable/_form_sidebar.html.erb +35 -0
- data/app/views/binda/structures/_form_body.html.erb +6 -0
- data/app/views/binda/structures/_form_section.html.erb +1 -1
- data/app/views/binda/structures/edit.html.erb +3 -0
- data/app/views/binda/structures/index.html.erb +2 -0
- data/app/views/layouts/binda/_flash.html.erb +3 -3
- data/app/views/layouts/binda/_sidebar.html.erb +20 -15
- data/config/locales/en.yml +4 -0
- data/config/routes.rb +18 -21
- data/db/migrate/1_create_binda_tables.rb +12 -6
- data/lib/binda/engine.rb +5 -1
- data/lib/binda/version.rb +1 -1
- data/lib/generators/binda/install/install_generator.rb +9 -0
- data/lib/generators/binda/setup/setup_generator.rb +50 -7
- data/lib/tasks/binda.rake +78 -24
- metadata +100 -31
- data/app/controllers/binda/settings_controller.rb +0 -75
- data/app/controllers/concerns/binda/component_controller_helper.rb +0 -16
- data/app/helpers/binda/settings_helper.rb +0 -13
- data/app/models/binda/select.rb +0 -7
- data/app/models/binda/setting.rb +0 -42
- data/app/views/binda/components/_form_item_selectable.html.erb +0 -72
- data/app/views/binda/components/_form_section.html.erb +0 -42
- data/app/views/binda/components/_form_section_repeater.html.erb +0 -41
- data/app/views/binda/components/_form_sidebar.html.erb +0 -34
- data/app/views/binda/settings/_dashboard_form.html.erb +0 -51
- data/app/views/binda/settings/_form.html.erb +0 -52
- data/app/views/binda/settings/dashboard.html.erb +0 -9
- data/app/views/binda/settings/edit.html.erb +0 -8
- data/app/views/binda/settings/index.html.erb +0 -34
- data/app/views/binda/settings/new.html.erb +0 -8
@@ -32,27 +32,10 @@ module Binda
|
|
32
32
|
end
|
33
33
|
|
34
34
|
def update
|
35
|
-
# Create new fields if any
|
36
|
-
new_params[:new_field_settings].each do |field_setting|
|
37
|
-
unless field_setting[:name].blank?
|
38
|
-
new_field_setting = @field_group.field_settings.create( field_setting )
|
39
|
-
unless new_field_setting
|
40
|
-
return redirect_to edit_structure_field_group_path( @structure.slug, @field_group.slug ), flash: { error: new_field_setting.errors }
|
41
|
-
end
|
42
|
-
end
|
43
|
-
end
|
44
35
|
|
45
|
-
#
|
46
|
-
|
47
|
-
|
48
|
-
unless choice[:label].blank? || choice[:value].blank?
|
49
|
-
new_choice = Choice.create( choice )
|
50
|
-
unless new_choice
|
51
|
-
return redirect_to edit_structure_field_group_path( @structure.slug, @field_group.slug ), flash: { error: new_choice.errors }
|
52
|
-
end
|
53
|
-
end
|
54
|
-
end
|
55
|
-
end
|
36
|
+
# Add nested classes
|
37
|
+
add_new_field_settings
|
38
|
+
add_new_choices
|
56
39
|
|
57
40
|
# Update the other ones
|
58
41
|
if @field_group.update(field_group_params)
|
@@ -100,11 +83,12 @@ module Binda
|
|
100
83
|
:required,
|
101
84
|
:default_text,
|
102
85
|
:ancestry,
|
103
|
-
:
|
86
|
+
:default_choice_id,
|
104
87
|
:allow_null,
|
105
88
|
choices: [],
|
106
89
|
choices_attributes:
|
107
|
-
[ :
|
90
|
+
[ :id,
|
91
|
+
:field_setting_id,
|
108
92
|
:label,
|
109
93
|
:value
|
110
94
|
]
|
@@ -115,7 +99,8 @@ module Binda
|
|
115
99
|
def new_params
|
116
100
|
params.require(:field_group).permit(
|
117
101
|
new_field_settings:
|
118
|
-
[ :
|
102
|
+
[ :id,
|
103
|
+
:field_group_id,
|
119
104
|
:field_setting_id,
|
120
105
|
:name,
|
121
106
|
:slug,
|
@@ -129,7 +114,8 @@ module Binda
|
|
129
114
|
choices: []
|
130
115
|
],
|
131
116
|
new_choices:
|
132
|
-
[ :
|
117
|
+
[ :id,
|
118
|
+
:field_setting_id,
|
133
119
|
:label,
|
134
120
|
:value
|
135
121
|
]
|
@@ -140,5 +126,31 @@ module Binda
|
|
140
126
|
FieldSetting.reset_field_settings_array
|
141
127
|
end
|
142
128
|
|
129
|
+
def add_new_field_settings
|
130
|
+
# Create new fields if any
|
131
|
+
new_params[:new_field_settings].each do |field_setting|
|
132
|
+
unless field_setting[:name].blank?
|
133
|
+
new_field_setting = @field_group.field_settings.create( field_setting )
|
134
|
+
unless new_field_setting
|
135
|
+
return redirect_to edit_structure_field_group_path( @structure.slug, @field_group.slug ), flash: { error: new_field_setting.errors }
|
136
|
+
end
|
137
|
+
end
|
138
|
+
end
|
139
|
+
end
|
140
|
+
|
141
|
+
def add_new_choices
|
142
|
+
# Create new fields if any
|
143
|
+
unless new_params[:new_choices].nil?
|
144
|
+
new_params[:new_choices].each do |choice|
|
145
|
+
unless choice[:label].blank? || choice[:value].blank?
|
146
|
+
new_choice = Choice.create( choice )
|
147
|
+
unless new_choice
|
148
|
+
return redirect_to edit_structure_field_group_path( @structure.slug, @field_group.slug ), flash: { error: new_choice.errors }
|
149
|
+
end
|
150
|
+
end
|
151
|
+
end
|
152
|
+
end
|
153
|
+
end
|
154
|
+
|
143
155
|
end
|
144
156
|
end
|
@@ -25,23 +25,23 @@ module Binda
|
|
25
25
|
@field_setting = @field_group.field_settings.build(field_setting_params)
|
26
26
|
|
27
27
|
if @field_setting.save
|
28
|
-
redirect_to structure_field_group_field_setting_path( @structure
|
28
|
+
redirect_to structure_field_group_field_setting_path( @structure, @field_group, @field_setting ), notice: 'Field setting was successfully created.'
|
29
29
|
else
|
30
|
-
redirect_to new_structure_field_group_field_setting_path( @structure
|
30
|
+
redirect_to new_structure_field_group_field_setting_path( @structure, @field_group ), flash: { alert: @field_setting.errors }
|
31
31
|
end
|
32
32
|
end
|
33
33
|
|
34
34
|
def update
|
35
35
|
if @field_setting.update(field_setting_params)
|
36
|
-
redirect_to structure_field_group_field_setting_path( @structure
|
36
|
+
redirect_to structure_field_group_field_setting_path( @structure, @field_group, @field_setting ), notice: 'Field setting was successfully updated.'
|
37
37
|
else
|
38
|
-
redirect_to edit_structure_field_group_field_setting_path( @structure
|
38
|
+
redirect_to edit_structure_field_group_field_setting_path( @structure, @field_group, @field_setting ), flash: { alert: @field_setting.errors }
|
39
39
|
end
|
40
40
|
end
|
41
41
|
|
42
42
|
def destroy
|
43
43
|
@field_setting.destroy
|
44
|
-
redirect_to structure_field_group_path( @structure
|
44
|
+
redirect_to structure_field_group_path( @structure, @field_group ), notice: 'Field setting and all dependent content were successfully destroyed.'
|
45
45
|
end
|
46
46
|
|
47
47
|
private
|
@@ -3,7 +3,6 @@ require_dependency "binda/application_controller"
|
|
3
3
|
module Binda
|
4
4
|
class Manage::UsersController < ApplicationController
|
5
5
|
before_action :set_user, only: [:show, :edit, :update, :destroy]
|
6
|
-
before_action :check_if_is_superadmin, only: [:update, :destroy]
|
7
6
|
|
8
7
|
def index
|
9
8
|
@users = User.all
|
@@ -38,7 +37,7 @@ module Binda
|
|
38
37
|
|
39
38
|
def update
|
40
39
|
if @user.is_superadmin && !current_user.is_superadmin
|
41
|
-
redirect_to manage_users_url, notice: 'Sorry, you cannot edit a administrator.'
|
40
|
+
redirect_to manage_users_url, notice: 'Sorry, you cannot edit a super administrator.'
|
42
41
|
else
|
43
42
|
respond_to do |format|
|
44
43
|
if @user.update(user_params)
|
@@ -57,7 +56,7 @@ module Binda
|
|
57
56
|
def destroy
|
58
57
|
if current_user.email == @user.email
|
59
58
|
redirect_to manage_users_url, flash: { alert: 'Sorry, you cannot delete your own account.' }
|
60
|
-
elsif !current_user.is_superadmin
|
59
|
+
elsif @user.is_superadmin && !current_user.is_superadmin
|
61
60
|
redirect_to manage_users_url, flash: { alert: 'Sorry, you cannot delete an administrator.' }
|
62
61
|
else
|
63
62
|
@user.destroy
|
@@ -87,11 +86,5 @@ module Binda
|
|
87
86
|
params[:user].delete(:password_confirmation)
|
88
87
|
end
|
89
88
|
end
|
90
|
-
|
91
|
-
def check_if_is_superadmin
|
92
|
-
if current_user.is_superadmin
|
93
|
-
redirect_to manage_users_url, alert: 'Sorry, it\'s forbidden to modify this account.'
|
94
|
-
end
|
95
|
-
end
|
96
89
|
end
|
97
90
|
end
|
@@ -5,7 +5,7 @@ module Binda
|
|
5
5
|
before_action :set_structure, only: [:show, :edit, :update, :destroy, :fields_update ]
|
6
6
|
|
7
7
|
def index
|
8
|
-
@structures = Structure.order('position').all
|
8
|
+
@structures = Structure.order('position').all.page params[:page]
|
9
9
|
end
|
10
10
|
|
11
11
|
def show
|
@@ -23,14 +23,7 @@ module Binda
|
|
23
23
|
@structure = Structure.new(structure_params)
|
24
24
|
|
25
25
|
if @structure.save
|
26
|
-
#
|
27
|
-
@field_group = @structure.field_groups.build( name: 'General Details', position: 1 )
|
28
|
-
# Unless there is a problem...
|
29
|
-
unless @field_group.save
|
30
|
-
return redirect_to structure_path( @structure.slug ), flash: { error: 'General Details group hasn\'t been created' }
|
31
|
-
end
|
32
|
-
# ... redirect to the new structure path
|
33
|
-
redirect_to structure_path( @structure.slug ), notice: "#{ @structure.name } structure was successfully created."
|
26
|
+
redirect_to structure_path( @structure.slug ), notice: "#{ @structure.name.capitalize } structure was successfully created."
|
34
27
|
else
|
35
28
|
render :new
|
36
29
|
end
|
@@ -49,7 +42,7 @@ module Binda
|
|
49
42
|
|
50
43
|
# Update the other ones
|
51
44
|
if @structure.update(structure_params)
|
52
|
-
redirect_to structure_path( @structure.slug ), notice: "#{ @structure.name } structure was successfully updated."
|
45
|
+
redirect_to structure_path( @structure.slug ), notice: "#{ @structure.name.capitalize } structure was successfully updated."
|
53
46
|
else
|
54
47
|
render :edit
|
55
48
|
end
|
@@ -57,11 +50,11 @@ module Binda
|
|
57
50
|
|
58
51
|
def destroy
|
59
52
|
@structure.destroy
|
60
|
-
redirect_to structures_url, notice: "#{ @structure.name } structure was successfully destroyed."
|
53
|
+
redirect_to structures_url, notice: "#{ @structure.name.capitalize } structure was successfully destroyed."
|
61
54
|
end
|
62
55
|
|
63
56
|
def fields_update
|
64
|
-
redirect_to :back, notice: "#{ @structure.name } structure was successfully updated."
|
57
|
+
redirect_to :back, notice: "#{ @structure.name.capitalize } structure was successfully updated."
|
65
58
|
end
|
66
59
|
|
67
60
|
def sort
|
@@ -79,7 +72,7 @@ module Binda
|
|
79
72
|
|
80
73
|
# Only allow a trusted parameter "white list" through.
|
81
74
|
def structure_params
|
82
|
-
params.require(:structure).permit(:name, :slug, :position, :has_categories, field_groups_attributes: [ :id, :name, :structure_id, :slug ] )
|
75
|
+
params.require(:structure).permit(:name, :slug, :position, :has_categories, :instance_type, field_groups_attributes: [ :id, :name, :structure_id, :slug ] )
|
83
76
|
end
|
84
77
|
|
85
78
|
def new_params
|
@@ -0,0 +1,226 @@
|
|
1
|
+
module Binda
|
2
|
+
# Binda comes with a bunch of helpers to make life easier.
|
3
|
+
module DefaultHelpers
|
4
|
+
extend ActiveSupport::Concern
|
5
|
+
|
6
|
+
# Get components
|
7
|
+
#
|
8
|
+
# This method retrieves **all components** belonging to a specific structure.
|
9
|
+
# With this method you can optimize the query in order to avoid the infamous
|
10
|
+
# [N+1 issue](https://youtu.be/oJ4Ur5XPAF8) by specifing what field types
|
11
|
+
# you are going to request in the view. The list of field types available
|
12
|
+
# can be found in the [official documentation](https://github.com/lacolonia/binda/wiki/Fields).
|
13
|
+
# Field types must be listed as separated strings, lowercase and plural.
|
14
|
+
# See the following examples.
|
15
|
+
#
|
16
|
+
# @example Get all components belonging to `page` structure:
|
17
|
+
# get_components('page')
|
18
|
+
# # return all published pages sorted by position
|
19
|
+
#
|
20
|
+
# get_components('page', { published: false })
|
21
|
+
# # return all published and draft pages sorted by position
|
22
|
+
#
|
23
|
+
# get_components('page', { custom_order: 'created_at DESC' })
|
24
|
+
# # return all published pages sorted chronologically
|
25
|
+
#
|
26
|
+
# get_components('page', { fields: ['strings'] })
|
27
|
+
# get_components('page', { fields: ['strings', 'dates', 'assets'] })
|
28
|
+
# # it optimizes the query in order to avoid the N+1 issue
|
29
|
+
# # useful if you are going to request any field from these components
|
30
|
+
# # or from any repeaters
|
31
|
+
#
|
32
|
+
# get_components('page', { published: true, custom_order: 'name', fields: ['string', 'repeaters'] })
|
33
|
+
# # it's then possible to create any sort of combination
|
34
|
+
#
|
35
|
+
# @param slug [string] The slug of the structure to which the components belong
|
36
|
+
# @param args [hash] A hash containing the options used to customize the query.
|
37
|
+
#
|
38
|
+
# The hash parameters are:
|
39
|
+
#
|
40
|
+
# - `published` (boolean) - Set to true to gather just published components,
|
41
|
+
# false to get them all
|
42
|
+
# - `custom_order` (string) - Set the order parameter. Default is `position`,
|
43
|
+
# but you can use any of the following `created_at`, `updated_at`, `id`, `name`, `slug`.
|
44
|
+
# Append `DESC` if you want to reverse the order, e.g. `position DESC`
|
45
|
+
# - `fields` (array) - Include related field classes to the query. The list of field types available
|
46
|
+
# can be found in the [official documentation](https://github.com/lacolonia/binda/wiki/Fields).
|
47
|
+
# Field types must be listed as separated strings, lowercase and plural.
|
48
|
+
#
|
49
|
+
# @return [ActiveRecord Object]
|
50
|
+
def get_components( slug, args = { published: true, custom_order: 'position', fields: [] })
|
51
|
+
|
52
|
+
validate_provided_arguments( args )
|
53
|
+
|
54
|
+
# Sets defaults
|
55
|
+
args[:published] = 'true' if args[:published].nil?
|
56
|
+
args[:custom_order] = 'position' if args[:custom_order].nil?
|
57
|
+
args[:fields] = [] if args[:fields].nil?
|
58
|
+
|
59
|
+
validate_provided_fields( args )
|
60
|
+
validate_provided_custom_order( args )
|
61
|
+
|
62
|
+
# Generate query
|
63
|
+
if args[:published]
|
64
|
+
if args[:fields].any?
|
65
|
+
Component.where( structure_id: Structure.where( slug: slug ) ).published.includes( args[:fields] )
|
66
|
+
else
|
67
|
+
Component.where( structure_id: Structure.where( slug: slug ) ).published
|
68
|
+
end
|
69
|
+
else
|
70
|
+
if args[:fields].any?
|
71
|
+
Component.where( structure_id: Structure.where( slug: slug ) ).order( args[:custom_order] ).includes( args[:fields] )
|
72
|
+
else
|
73
|
+
Component.where( structure_id: Structure.where( slug: slug ) ).order( args[:custom_order] )
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
# This method retrieves a **single component**.
|
79
|
+
# With this method you can optimize the query in order to avoid the infamous
|
80
|
+
# [N+1 issue](https://youtu.be/oJ4Ur5XPAF8) by specifing what field types
|
81
|
+
# you are going to request in the view. The list of field types available
|
82
|
+
# can be found in the [official documentation](https://github.com/lacolonia/binda/wiki/Fields).
|
83
|
+
# Field types must be listed as separated strings, lowercase and plural.
|
84
|
+
# See the following examples.
|
85
|
+
#
|
86
|
+
# @example Get a component with slug `my-first-post`
|
87
|
+
# get_component('my-first-post')
|
88
|
+
# # return the component
|
89
|
+
#
|
90
|
+
# get_component('my-first-post', { fields: ['strings', 'texts', 'assets', 'selections'] })
|
91
|
+
# # return the component and optimize the query for any of the listed fields related to it
|
92
|
+
#
|
93
|
+
#
|
94
|
+
# @param slug [string] The slug of the component
|
95
|
+
# @param args [hash] A hash containing the options used to customize the query.
|
96
|
+
#
|
97
|
+
# The hash parameters are:
|
98
|
+
#
|
99
|
+
# - `fields` (array) - Include related field classes to the query. The list of field types available
|
100
|
+
# can be found in the [official documentation](https://github.com/lacolonia/binda/wiki/Fields).
|
101
|
+
# Field types must be listed as separated strings, lowercase and plural.
|
102
|
+
#
|
103
|
+
# @return [ActiveRecord Object]
|
104
|
+
def get_component( slug, args = { fields: [] })
|
105
|
+
|
106
|
+
validate_provided_arguments( args )
|
107
|
+
|
108
|
+
# Sets defaults
|
109
|
+
args[:fields] = [] if args[:fields].nil?
|
110
|
+
|
111
|
+
validate_provided_fields( args )
|
112
|
+
|
113
|
+
if args[:fields].any?
|
114
|
+
Component.where(slug: slug).first.inlcudes( args[:fields] )
|
115
|
+
else
|
116
|
+
Component.where(slug: slug).first
|
117
|
+
end
|
118
|
+
end
|
119
|
+
|
120
|
+
# This method retrieves a **board**.
|
121
|
+
# With this method you can optimize the query in order to avoid the infamous
|
122
|
+
# [N+1 issue](https://youtu.be/oJ4Ur5XPAF8) by specifing what field types
|
123
|
+
# you are going to request in the view. The list of field types available
|
124
|
+
# can be found in the [official documentation](https://github.com/lacolonia/binda/wiki/Fields).
|
125
|
+
# Field types must be listed as separated strings, lowercase and plural.
|
126
|
+
# See the following examples.
|
127
|
+
#
|
128
|
+
# @example Get a board with slug `my-dashboard`
|
129
|
+
# get_board('my-dashboard')
|
130
|
+
# # return the board
|
131
|
+
#
|
132
|
+
# get_board('my-dashboard', { fields: ['strings', 'texts', 'assets', 'selections'] })
|
133
|
+
# # return the board and optimize the query for any of the listed fields related to it
|
134
|
+
#
|
135
|
+
#
|
136
|
+
# @param slug [string] The slug of the board
|
137
|
+
# @param args [hash] A hash containing the options used to customize the query.
|
138
|
+
#
|
139
|
+
# The hash parameters are:
|
140
|
+
#
|
141
|
+
# - `fields` (array) - Include related field classes to the query. The list of field types available
|
142
|
+
# can be found in the [official documentation](https://github.com/lacolonia/binda/wiki/Fields).
|
143
|
+
# Field types must be listed as separated strings, lowercase and plural.
|
144
|
+
#
|
145
|
+
# @return [ActiveRecord Object]
|
146
|
+
def get_board( slug, args = { fields: [] })
|
147
|
+
|
148
|
+
validate_provided_arguments( args )
|
149
|
+
|
150
|
+
# Sets defaults
|
151
|
+
args[:fields] = [] if args[:fields].nil?
|
152
|
+
|
153
|
+
check_provided_fields( args )
|
154
|
+
|
155
|
+
if args[:fields].any?
|
156
|
+
Board.where(slug: slug).first.inlcudes( args[:fields] )
|
157
|
+
else
|
158
|
+
Board.where(slug: slug).first
|
159
|
+
end
|
160
|
+
end
|
161
|
+
|
162
|
+
# This method retrieves a **repeater**.
|
163
|
+
# With this method you can optimize the query in order to avoid the infamous
|
164
|
+
# [N+1 issue](https://youtu.be/oJ4Ur5XPAF8) by specifing what field types
|
165
|
+
# you are going to request in the view. The list of field types available
|
166
|
+
# can be found in the [official documentation](https://github.com/lacolonia/binda/wiki/Fields).
|
167
|
+
# Field types must be listed as separated strings, lowercase and plural.
|
168
|
+
# See the following examples.
|
169
|
+
#
|
170
|
+
# @example Get a repeater with slug `slideshow`
|
171
|
+
# get_repeater('slideshow')
|
172
|
+
# # return the repeater
|
173
|
+
#
|
174
|
+
# get_repeater('slideshow', { fields: ['strings', 'assets'] })
|
175
|
+
# # return the repeater and optimize the query for any of the listed fields related to it
|
176
|
+
#
|
177
|
+
#
|
178
|
+
# @param slug [string] The slug of the repeater
|
179
|
+
# @param args [hash] A hash containing the options used to customize the query.
|
180
|
+
#
|
181
|
+
# The hash parameters are:
|
182
|
+
#
|
183
|
+
# - `fields` (array) - Include related field classes to the query. The list of field types available
|
184
|
+
# can be found in the [official documentation](https://github.com/lacolonia/binda/wiki/Fields).
|
185
|
+
# Field types must be listed as separated strings, lowercase and plural.
|
186
|
+
#
|
187
|
+
# @return [ActiveRecord Object]
|
188
|
+
def get_repeater( slug, args = { fields: [] })
|
189
|
+
|
190
|
+
validate_provided_arguments( args )
|
191
|
+
|
192
|
+
# Sets defaults
|
193
|
+
args[:fields] = [] if args[:fields].nil?
|
194
|
+
|
195
|
+
check_provided_fields( args )
|
196
|
+
|
197
|
+
if args[:fields].any?
|
198
|
+
Repeater.where(slug: slug).first.inlcudes( args[:fields] )
|
199
|
+
else
|
200
|
+
Repeater.where(slug: slug).first
|
201
|
+
end
|
202
|
+
end
|
203
|
+
|
204
|
+
private
|
205
|
+
|
206
|
+
# Check if provided `fields` are ok, otherwise raise an error
|
207
|
+
def validate_provided_fields( args )
|
208
|
+
raise ArgumentError, "Argument error in get_components(): fields should be an Array, not a #{args[:fields].class.to_s}.", caller unless args[:fields].instance_of? Array
|
209
|
+
args[:fields].each do |f|
|
210
|
+
raise ArgumentError, "Argument error in get_components(): #{f.to_s} is not a valid field type.", caller unless FieldSetting.get_field_classes.map{ |fc| fc.underscore.pluralize }.include? f.to_s
|
211
|
+
end
|
212
|
+
end
|
213
|
+
|
214
|
+
# Check if provided `custom_order` is ok, otherwise raise an error
|
215
|
+
def validate_provided_custom_order( args )
|
216
|
+
raise ArgumentError, "Argument error in get_components(): custom_order should be a String, not a #{args[:custom_order].class.to_s}.", caller unless args[:custom_order].instance_of? ::String
|
217
|
+
end
|
218
|
+
|
219
|
+
# Check if provided arguments are ok, otherwise raise an error
|
220
|
+
def validate_provided_arguments( args )
|
221
|
+
args.each do |key, value|
|
222
|
+
raise ArgumentError, "Argument error in get_components(): #{key.to_s} is not a valid key.", caller unless ['published', 'custom_order', 'fields'].include? key.to_s
|
223
|
+
end
|
224
|
+
end
|
225
|
+
end
|
226
|
+
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
module Binda
|
2
|
+
module FieldableHelpers
|
3
|
+
|
4
|
+
extend ActiveSupport::Concern
|
5
|
+
|
6
|
+
# Only allow a trusted parameter "white list" through.
|
7
|
+
def fieldable_params
|
8
|
+
[ texts_attributes: [ :id, :field_setting_id, :fieldable_type, :fieldable_id, :content ],
|
9
|
+
strings_attributes: [ :id, :field_setting_id, :fieldable_type, :fieldable_id, :content ],
|
10
|
+
assets_attributes: [ :id, :field_setting_id, :fieldable_type, :fieldable_id, :image ],
|
11
|
+
dates_attributes: [ :id, :field_setting_id, :fieldable_type, :fieldable_id, :date ],
|
12
|
+
galleries_attributes: [ :id, :field_setting_id, :fieldable_type, :fieldable_id ],
|
13
|
+
radios_attributes: [ :id, :field_setting_id, :fieldable_type, :fieldable_id, :choice_ids ],
|
14
|
+
selections_attributes: [ :id, :field_setting_id, :fieldable_type, :fieldable_id, :choice_ids ],
|
15
|
+
checkboxes_attributes: [ :id, :field_setting_id, :fieldable_type, :fieldable_id, choice_ids: [] ],
|
16
|
+
repeaters_attributes: [ :id, :field_setting_id, :fieldable_type, :fieldable_id, :field_group_id,
|
17
|
+
texts_attributes: [ :id, :field_setting_id, :fieldable_type, :fieldable_id, :content ],
|
18
|
+
assets_attributes: [ :id, :field_setting_id, :fieldable_type, :fieldable_id, :image ],
|
19
|
+
dates_attributes: [ :id, :field_setting_id, :fieldable_type, :fieldable_id, :date ],
|
20
|
+
galleries_attributes: [ :id, :field_setting_id, :fieldable_type, :fieldable_id ],
|
21
|
+
repeaters_attributes: [ :id, :field_setting_id, :fieldable_type, :fieldable_id, :field_group_id ],
|
22
|
+
radios_attributes: [ :id, :field_setting_id, :fieldable_type, :fieldable_id, :choice_ids ],
|
23
|
+
selections_attributes: [ :id, :field_setting_id, :fieldable_type, :fieldable_id, :choice_ids ],
|
24
|
+
checkboxes_attributes: [ :id, :field_setting_id, :fieldable_type, :fieldable_id, choice_ids: [] ]
|
25
|
+
]]
|
26
|
+
end
|
27
|
+
|
28
|
+
def repeater_params
|
29
|
+
params.require(:repeater).permit(
|
30
|
+
new_repeaters_attributes: [ :id, :field_setting_id, :field_group_id, :fieldable_type, :fieldable_id,
|
31
|
+
texts_attributes: [ :id, :field_setting_id, :fieldable_type, :fieldable_id, :content ],
|
32
|
+
strings_attributes: [ :id, :field_setting_id, :fieldable_type, :fieldable_id, :content ],
|
33
|
+
assets_attributes: [ :id, :field_setting_id, :fieldable_type, :fieldable_id, :image ],
|
34
|
+
dates_attributes: [ :id, :field_setting_id, :fieldable_type, :fieldable_id, :date ],
|
35
|
+
galleries_attributes: [ :id, :field_setting_id, :fieldable_type, :fieldable_id ],
|
36
|
+
repeaters_attributes: [ :id, :field_setting_id, :field_group_id, :fieldable_type, :fieldable_id ],
|
37
|
+
radios_attributes: [ :id, :field_setting_id, :fieldable_type, :fieldable_id, :choice_ids ],
|
38
|
+
selections_attributes: [ :id, :field_setting_id, :fieldable_type, :fieldable_id, :choice_ids ],
|
39
|
+
checkboxes_attributes: [ :id, :field_setting_id, :fieldable_type, :fieldable_id, choice_ids: [] ]
|
40
|
+
])
|
41
|
+
end
|
42
|
+
|
43
|
+
end
|
44
|
+
end
|