binda 0.1.1 → 0.1.2

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.
Files changed (31) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +3 -3
  3. data/app/assets/javascripts/binda/components/{form_item_choice.js → field_setting_choices.js} +13 -12
  4. data/app/assets/javascripts/binda/components/form_item_repeater.js +34 -30
  5. data/app/assets/javascripts/binda/components/login_form.js +15 -15
  6. data/app/assets/javascripts/binda/components/sortable.js +22 -20
  7. data/app/assets/javascripts/binda/dist/binda.bundle.js +154 -140
  8. data/app/assets/javascripts/binda/index.js +20 -20
  9. data/app/assets/stylesheets/binda/components/field_setting_choices.scss +90 -0
  10. data/app/assets/stylesheets/binda/components/form_item.scss +0 -50
  11. data/app/assets/stylesheets/binda/components/{form_item_choice.scss → form_item_choices.scss} +1 -7
  12. data/app/assets/stylesheets/binda/components/main_sortable_table.scss +68 -0
  13. data/app/assets/stylesheets/binda/components/main_table.scss +2 -5
  14. data/app/assets/stylesheets/binda/components/sortable.scss +1 -2
  15. data/app/assets/stylesheets/binda/components/standard-form.scss +5 -6
  16. data/app/assets/stylesheets/binda/index.scss +3 -1
  17. data/app/controllers/binda/field_groups_controller.rb +22 -75
  18. data/app/controllers/binda/manage/users_controller.rb +18 -13
  19. data/app/controllers/binda/structures_controller.rb +12 -11
  20. data/app/models/binda/choice.rb +10 -5
  21. data/app/models/binda/field_setting.rb +16 -6
  22. data/app/models/concerns/binda/fieldable_associations.rb +24 -12
  23. data/app/views/binda/components/sort_index.html.erb +21 -25
  24. data/app/views/binda/field_groups/_form_body.html.erb +5 -5
  25. data/app/views/binda/field_groups/_form_item_choice.erb +49 -66
  26. data/app/views/binda/structures/_form_section.html.erb +1 -1
  27. data/app/views/binda/structures/index.html.erb +2 -2
  28. data/app/views/binda/structures/sort_index.html.erb +28 -21
  29. data/config/locales/en.yml +7 -5
  30. data/lib/binda/version.rb +1 -1
  31. metadata +6 -4
@@ -2,8 +2,10 @@
2
2
  {
3
3
  table
4
4
  {
5
+ display: table;
5
6
  width: 100%;
6
7
  background-color: $color-gray-lightest;
8
+ box-sizing: border-box;
7
9
 
8
10
  th
9
11
  {
@@ -48,11 +50,6 @@
48
50
  {
49
51
  border-bottom: 1px solid $color-gray-darker;
50
52
  }
51
-
52
- tr.ui-sortable-handle:hover
53
- {
54
- border: 1px solid transparent;
55
- }
56
53
  }
57
54
 
58
55
  .prev
@@ -16,6 +16,7 @@ ul#sortable
16
16
  transition: box-shadow .3s ease, transform .3s ease;
17
17
  box-shadow: 0 0 0 0 rgba(0,0,0,.25);
18
18
 
19
+
19
20
  img
20
21
  {
21
22
  width: auto;
@@ -74,8 +75,6 @@ tbody.sortable
74
75
  tr
75
76
  {
76
77
  transition: box-shadow .3s ease, transform .3s ease;
77
- margin-bottom: 8px;
78
- padding-bottom: 8px;
79
78
 
80
79
  img
81
80
  {
@@ -39,12 +39,7 @@
39
39
  border-right: none !important;
40
40
  border-left: none !important;
41
41
  }
42
-
43
- &.table-sort
44
- {
45
- border: none !important;
46
- }
47
-
42
+
48
43
  .control-label
49
44
  {
50
45
  font-weight: bold;
@@ -210,4 +205,8 @@
210
205
  .standard-form--hint-on-new-record
211
206
  {
212
207
  padding: 12px;
208
+ }
209
+
210
+ .standard-form--choices-header {
211
+
213
212
  }
@@ -19,7 +19,7 @@
19
19
  @import "components/login";
20
20
  @import "components/standard-form";
21
21
  @import "components/form_item";
22
- @import "components/form_item_choice";
22
+ @import "components/form_item_choices";
23
23
  @import "components/form_item_image";
24
24
  @import "components/form_item_video";
25
25
  @import "components/sortable";
@@ -28,6 +28,8 @@
28
28
  @import "components/select2";
29
29
  @import "components/texts";
30
30
  @import "components/popup_warning";
31
+ @import "components/main_sortable_table";
32
+ @import "components/field_setting_choices";
31
33
 
32
34
  // Controllers
33
35
  @import "controllers/components_index";
@@ -35,7 +35,7 @@ module Binda
35
35
  # Add nested classes
36
36
  add_new_field_settings
37
37
  add_new_choices
38
- update_choices
38
+ check_if_needs_to_update_choices
39
39
 
40
40
  # Update the other ones
41
41
  if @field_group.update(field_group_params)
@@ -64,63 +64,11 @@ module Binda
64
64
 
65
65
  # Only allow a trusted parameter "white list" through.
66
66
  def field_group_params
67
- params.require(:field_group).permit(
68
- :name,
69
- :slug,
70
- :description,
71
- :position,
72
- :layout,
73
- :structure_id,
74
- field_settings_attributes:
75
- [ :id,
76
- :field_group_id,
77
- :field_setting_id,
78
- :name,
79
- :slug,
80
- :description,
81
- :field_type,
82
- :position,
83
- :required,
84
- :default_text,
85
- :ancestry,
86
- :default_choice_id,
87
- :allow_null,
88
- accepted_structure_ids: [],
89
- choices: [],
90
- choices_attributes:
91
- [ :id,
92
- :field_setting_id,
93
- :label,
94
- :value
95
- ]
96
- ]
97
- )
67
+ params.require(:field_group).permit(:name, :slug, :description, :position, :layout, :structure_id, field_settings_attributes: [ :id, :field_group_id, :field_setting_id, :name, :slug, :description, :field_type, :position, :required, :default_text, :ancestry, :default_choice_id, :allow_null, accepted_structure_ids: [], choices: [], choices_attributes: [ :id, :field_setting_id, :label, :value ]])
98
68
  end
99
69
 
100
70
  def new_params
101
- params.require(:field_group).permit(
102
- new_field_settings:
103
- [ :id,
104
- :field_group_id,
105
- :field_setting_id,
106
- :name,
107
- :slug,
108
- :description,
109
- :field_type,
110
- :position,
111
- :required,
112
- :ancestry,
113
- :default_choice_id,
114
- :allow_null,
115
- choices: []
116
- ],
117
- new_choices:
118
- [ :id,
119
- :field_setting_id,
120
- :label,
121
- :value
122
- ]
123
- )
71
+ params.require(:field_group).permit( new_field_settings: [ :id, :field_group_id, :field_setting_id, :name, :slug, :description, :field_type, :position, :required, :ancestry, :default_choice_id, :allow_null, choices: [] ],new_choices: [ :id, :field_setting_id, :label, :value ])
124
72
  end
125
73
 
126
74
  def reset_field_settings_cache
@@ -130,24 +78,20 @@ module Binda
130
78
  def add_new_field_settings
131
79
  # Create new fields if any
132
80
  new_params[:new_field_settings].each do |field_setting|
133
- unless field_setting[:name].blank?
134
- new_field_setting = @field_group.field_settings.create( field_setting )
135
- unless new_field_setting
136
- return redirect_to edit_structure_field_group_path( @structure.slug, @field_group.slug ), flash: { error: new_field_setting.errors }
137
- end
138
- end
81
+ next if field_setting[:name].blank?
82
+ new_field_setting = @field_group.field_settings.create( field_setting )
83
+ next if new_field_setting
84
+ return redirect_to edit_structure_field_group_path( @structure.slug, @field_group.slug ), flash: { error: new_field_setting.errors }
139
85
  end
140
86
  end
141
87
 
142
88
  def add_new_choices
143
89
  # Create new fields if any
144
- unless new_params[:new_choices].nil?
145
- new_params[:new_choices].each do |choice|
146
- unless choice[:label].blank? || choice[:value].blank?
147
- create_new_choice choice
148
- end
149
- end
150
- end
90
+ return if new_params[:new_choices].nil?
91
+ new_params[:new_choices].each do |choice|
92
+ next if choice[:label].blank? || choice[:value].blank?
93
+ create_new_choice choice
94
+ end
151
95
  end
152
96
 
153
97
  # Create new choice (depends directly from add_new_choice method)
@@ -158,18 +102,21 @@ module Binda
158
102
  end
159
103
  end
160
104
 
161
- def update_choices
105
+ def check_if_needs_to_update_choices
162
106
  return if field_group_params[:field_settings_attributes].nil?
163
107
  field_group_params[:field_settings_attributes].each do |_, field_setting_params|
164
108
  next if field_setting_params[:choices_attributes].nil?
165
- field_setting_params[:choices_attributes].each do |_, choice_params|
166
- choice = Choice.find(choice_params[:id])
167
- unless choice.update(choice_params)
168
- return redirect_to edit_structure_field_group_path( @structure.slug, @field_group.slug ), flash: { error: choice.errors }
169
- end
170
- end
109
+ update_field_setting_choices field_setting_params
171
110
  end
172
111
  end
173
112
 
113
+ def update_field_setting_choices field_setting_params
114
+ field_setting_params[:choices_attributes].each do |_, choice_params|
115
+ choice = Choice.find(choice_params[:id])
116
+ unless choice.update(choice_params)
117
+ return redirect_to edit_structure_field_group_path( @structure.slug, @field_group.slug ), flash: { error: choice.errors }
118
+ end
119
+ end
120
+ end
174
121
  end
175
122
  end
@@ -36,19 +36,18 @@ module Binda
36
36
  end
37
37
 
38
38
  def update
39
- if @user.is_superadmin && !current_user.is_superadmin
40
- redirect_to manage_users_url, notice: 'Sorry, you cannot edit a super administrator.'
41
- else
42
- respond_to do |format|
43
- if @user.update(user_params)
44
- format.html { redirect_to manage_user_path( @user.id ), notice: 'User was successfully updated.' }
45
- format.xml { head :ok }
46
- format.json { render :show, status: :ok, location: @user }
47
- else
48
- format.html { redirect_to edit_manage_user_path( @user.id ), flash: { alert: @user.errors } }
49
- format.xml { head :bad_request }
50
- format.json { render json: @user.errors, status: :unprocessable_entity }
51
- end
39
+ # Check if the user to be updated is superadmin and the user which is updating is superadmin
40
+ check_if_superadmin
41
+
42
+ respond_to do |format|
43
+ if @user.update(user_params)
44
+ format.html { redirect_to manage_user_path( @user.id ), notice: 'User was successfully updated.' }
45
+ format.xml { head :ok }
46
+ format.json { render :show, status: :ok, location: @user }
47
+ else
48
+ format.html { redirect_to edit_manage_user_path( @user.id ), flash: { alert: @user.errors } }
49
+ format.xml { head :bad_request }
50
+ format.json { render json: @user.errors, status: :unprocessable_entity }
52
51
  end
53
52
  end
54
53
  end
@@ -86,5 +85,11 @@ module Binda
86
85
  params[:user].delete(:password_confirmation)
87
86
  end
88
87
  end
88
+
89
+ def check_if_superadmin
90
+ if @user.is_superadmin && !current_user.is_superadmin
91
+ return redirect_to manage_users_url, notice: 'Sorry, you cannot edit a super administrator.'
92
+ end
93
+ end
89
94
  end
90
95
  end
@@ -31,14 +31,7 @@ module Binda
31
31
 
32
32
  def update
33
33
  # Create new fields if any
34
- new_params[:new_field_groups].each do |field_group|
35
- unless field_group[:name].blank?
36
- new_field_group = @structure.field_groups.create( name: field_group[:name] )
37
- unless new_field_group
38
- return redirect_to structure_path( @structure.slug ), flash: { error: new_field_group.errors }
39
- end
40
- end
41
- end
34
+ add_new_field_groups
42
35
 
43
36
  # Update the other ones
44
37
  if @structure.update(structure_params)
@@ -61,8 +54,7 @@ module Binda
61
54
  params[:structure].each_with_index do |id, i|
62
55
  Structure.find( id ).update({ position: i + 1 })
63
56
  end
64
- render js: "$('.popup-warning').addClass('popup-warning--hidden');"
65
- head :ok
57
+ render js: "$('##{params[:id]}').sortable('option', 'disabled', false); $('.popup-warning').addClass('popup-warning--hidden'); $('.sortable').removeClass('sortable--disabled')"
66
58
  end
67
59
 
68
60
  def sort_index
@@ -81,7 +73,16 @@ module Binda
81
73
  end
82
74
 
83
75
  def new_params
84
- params.require(:structure).permit( new_field_groups:[ :name, :structure_id ] )
76
+ params.require(:structure).permit( new_field_groups:[ :name, :slug, :structure_id ] )
77
+ end
78
+
79
+ def add_new_field_groups
80
+ new_params[:new_field_groups].each do |field_group|
81
+ next if field_group[:name].blank?
82
+ new_field_group = @structure.field_groups.create( name: field_group[:name], slug: field_group[:slug] )
83
+ next if new_field_group
84
+ return redirect_to structure_path( @structure.slug ), flash: { error: new_field_group.errors }
85
+ end
85
86
  end
86
87
  end
87
88
  end
@@ -72,12 +72,17 @@ module Binda
72
72
  selections = Selection.joins(:field_setting).where(binda_field_settings: {id: field_setting.id })
73
73
 
74
74
  # Make sure none of the Binda::Selection instances (radios/checkboxes/selects) are left without a choice
75
- selections.each do |selection|
76
- next if selection.choices.any?
77
- selection.choices << Choice.find(self.field_setting.default_choice_id)
78
- raise "It hasn't been possible to set the default choice for Binda::Selection with id=#{selection.id}" unless selection.save
79
- end
75
+ update_selection selections
80
76
  end
81
77
 
78
+ private
79
+ def update_selection selections
80
+ selections.each do |selection|
81
+ next if selection.choices.any?
82
+ selection.choices << Choice.find(self.field_setting.default_choice_id)
83
+ raise "It hasn't been possible to set the default choice for Binda::Selection with id=#{selection.id}" unless selection.save
84
+ end
85
+ end
86
+
82
87
  end
83
88
  end
@@ -153,19 +153,29 @@ module Binda
153
153
  def create_field_instances
154
154
  structure = self.field_group.structure
155
155
  field_class = self.field_type.pluralize
156
+ field_setting_id = self.id
156
157
  case
157
158
  when structure.components.any?
158
159
  structure.components.each do |component|
159
- unless component.send(field_class).where(field_setting_id: self.id).any?
160
- component.send(field_class).create!(field_setting_id: self.id)
161
- end
160
+ create_field_instances_for_component( component, field_class, field_setting_id )
162
161
  end
163
162
  when structure.board.present?
164
- unless structure.board.send(field_class).where(field_setting_id: self.id).any?
165
- structure.board.send(field_class).create!(field_setting_id: self.id)
166
- end
163
+ create_field_instances_for_board( structure.board, field_class, field_setting_id )
164
+ end
165
+ end
166
+
167
+ # Helper for create_field_instances method
168
+ def create_field_instances_for_component( component, field_class, field_setting_id )
169
+ unless component.send(field_class).where(field_setting_id: field_setting_id).any?
170
+ component.send(field_class).create!(field_setting_id: field_setting_id)
167
171
  end
168
172
  end
169
173
 
174
+ # Helper for create_field_instances method
175
+ def create_field_instances_for_board ( board, field_class, field_setting_id )
176
+ unless board.send(field_class).where(field_setting_id: field_setting_id).any?
177
+ board.send(field_class).create!(field_setting_id: field_setting_id)
178
+ end
179
+ end
170
180
  end
171
181
  end
@@ -96,12 +96,18 @@ module Binda
96
96
  unless obj.nil?
97
97
  obj.content
98
98
  else
99
- you_mean_string = !self.strings.find{ |t| t.field_setting_id == FieldSetting.get_id( field_slug ) && t.type = 'Binda::String' }.nil?
100
- if you_mean_string
101
- raise ArgumentError, "This slug (#{field_slug}) is associated to a string not a text. Use get_string() instead on instance (#{self.class.name} ##{self.id}).", caller
102
- else
103
- raise ArgumentError, "There isn't any text associated to the current slug (#{field_slug}) on instance (#{self.class.name} ##{self.id}).", caller
104
- end
99
+ check_text_error field_slug
100
+ end
101
+ end
102
+
103
+ # Check why get_text doesn't return a value
104
+ # This method isn't supposed to be used by anything other than get_text method
105
+ def check_text_error field_slug
106
+ you_mean_string = !self.strings.find{ |t| t.field_setting_id == FieldSetting.get_id( field_slug ) && t.type = 'Binda::String' }.nil?
107
+ if you_mean_string
108
+ raise ArgumentError, "This slug (#{field_slug}) is associated to a string not a text. Use get_string() instead on instance (#{self.class.name} ##{self.id}).", caller
109
+ else
110
+ raise ArgumentError, "There isn't any text associated to the current slug (#{field_slug}) on instance (#{self.class.name} ##{self.id}).", caller
105
111
  end
106
112
  end
107
113
 
@@ -130,12 +136,18 @@ module Binda
130
136
  unless obj.nil?
131
137
  obj.content
132
138
  else
133
- you_mean_text = !self.strings.find{ |t| t.field_setting_id == FieldSetting.get_id( field_slug ) && t.type = 'Binda::Text' }.nil?
134
- if you_mean_text
135
- raise ArgumentError, "This slug (#{field_slug}) is associated to a text not a string. Use get_text() instead on instance (#{self.class.name} ##{self.id}).", caller
136
- else
137
- raise ArgumentError, "There isn't any string associated to the current slug (#{field_slug}) on instance (#{self.class.name} ##{self.id}).", caller
138
- end
139
+ check_string_error field_slug
140
+ end
141
+ end
142
+
143
+ # Check why get_string doesn't return a value
144
+ # This method isn't supposed to be used by anything other than get_string method
145
+ def check_string_error field_slug
146
+ you_mean_text = !self.strings.find{ |t| t.field_setting_id == FieldSetting.get_id( field_slug ) && t.type = 'Binda::Text' }.nil?
147
+ if you_mean_text
148
+ raise ArgumentError, "This slug (#{field_slug}) is associated to a text not a string. Use get_text() instead on instance (#{self.class.name} ##{self.id}).", caller
149
+ else
150
+ raise ArgumentError, "There isn't any string associated to the current slug (#{field_slug}) on instance (#{self.class.name} ##{self.id}).", caller
139
151
  end
140
152
  end
141
153
 
@@ -17,30 +17,26 @@
17
17
  </div>
18
18
  <p><%= t 'binda.sort_items_loader', { arg1: t('binda.components').downcase } %></p>
19
19
  </div>
20
- <div class="main-table--container">
21
-
22
- <h5 class="text-muted"><%= t('binda.sort_index_hint', { arg1: @structure.name.humanize.split.join(' ').pluralize }).html_safe %></h5>
23
-
24
- <table class="standard-form--container table-sort">
25
- <thead class="standard-form--header">
26
- <tr>
27
- <th style="width: 50%;">Title</th>
28
- <th style="width: 50%;">Publish state</th>
29
- <th></th>
30
- </tr>
31
- </thead>
32
- <tbody id="<%= @structure.slug %>-components--sort-index" class="sortable" data-update-url="<%= structure_components_sort_path( @structure.slug ) %>">
33
- <% @components.order( :position, :name ).each do |component| %>
34
- <tr id="component_<%= component.id %>">
35
- <td><%= link_to component.name, structure_component_path( @structure.slug, component ) %></td>
36
- <td><%= component.publish_state %></td>
37
- <td></td>
38
- </tr>
39
- <% end %>
40
- <tbody>
41
- </table>
42
-
43
- <%= link_to "<i class=\"fa fa-plus\" aria-hidden=\"true\"></i> New #{ @structure.name.humanize.split.map(&:capitalize).join(' ') }".html_safe, new_structure_component_path, class: 'main-header--link b-btn b-btn-primary', action: "new" %>
44
-
20
+
21
+ <div class="main-sortable-table">
22
+
23
+ <div class="main-sortable-table--header-row">
24
+ <div class="main-sortable-table--header-title"><p>Title</p></div>
25
+ <div class="main-sortable-table--header-title"><p>Publish State</p></div>
26
+ </div>
27
+
28
+ <div id="<%= @structure.slug %>-components--sort-index" class="sortable" data-update-url="<%= structure_components_sort_path( @structure.slug ) %>">
29
+ <% @components.order( :position, :name ).each do |component| %>
30
+ <div class="main-sortable-table--row ui-sortable-handle" id="component_<%= component.id %>">
31
+ <div class="main-sortable-table--row-cell"><%= link_to component.name, structure_component_path( @structure.slug, component ) %></div>
32
+ <div class="main-sortable-table--row-cell"><%= component.publish_state %></div>
33
+ </div>
34
+ <% end %>
35
+ </div>
36
+
45
37
  </div>
38
+
39
+ <%= link_to "<i class=\"fa fa-plus\" aria-hidden=\"true\"></i> New #{ @structure.name.humanize.split.map(&:capitalize).join(' ') }".html_safe, new_structure_component_path, class: 'main-header--link b-btn b-btn-primary', action: "new" %>
40
+
41
+
46
42
  <% end %>
@@ -43,10 +43,11 @@
43
43
 
44
44
  <%= render "form_section", f: f %>
45
45
 
46
- <div class="clearfix"></div>
47
46
  </div>
47
+ </div>
48
48
 
49
- <% @field_group.field_settings.where( field_type: 'repeater' ).order( :position, :id ).each do |repeater| %>
49
+ <% @field_group.field_settings.where( field_type: 'repeater' ).order( :position, :id ).each do |repeater| %>
50
+ <div class="standard-form--container">
50
51
  <div id="form-section--repeater-<%= repeater.id %>" class="form-section--wide-container">
51
52
  <div class="form-section">
52
53
 
@@ -56,12 +57,11 @@
56
57
 
57
58
  <%= render "form_section_repeater", f: f, repeater: repeater %>
58
59
 
59
- <div class="clearfix"></div>
60
60
  </div>
61
61
  </div>
62
- <% end %>
62
+ </div>
63
+ <% end %>
63
64
 
64
- </div>
65
65
 
66
66
  </div>
67
67