modal_logic 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- NDViZWI1NDAwYjVkZWI1ZDk5MmZmZGNlZDE4MTNlNDFjYTA2MWRmMQ==
4
+ NjRhZWYxMTUwZjM4ZmIxYWFiYzljNWEwNjk3MDVjYzZjMzEzNDhkNw==
5
5
  data.tar.gz: !binary |-
6
- NWExM2QxZDVjMzMyNTJkNDRlNWFhZmQzNWRmOGJlZWQ0YTc1Y2I3MA==
6
+ MDgyYjBhZjdlOTRjZjJiOWNlZDM4ZTQzNmFlYTBjMTNjMjI0NzhlNQ==
7
7
  !binary "U0hBNTEy":
8
8
  metadata.gz: !binary |-
9
- MzFkNGY4ZDQ3MGU0ZDhhZDE0Mzc3ZDRhZGMzYWE0YWEyNTI1MDU5NjQ1YzE3
10
- ZTM5ZTk5OTRiOGFmNzI4MTdlNzU0YTRhYmViM2RmZGYyY2JhMDJkNDk5MTFm
11
- ZGE0ZWUwNjc3NWU1ZTZkZDhhNWIwNGNkZTBhYjcxZDM0OTJkNzQ=
9
+ MWQ0MmIzZTRkMTJmMDNlZTVkYTljNDUzZjVlYWVmNDZkMTNmNjA3NjVhZTIy
10
+ ZmQ4OWEwMmE3YjcyNjg0NjdmNmIzOGMxMDRjNjFkMjg0ODcyZjYxYjIyYjFj
11
+ MzZmMWMwMzg2MzdiMGNhY2MwZWEwNDMwMTBhMTU2NDExM2YyZjc=
12
12
  data.tar.gz: !binary |-
13
- Njc5ZGRlYTJhZDdlNmU4YmExZmY2NDE3ODY5MGExOGRmMjc5MTFlMmU4MmY1
14
- OGQ2ZWQ3YjJmODBlMjE5MzVmNDUzMjA4NTliMWNhMGNiNDQ2NWMyNDE0NmJk
15
- Y2ZjZWE3ZGY4N2U5ZjkwMDUyMmE2N2VjN2RkNjlhZDgwNWJmZmM=
13
+ YzllZDEwMTZkN2VmNTI2OGU2M2RkMzk3MThjOGVkZjA5MGNmMmM1N2QwZDM1
14
+ N2EzZWEzOWI0YzJlMjVhMWFhNWEyZmZhZDU0NzVhZTkwMzczNjFiM2Q4YWI1
15
+ ODk3ZGEyODlhY2M3NzkyZGFlNDY3NjJkYTkwMTkxOTQzNzRjMmU=
data/README.md CHANGED
@@ -1,3 +1,23 @@
1
1
  = ModalLogic
2
2
 
3
- This project rocks and uses MIT-LICENSE.
3
+
4
+
5
+
6
+ ===Install
7
+
8
+ ```ruby
9
+ # Gemfile
10
+
11
+ gem 'modal_logic'
12
+
13
+ group :assets do
14
+ gem 'handlebars_assets'
15
+ end
16
+ ```
17
+
18
+ ```javascript
19
+ /* app/assets/javascripts/application.js */
20
+ //= require modal_logic
21
+ //= require handlebars.runtime
22
+ //= require templates/modal/crud
23
+ ```
@@ -3,42 +3,25 @@ class ModalLogic
3
3
  that = @
4
4
  $('a[data-remote][modal],form[data-remote][modal]').live 'ajax:complete', (jq_event, xhr) ->
5
5
  response = JSON.parse(xhr.responseText)
6
- if response.close?
7
- if response.redirect_location?
8
- window.location = response.redirect_location
6
+ if response?
7
+ if response.close?
8
+ window.location = if response.redirect_location? then response.redirect_location else window.location
9
9
  else
10
- window.location = window.location
11
- else if ! response.html?
10
+ that.createOrUpdateModalContent(response)
11
+ else
12
12
  that.createOrUpdateModalContent
13
13
  title: 'Error'
14
- html: 'There was an error. Please reload the page and try again'
15
- else
16
- that.createOrUpdateModalContent(response)
14
+ body: 'There was an error. Please reload the page and try again'
15
+
17
16
  createOrUpdateModalContent: (context) =>
18
- modal = $('.modal.modal-logic')
19
- if modal.length is 0
20
- html = @renderTemplate
21
- title: context.title
22
- body: context.html
23
- $('body').append html
17
+ html = HandlebarsTemplates['modal/crud']( modal: context )
18
+ $modal = $('.modal.modal-logic')
19
+ if $modal.length > 0
20
+ $modal.modal('hide').replaceWith html
24
21
  else
25
- modal.find('.modal-body').empty().append context.html
26
- modal.find('.modal-header > .modal-title').empty().append context.title
27
-
22
+ $('body').append html
28
23
  @showModal()
29
24
 
30
25
  showModal: =>
31
26
  $('.modal.modal-logic').modal('show')
32
27
 
33
- renderTemplate: (context) =>
34
- """
35
- <div class="modal modal-logic hide fade">
36
- <div class="modal-header">
37
- <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
38
- <h3 class='modal-title'>#{context.title}</h3>
39
- </div>
40
- <div class="modal-body">
41
- #{context.body}
42
- </div>
43
- </div>
44
- """
@@ -4,7 +4,6 @@ var ModalLogic,
4
4
 
5
5
  ModalLogic = (function() {
6
6
  function ModalLogic() {
7
- this.renderTemplate = __bind(this.renderTemplate, this);
8
7
  this.showModal = __bind(this.showModal, this);
9
8
  this.createOrUpdateModalContent = __bind(this.createOrUpdateModalContent, this);
10
9
  var that;
@@ -14,36 +13,32 @@ ModalLogic = (function() {
14
13
  var response;
15
14
 
16
15
  response = JSON.parse(xhr.responseText);
17
- if (response.close != null) {
18
- if (response.redirect_location != null) {
19
- return window.location = response.redirect_location;
16
+ if (response != null) {
17
+ if (response.close != null) {
18
+ return window.location = response.redirect_location != null ? response.redirect_location : window.location;
20
19
  } else {
21
- return window.location = window.location;
20
+ return that.createOrUpdateModalContent(response);
22
21
  }
23
- } else if (response.html == null) {
22
+ } else {
24
23
  return that.createOrUpdateModalContent({
25
24
  title: 'Error',
26
- html: 'There was an error. Please reload the page and try again'
25
+ body: 'There was an error. Please reload the page and try again'
27
26
  });
28
- } else {
29
- return that.createOrUpdateModalContent(response);
30
27
  }
31
28
  });
32
29
  }
33
30
 
34
31
  ModalLogic.prototype.createOrUpdateModalContent = function(context) {
35
- var html, modal;
36
-
37
- modal = $('.modal.modal-logic');
38
- if (modal.length === 0) {
39
- html = this.renderTemplate({
40
- title: context.title,
41
- body: context.html
42
- });
43
- $('body').append(html);
32
+ var $modal, html;
33
+
34
+ html = HandlebarsTemplates['modal/crud']({
35
+ modal: context
36
+ });
37
+ $modal = $('.modal.modal-logic');
38
+ if ($modal.length > 0) {
39
+ $modal.modal('hide').replaceWith(html);
44
40
  } else {
45
- modal.find('.modal-body').empty().append(context.html);
46
- modal.find('.modal-header > .modal-title').empty().append(context.title);
41
+ $('body').append(html);
47
42
  }
48
43
  return this.showModal();
49
44
  };
@@ -52,10 +47,6 @@ ModalLogic = (function() {
52
47
  return $('.modal.modal-logic').modal('show');
53
48
  };
54
49
 
55
- ModalLogic.prototype.renderTemplate = function(context) {
56
- return "<div class=\"modal modal-logic hide fade\">\n <div class=\"modal-header\">\n <button type=\"button\" class=\"close\" data-dismiss=\"modal\" aria-hidden=\"true\">×</button>\n <h3 class='modal-title'>" + context.title + "</h3>\n </div>\n <div class=\"modal-body\">\n " + context.body + "\n </div>\n</div>";
57
- };
58
-
59
50
  return ModalLogic;
60
51
 
61
52
  })();
@@ -0,0 +1,12 @@
1
+ <div class="modal modal-logic hide">
2
+ <div class="modal-header">
3
+ <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
4
+ <h3 class='modal-title'>{{modal.title}}</h3>
5
+ </div>
6
+ <div class="modal-body">
7
+ {{#if modal.errors.base}}
8
+ <div class='alert alert-error'>{{{modal.errors.base}}}</div>
9
+ {{/if}}
10
+ {{{modal.body}}}
11
+ </div>
12
+ </div>
@@ -3,20 +3,22 @@ module ModalLogic
3
3
  def modal_form_response( model, opts = {})
4
4
  response = {}
5
5
  case params[:action]
6
- when 'create'
7
- if ! model.valid?
8
- response[:html] = render_to_string path_to_current_controller_form
9
- response[:errors] = model.errors
6
+ when 'new', 'edit'
7
+ response[:body] = render_to_string path_to_current_controller_form, layout: false
8
+ response[:title] = opts[:title] || modal_title(model)
9
+
10
+ when 'create', 'update'
11
+ if ! model.valid? || ! model.persisted? || opts[:errors].present?
12
+ response[:body] = render_to_string path_to_current_controller_form
13
+ response[:errors] = opts[:errors] || model.errors
10
14
  response[:flash] = flash
11
15
  response[:title] = opts[:title] || modal_title(model)
12
16
  else
13
17
  response[:redirect_location] = opts[:redirect_location] if opts[:redirect_location]
14
18
  response[:close] = true
15
19
  end
16
- when 'new'
17
- response[:html] = render_to_string path_to_current_controller_form, layout: false
18
- response[:title] = opts[:title] || modal_title(model)
19
20
  end
21
+
20
22
  response
21
23
  end
22
24
 
@@ -26,9 +28,7 @@ module ModalLogic
26
28
  end
27
29
 
28
30
  def path_to_current_controller_form( opts = {} )
29
- filename = opts[:filename] || '_form'
30
- request_base_path = request.path.split(params[:action].to_s).last
31
- File.join(Rails.root, 'app/views', request_base_path, filename )
31
+ File.join(Rails.root, 'app/views', params[:controller], opts[:filename] || '_form')
32
32
  end
33
33
  end
34
34
  end
@@ -1,3 +1,3 @@
1
1
  module ModalLogic
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: modal_logic
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Adam Hallett
@@ -24,6 +24,20 @@ dependencies:
24
24
  - - ~>
25
25
  - !ruby/object:Gem::Version
26
26
  version: 3.2.13
27
+ - !ruby/object:Gem::Dependency
28
+ name: handlebars_assets
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '='
32
+ - !ruby/object:Gem::Version
33
+ version: 0.14.1
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '='
39
+ - !ruby/object:Gem::Version
40
+ version: 0.14.1
27
41
  description: The simplicity of Rails forms coupled with Bootstrap modal
28
42
  email:
29
43
  - adam.t.hallett@gmail.com
@@ -33,6 +47,7 @@ extra_rdoc_files: []
33
47
  files:
34
48
  - app/assets/javascripts/modal_logic.coffee
35
49
  - app/assets/javascripts/modal_logic.js
50
+ - app/assets/javascripts/templates/modal/crud.hbs
36
51
  - lib/modal_logic/engine.rb
37
52
  - lib/modal_logic/helpers.rb
38
53
  - lib/modal_logic/version.rb