bootbox_crud 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (44) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +31 -0
  3. data/MIT.license +20 -0
  4. data/README.md +301 -0
  5. data/app/views/layouts/_bb_alert.html.haml +2 -0
  6. data/app/views/layouts/_show_link_to.html.haml +5 -0
  7. data/app/views/layouts/_show_link_to_array.html.haml +6 -0
  8. data/app/views/layouts/_show_value.html.haml +4 -0
  9. data/app/views/modals/_alert.js.erb +5 -0
  10. data/app/views/modals/_create.js.erb +1 -0
  11. data/app/views/modals/_destroy.js.erb +1 -0
  12. data/app/views/modals/_form.js.erb +16 -0
  13. data/app/views/modals/_update.js.erb +1 -0
  14. data/bootbox_crud.gemspec +28 -0
  15. data/lib/bootbox_crud.rb +7 -0
  16. data/lib/bootbox_crud/action_view/helpers.rb +37 -0
  17. data/lib/bootbox_crud/engine.rb +23 -0
  18. data/lib/bootbox_crud/version.rb +5 -0
  19. data/lib/generators/bootbox_crud/install_generator.rb +16 -0
  20. data/lib/generators/bootbox_crud/templates/README +6 -0
  21. data/lib/generators/bootbox_crud/templates/app/assets/javascripts/models.js +7 -0
  22. data/lib/generators/bootbox_crud/templates/config/initializers/simple_form.rb +142 -0
  23. data/lib/generators/bootbox_crud/templates/config/initializers/simple_form_bootstrap.rb +163 -0
  24. data/lib/generators/rails/haml_modal_crud/USAGE +15 -0
  25. data/lib/generators/rails/haml_modal_crud/haml_modal_crud_generator.rb +30 -0
  26. data/lib/generators/rails/haml_modal_crud/templates/create.js.erb +2 -0
  27. data/lib/generators/rails/haml_modal_crud/templates/destroy.js.erb +1 -0
  28. data/lib/generators/rails/haml_modal_crud/templates/update.js.erb +1 -0
  29. data/lib/generators/rails/modal_crud_route/USAGE +9 -0
  30. data/lib/generators/rails/modal_crud_route/modal_crud_route_generator.rb +21 -0
  31. data/lib/generators/rails/modal_crud_route/templates/models.js +7 -0
  32. data/lib/templates/haml/scaffold/_form.html.haml +12 -0
  33. data/lib/templates/haml/scaffold/edit.html.haml +2 -0
  34. data/lib/templates/haml/scaffold/index.html.haml +51 -0
  35. data/lib/templates/haml/scaffold/new.html.haml +2 -0
  36. data/lib/templates/haml/scaffold/show.html.haml +7 -0
  37. data/lib/templates/rails/scaffold_controller/controller.rb +102 -0
  38. data/vendor/assets/javascripts/bootbox_crud_main.js +6 -0
  39. data/vendor/assets/javascripts/bootbox_crud_modals.js +206 -0
  40. data/vendor/assets/javascripts/sortable.js +177 -0
  41. data/vendor/assets/stylesheets/bootbox_crud.css.scss +25 -0
  42. data/vendor/assets/stylesheets/bootbox_crud_main.css.scss +2 -0
  43. data/vendor/assets/stylesheets/sortable-theme-minimal.css +47 -0
  44. metadata +255 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 327909d03311374db095666dbfdb1bb53fbeb67e
4
+ data.tar.gz: a6b888c27abd58112c69d967872e08f8cde0a7aa
5
+ SHA512:
6
+ metadata.gz: 7f970bd5a452e99bfe27acc053c078829bddc3531730cf92c04ef829fa752122489a92700b43d118c94562d888d6c351258dcafe25737cfcc3104e05e72d272b
7
+ data.tar.gz: 63241a4c639a077b1d961cfb314a52950a855483560b28409bdfe7dbc939316d66deac24ecb295c12d5f20a653371fa125521ca03b38ae05680f4dda6692bfd4
data/.gitignore ADDED
@@ -0,0 +1,31 @@
1
+ *.gem
2
+ *.rbc
3
+ /.config
4
+ /coverage/
5
+ /InstalledFiles
6
+ /pkg/
7
+ /spec/reports/
8
+ /test/tmp/
9
+ /test/version_tmp/
10
+ /tmp/
11
+
12
+ ## Documentation cache and generated files:
13
+ /.yardoc/
14
+ /_yardoc/
15
+ /doc/
16
+ /rdoc/
17
+
18
+ ## Environment normalisation:
19
+ /.bundle/
20
+ /vendor/bundle
21
+ /lib/bundler/man/
22
+
23
+ # for a library or gem, you might want to ignore these files since the code is
24
+ # intended to run in multiple environments; otherwise, check them in:
25
+ Gemfile.lock
26
+ .ruby-version
27
+ .ruby-gemset
28
+
29
+ # unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
30
+ .rvmrc
31
+
data/MIT.license ADDED
@@ -0,0 +1,20 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2013 - 2014 Nguyen Huu Phuoc
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of
6
+ this software and associated documentation files (the "Software"), to deal in
7
+ the Software without restriction, including without limitation the rights to
8
+ use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9
+ the Software, and to permit persons to whom the Software is furnished to do so,
10
+ subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17
+ FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18
+ COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19
+ IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20
+ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,301 @@
1
+ # Bootbox modal CRUD
2
+
3
+ Provides Rails modal CRUD scaffolding powered by bootstrap & bootbox & simple_form. Built for use with Turbolinks, jQuery and Twitter Bootstrap 3.
4
+
5
+ ## Initial setup
6
+
7
+ Set custom scaffolding generators in your ```config/application.rb```
8
+
9
+ config.generators do |g|
10
+ g.template_engine :haml_modal_crud
11
+ g.resource_route :modal_crud_route
12
+ end
13
+
14
+ Require the main javascript file in your ```app/assets/javascripts/application.js```
15
+
16
+ //= require bootbox_crud_main
17
+
18
+ Require the main stylesheet file in your ```app/assets/stylesheets/application.css``` and also don't forget to add the main Bootstrap stylesheet
19
+
20
+ *= require bootstrap
21
+ *= require bootbox_crud_main
22
+
23
+ Add bootbox alert markup into the main container of your ```app/views/layouts/application.html.haml```
24
+
25
+ %body
26
+ .container
27
+ = yeild
28
+ = bb_alert
29
+
30
+ Run the install generator to copy over default simple_form initializers and models.js for defining modal CRUD enabled models
31
+
32
+ $ bundle exec rails g bootbox_crud:install
33
+
34
+ ## Scaffolding
35
+
36
+ Custom model scaffolding is enabled by installing and configuring this gem. The generated files are following the rules described in the Modal CRUD section.
37
+
38
+ $ bundle exec rails g scaffold Block name width:integer height:integer depth:integer
39
+
40
+ The above code will give you a ready to use, nicely formatted modal enabled Block CRUD. Just migrate the DB.
41
+
42
+ # Modal CRUD with [bootbox.js](https://github.com/rocsci/bootbox-rails)
43
+
44
+ This concept is a work in progress and suggestions are welcome.
45
+
46
+ Main source files are in the gem:
47
+
48
+ vendor/assets/javascripts/bootbox_crud_modals.js
49
+ app/views/modals/_form.js
50
+ app/views/modals/_alert.js
51
+ app/views/modals/_create.js
52
+ app/views/modals/_destroy.js
53
+ app/views/modals/_update.js
54
+
55
+ and in your application
56
+
57
+ app/assets/javascripts/models.js
58
+
59
+ ## Controller
60
+
61
+ ### Create, update and destroy
62
+
63
+ All of these methods must have a js response format defined in the respond_to block. Update example:
64
+
65
+ respond_to do |format|
66
+ if @model.update(model_params)
67
+ ...
68
+ format.js
69
+ else
70
+ ...
71
+ format.js
72
+ end
73
+ end
74
+ end
75
+
76
+ ### New, edit and show
77
+
78
+ If you want your modals to be fast, related actions shouldn't render with a full layout. Instead return only the form or detail template.
79
+ In another words, use ``` render layout: false ```.
80
+
81
+ def new
82
+ @model = Model.new
83
+
84
+ respond_to do |format|
85
+ format.html { render layout: false }
86
+ format.json { render json: @model }
87
+ end
88
+ end
89
+
90
+ def edit
91
+ render layout: false
92
+ end
93
+
94
+ #### Different show view template for modal and normal page
95
+
96
+ In cases where a separate view template is needed for modal and normal full page show action, you can achieve that by doing the following.
97
+ The modal show request accepted content type is \*/\* so it is going to take the .js format response option as acceptable, because it is the first in the list.
98
+ A standard request will skip format.js and take the html option.
99
+
100
+ def show
101
+ respond_to do |format|
102
+ format.js { render 'show_modal', layout: false }
103
+ format.html
104
+ end
105
+ end
106
+
107
+ This method can also be used for differentiating edit and new actions for modal and normal views.
108
+
109
+ ## Views
110
+
111
+ ### Create, update and destroy
112
+
113
+ Three new files have to be added to the views directory of the related model:
114
+
115
+ **create.js.erb**
116
+
117
+ <%= render partial: 'modals/create', locals: { model: @model, form_path: 'model/form' } %>
118
+
119
+ **update.js.erb**
120
+
121
+ <%= render partial: 'modals/update', locals: { model: @model, form_path: 'model/form' } %>
122
+
123
+ **destroy.js.erb**
124
+
125
+ <%= render partial: 'modals/destroy', locals: { model: @model, form_path: 'model/form' } %>
126
+
127
+
128
+ The partials in app/views/modals directory should provide the necessary functionality for most cases. If you need some special behaviour, implement it instead of rendering the partial.
129
+
130
+ **Create/Destroy visit_path** [optional parameter]: determines what page to load after the model has been created/destroyed.
131
+
132
+ The path is visited by invoking Turbolinks.visit(visit_path). If it isn't set, the current page will be reloaded.
133
+
134
+ It is mostly useful in cases:
135
+
136
+ * when you create a new model and want to redirect to its full page detail or when you create a new subpage/tab and you want to refresh the page with an anchor after the base URL (the example is used for overviews)
137
+
138
+
139
+ <%= render partial: 'modals/create', locals: { model: @overview, form_path: 'overviews/form', visit_path: "#overview_#{@overview.id}" } %>
140
+
141
+ * where you can delete a model from its full page detail and you need to redirect the user to some list view, a simple page reload wouldn't be useful because the model was destroyed and its detail can't be shown anymore
142
+
143
+
144
+ <%= render partial: 'modals/destroy', locals: { visit_path: "/#{ location_name.to_s }/unipolars" } %>
145
+
146
+ ### New, edit and show
147
+
148
+ All views being shown inside a modal window must have a root node with ```id='content'```.
149
+ Example in haml:
150
+
151
+ #content
152
+ = render 'form'
153
+
154
+ The javascript handling the response will fill this node and its contents into the modal window body.
155
+
156
+ #### Forms
157
+
158
+ Forms used inside of modal windows need to have the remote attribute set to true.
159
+
160
+ = simple_form @model, remote: true
161
+
162
+ This way they are submitted via an ajax request.
163
+
164
+ The ```ApplicationHelper#remote_form_options``` helper with default simple\_form formatting and layout wrapping options should be used instead of only writing ```remote: true``` to keep all forms unified.
165
+ But more on that in the main forms section of this readme.
166
+
167
+ #### Show helpers
168
+
169
+ Use these in case you want to show values of a model in a modal window.
170
+
171
+ show_value(label, value)
172
+ show_link_to(label, object, link_text)
173
+ show_link_to_array(label, objects, name_object_field)
174
+
175
+ show_value 'Name', @model.name
176
+ show_link_to 'Model', @model, @model.name
177
+ show_link_to_array 'Model blackboxes', @model.blackboxes, 'name'
178
+
179
+ Wrap them into a div with ```class='form-horizontal'```. Bootstrap horizontal form layout with little customization is used to display the values.
180
+
181
+ Adding ```class='show'``` to root node with ```id='content'``` is mandatory to get the right styling. If you forget this one, it will look like input fields.
182
+
183
+ #content.show
184
+ .form-horizontal
185
+ = show_value 'Name', @model.name
186
+ = show_value 'Status', @model.status
187
+
188
+ ### Links to modals
189
+
190
+ Making links modal enabled is done via data attributes.
191
+
192
+ data-entity='Model'
193
+ data-action='update'
194
+ data-id='1'
195
+
196
+ In haml:
197
+
198
+ = link_to '#', :class => 'btn btn-primary btn-sm', data: { id: @model.id, entity: 'Model', action: 'update' } do
199
+ %i.fa.fa-edit
200
+ edit
201
+
202
+ There is a global button handler searching for any DOM nodes with 'data-entity' attribute. Click events of such nodes lead to modals.
203
+ The logic is simple, clicking on a node with the above values will lead to this function invocation:
204
+
205
+ BBCrud.Model.update({id: 1})
206
+
207
+ Available actions are:
208
+
209
+ * create
210
+ * update
211
+ * show
212
+
213
+ #### Adding a new model to modals on the client side
214
+
215
+ The above mentioned functions are defined in **models.js**, if you want to add modals to a new model, you have to add a new line there.
216
+
217
+ BBCrud.Models.add(namespace, baseUrl, modalHeaderTitle);
218
+
219
+ * **namespace** is used to define the BBCrud.Unipolar object
220
+ * **baseUrl** is setting the base route for the models actions
221
+ * **modalHeaderTitle** is used in modal titles, it should be a singular downcase name of the model
222
+
223
+ Filled in for unipolars:
224
+
225
+ BBCrud.Models.add('Unipolar', '/unipolars/', 'unipolar');
226
+
227
+ The above function invocation creates these functions:
228
+
229
+ BBCrud.Unipolar.create
230
+ BBCrud.Unipolar.update
231
+ BBCrud.Unipolar.show
232
+
233
+ Now you should be all set to click your data-entity links and call the functions from your scripts if needed.
234
+
235
+ *Note*: I18n is currently not supported on the client, there are a few ways how to approach it. The necessary changes should be simple.
236
+
237
+ #### Adding custom actions to models (non CRUD)
238
+
239
+ Is done in ```models.js``` by adding a line similar to the for defining CRUD actions. Here is an example for close event action:
240
+
241
+ BBCrud.Models.addAction('Event', '/events/', 'event', 'close');
242
+
243
+ The first three arguments are the same as for ```BBCrud.Models.add``` function, the last argument is the new action name.
244
+ After adding this line, you can create a button with the following data attributes and your modal is almost ready.
245
+
246
+ data-entity='Event'
247
+ data-action='close'
248
+ data-id='1'
249
+
250
+ Of course you have to do all the little tweaks on this action, as you did for CRUD. Meaning, adding ```#content``` to the form view and rendering it without layout. Next adding ```format.js``` to the relevant controller action and creating a ```*.js.erb``` view template for it.
251
+
252
+ To finish our ```Event.close``` example, the file will be named ```events/close_event.js.erb``` and it's contents:
253
+
254
+ <%= render partial: 'modals/form', locals: { model: @event, form_path: 'events/close', success_alert: 'Closed' , error_alert: 'Error while closing' } %>
255
+
256
+ The important detail to notice is the use of ```modals/form``` partial, which was made to handle custom actions. The rest of the arguments are self-explanatory.
257
+
258
+ ## Modals in short
259
+
260
+ * Create format.js lines in respond_to block of controller :create, :update, :destroy
261
+ * Return views without layout in controller :new, :show, :edit
262
+ * Make sure your views have a root ```id='content'``` node & with ```class='show'``` in case of a show view using show helpers from this guide
263
+ * Make sure your form has ```remote: true``` or preferably ```remote_form_options``` with simple\_form, the layout should be the same as in the example in form section of this README
264
+ * Add create, update and destroy .js.erb files to the view directory and fill them according to this guide
265
+ * Add your model definition as a new line to models.js
266
+ * Add ```data-entity```, ```data-action``` and ```data-id``` with the right values to an element on the page and click it, or call ``BBCrud.ModelName.create/update/show()`` from your javascript to show the modal
267
+
268
+
269
+ ## Custom scaffold source files
270
+
271
+ If you want have a peek or override some of the scaffolding templates, here is an overview of the files used:
272
+
273
+ **haml\_modal\_crud**
274
+
275
+ lib/generators/rails/haml_modal_crud/templates/create.js.erb
276
+ lib/generators/rails/haml_modal_crud/templates/destroy.js.erb
277
+ lib/generators/rails/haml_modal_crud/templates/update.js.erb
278
+ lib/generators/rails/haml_modal_crud/haml_modal_crud_generator.rb
279
+ lib/generators/rails/haml_modal_crud/USAGE
280
+
281
+ **modal\_crud\_route**
282
+
283
+ lib/generators/rails/modal_crud_route/modal_crud_route_generator.rb
284
+
285
+ **haml scaffold templates override**
286
+
287
+ lib/templates/haml/scaffold/_form.html.haml
288
+ lib/templates/haml/scaffold/edit.html.haml
289
+ lib/templates/haml/scaffold/index.html.haml
290
+ lib/templates/haml/scaffold/new.html.haml
291
+ lib/templates/haml/scaffold/show.html.haml
292
+
293
+ **controller scaffold template override**
294
+
295
+ lib/templates/rails/scaffold_controller/controller.rb
296
+
297
+ # TODOs
298
+
299
+ * I18n support on client side
300
+ * Improve BBCrud.Alert / integration with Rails flash messages
301
+ * Generator tests
@@ -0,0 +1,2 @@
1
+ %div.bb-alert.alert.alert-info{ style: 'display:none;' }
2
+ %span
@@ -0,0 +1,5 @@
1
+ .form-group.string.optional
2
+ %label.string.optional.control-label.col-sm-5= label.to_s
3
+ .col-sm-6
4
+ .form-control
5
+ = link_to link_to_text, link_to_object
@@ -0,0 +1,6 @@
1
+ .form-group.string.optional
2
+ %label.string.optional.control-label.col-sm-5= label.to_s
3
+ .col-sm-6
4
+ - objects.each do |o|
5
+ .form-control
6
+ = link_to o[field], o
@@ -0,0 +1,4 @@
1
+ .form-group.string.optional
2
+ %label.string.optional.control-label.col-sm-5= label.to_s
3
+ .col-sm-6
4
+ .form-control= value
@@ -0,0 +1,5 @@
1
+ var modal = $('.bootbox.modal');
2
+ modal.modal('hide');
3
+ BBCrud.Alert.show('<%= alert %>');
4
+ var path = '<%= defined?(visit_path) ? visit_path : nil %>';
5
+ Turbolinks.visit(path === '' ? null : path);
@@ -0,0 +1 @@
1
+ <%= render partial: 'modals/form', locals: { model: model, form_path: form_path, success_alert: defined?(success_alert) ? success_alert : 'Created' , error_alert: defined?(error_alert) ? error_alert : 'Error while creating', visit_path: defined?(visit_path) ? visit_path : nil } %>
@@ -0,0 +1 @@
1
+ <%= render partial: 'modals/form', locals: { model: model, form_path: form_path, success_alert: defined?(success_alert) ? success_alert : 'Deleted' , error_alert: defined?(error_alert) ? error_alert : 'Error while deleting', visit_path: defined?(visit_path) ? visit_path : nil } %>
@@ -0,0 +1,16 @@
1
+ var modal = $('.bootbox.modal');
2
+ <%- if model.errors.any? %>
3
+ BBCrud.Alert.show('<%= error_alert %>');
4
+ <% if remotipart_submitted? %>
5
+ var form = "<%= j "#{render(form_path)}" %>";
6
+ <% else %>
7
+ var form = "<%= j render(form_path) %>";
8
+ <% end %>
9
+ modal.find('.modal-body').html(form);
10
+ modal.find('input[type="submit"]').hide();
11
+ <%- else %>
12
+ BBCrud.Alert.show('<%= success_alert %>');
13
+ modal.modal('hide');
14
+ var path = '<%= defined?(visit_path) ? visit_path : nil %>';
15
+ Turbolinks.visit(path === '' ? null : path);
16
+ <%- end %>
@@ -0,0 +1 @@
1
+ <%= render partial: 'modals/form', locals: { model: model, form_path: form_path, success_alert: defined?(success_alert) ? success_alert : 'Updated' , error_alert: defined?(error_alert) ? error_alert : 'Error while updating', visit_path: defined?(visit_path) ? visit_path : nil } %>
@@ -0,0 +1,28 @@
1
+ require File.expand_path('../lib/bootbox_crud/version', __FILE__)
2
+
3
+ Gem::Specification.new do |gem|
4
+ gem.authors = ["Jiri Kaipr"]
5
+ gem.email = ["jiri.kaipr@gmail.com"]
6
+ gem.description = %q{Provides Rails modal CRUD scaffolding powered by bootstrap & bootbox & simple_form. Built for use with Turbolinks, jQuery and Twitter Bootstrap 3.}
7
+ gem.homepage = "https://github.com/Virtualmaster/bootbox_crud"
8
+ gem.summary = %q{Rails CRUD scaffold with bootbox modals.}
9
+
10
+ gem.name = "bootbox_crud"
11
+ gem.require_paths = ["lib"]
12
+ gem.files = `git ls-files`.split("\n")
13
+ gem.version = BootboxCrud::Rails::VERSION
14
+ gem.platform = Gem::Platform::RUBY
15
+
16
+ gem.add_dependency "railties", ">= 4.0"
17
+ gem.add_dependency "sass-rails", "~> 5.0"
18
+ gem.add_dependency "bootstrap-sass", "~> 3.0"
19
+ gem.add_dependency "haml", "~> 4.0"
20
+ gem.add_dependency "haml-rails", "~> 0.7"
21
+ gem.add_dependency "simple_form", "~> 3.0"
22
+ gem.add_dependency "bootbox-rails", "~> 0.5"
23
+ gem.add_dependency "jquery-rails", "~> 3.0"
24
+ gem.add_dependency "turbolinks", "~> 2.5"
25
+ gem.add_dependency "jquery-turbolinks", "~> 2.1"
26
+ gem.add_dependency "remotipart", "~> 1.2"
27
+ gem.add_development_dependency "bundler", "~> 1.0"
28
+ end