ajax_modal_rails 1.0.1

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: d7e2e50ac6f2bf4d2cb1f30bb650257184e34763
4
+ data.tar.gz: 13027e7ce01f196e3e3a7163b80ce05acfced388
5
+ SHA512:
6
+ metadata.gz: 247c34fc573dcf5e5cbd1ab9213c1d303b2b8634fdf7a70b60a4a48f4d8edbdf05d1316a4023b38991a1e8b50aacf2b8a1f9f0d65eeb4924ed74b94d1b6ed674
7
+ data.tar.gz: e40a97c28651dc565a9e97b5f6057f731018d4d55502289569a9197dd81b024b93c3f99e40de26dd81b199daeeca7bae3c554bb536ca30b268c97c5aab0261c3
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2017 Rafe Rosen
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,76 @@
1
+ # AjaxModalRails
2
+ This plugin provides a simple way to do common modal interactions in a rails app. Add a single data attribute to links and forms and include a controller mixin to load pages via ajax in a modal.
3
+
4
+ ## Installation
5
+ Add this line to your application's Gemfile:
6
+
7
+ ```ruby
8
+ gem 'ajax_modal_rails', '~> 1.0'
9
+ ```
10
+
11
+ And then execute:
12
+ ```bash
13
+ $ bundle
14
+ ```
15
+
16
+ ### Setup Layout and Javascript
17
+ Run `rails generate ajax_modal_rails:install` to require the necessary javascript in `application.js` and add a partial render to the bottom of the body in `layouts/application.html.erb|haml`
18
+
19
+ #### Manual Installation
20
+
21
+ Add `//= require ajax_modal_rails` to `app/assets/application.js`
22
+
23
+ A page that opens a modal should call `render 'ajax_modal_rails/frame'` somewhere on the page. We recommend you do this in your layout so the modal frame is available for all pages.
24
+
25
+ ## Usage
26
+
27
+ Once everything is set up, loading to modals is pretty simple
28
+
29
+ ### Views
30
+
31
+ Add the attribute `data-loads-in-ajax-modal` to a link and it's request will load in a modal over the current page.
32
+
33
+ Add the attribute `data-submits-to-ajax-modal` to a form and the response from the form's submit will load in the modal frame.
34
+
35
+ ### Controller
36
+
37
+ A controller that is processing actions to be loaded in a modal should `include AjaxModalRails::Controller`
38
+
39
+ The mixin sets the appropriate layout for modal requests and adds behavior that allows a modal request that results in a redirect to redirect the whole page.
40
+
41
+ ## Example
42
+
43
+ These snippets are taken from the included [example application](blob/master/spec/dummy)
44
+
45
+ *app/controllers/messages_controller.rb*
46
+ ```ruby
47
+ class MessagesController < ApplicationController
48
+ before_action :set_message, only: [:edit, :update, :destroy]
49
+
50
+ include AjaxModalRails::Controller
51
+ ...
52
+ ```
53
+
54
+ *app/views/messages/index.html.erb*
55
+ ```erb
56
+ <%= link_to 'New Message', new_message_path, data: {loads_in_ajax_modal: true} %>
57
+ ```
58
+
59
+ *app/views/messages/\_form.html.erb*
60
+ ```erb
61
+ <%= form_with(model: message, data: {submits_to_ajax_modal: true}) do |form| %>
62
+ ```
63
+
64
+ ## Customization
65
+
66
+ If you want to customize the behavior of this gem, run `rails generate ajax_modal_rails:customize`. It will copy the views, javascript, and controller mixin into your application.
67
+
68
+ ## Requirements
69
+
70
+ The provided modal views and javascript require [Twitter Bootstrap 4.0](https://getbootstrap.com/docs/4.0/getting-started/introduction/). You can do some customization if your app is using something else (see above).
71
+
72
+ ## License
73
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
74
+
75
+ ## Gratitude
76
+ This gem was made possible by a Professional Development benefit from my employer, [Green River](http://www.greenriver.com)
data/Rakefile ADDED
@@ -0,0 +1,34 @@
1
+ begin
2
+ require 'bundler/setup'
3
+ rescue LoadError
4
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
5
+ end
6
+
7
+ require 'rdoc/task'
8
+
9
+ RDoc::Task.new(:rdoc) do |rdoc|
10
+ rdoc.rdoc_dir = 'rdoc'
11
+ rdoc.title = 'AjaxModalRails'
12
+ rdoc.options << '--line-numbers'
13
+ rdoc.rdoc_files.include('README.md')
14
+ rdoc.rdoc_files.include('lib/**/*.rb')
15
+ end
16
+
17
+
18
+
19
+ load 'rails/tasks/statistics.rake'
20
+
21
+
22
+
23
+ require 'bundler/gem_tasks'
24
+
25
+ require 'rake/testtask'
26
+
27
+ Rake::TestTask.new(:test) do |t|
28
+ t.libs << 'test'
29
+ t.pattern = 'test/**/*_test.rb'
30
+ t.verbose = false
31
+ end
32
+
33
+
34
+ task default: :test
File without changes
@@ -0,0 +1,60 @@
1
+ #############
2
+ # Ajax modals
3
+ #############
4
+ class window.AjaxModal
5
+ constructor: ->
6
+ @modal = $(".modal[data-ajax-modal]")
7
+ @content = @modal.find("[data-ajax-modal-content]")
8
+ @linkTriggersSelector = '[data-loads-in-ajax-modal]'
9
+ @formTriggersSelector = '[data-submits-to-ajax-modal]'
10
+ @loading = @modal.find("[data-ajax-modal-loading]")
11
+ @listen()
12
+
13
+ listen: ->
14
+ @_registerLinks()
15
+ @_registerForms()
16
+ @_registerClose()
17
+
18
+ _registerLinks: ->
19
+ $('body').on 'click', @linkTriggersSelector, (e) =>
20
+ e.preventDefault()
21
+ @loading.show()
22
+ @open()
23
+ @content.load e.currentTarget.getAttribute("href"), (data) =>
24
+ @loading.hide()
25
+
26
+ _registerForms: ->
27
+ console.log $(@formTriggersSelector).toArray()
28
+ # scope.find(@formTriggersSelector).attr('data-remote', true)
29
+ $('body').on 'submit', @formTriggersSelector, (event) =>
30
+ form = event.currentTarget
31
+ event.preventDefault()
32
+ console.log $(form).serialize()
33
+ $.ajax
34
+ url: form.getAttribute('action')
35
+ type: form.getAttribute('method')
36
+ dataType: 'html',
37
+ data: $(form).serialize()
38
+ complete: (xhr, status) =>
39
+ @loading.hide()
40
+ @content.html xhr.responseText
41
+ @open
42
+ @_registerLinks @content
43
+ return false
44
+
45
+
46
+ # $('body').on 'ajax:complete', @formTriggersSelector, (event, data, status, xhr) =>
47
+ # @_registerForms @content
48
+
49
+ # maybe don't need this for bootstrap
50
+ _registerClose: ->
51
+ @modal.on 'click', '[data-ajax-modal-close]', =>
52
+ @modal.modal('hide')
53
+ @reset()
54
+
55
+ open: ->
56
+ @modal.modal 'show'
57
+
58
+ reset: ->
59
+ @content.html("")
60
+ @loading.show()
@@ -0,0 +1,49 @@
1
+ module AjaxModalRails::Controller
2
+ # This module sets up controllers to load their content in a modal
3
+ # in response to pjax requests. See pjax-modals.js.coffee for client side details
4
+ #
5
+ # calling render will render a template to the modal
6
+ # and calling redirect_to will trigger a redirect on the underlying page
7
+ #
8
+ # note that inbound links should have `data-loads-in-pjax-modal` attributes
9
+ # and forms should have `data-submits-to-pjax-modal`
10
+
11
+ extend ActiveSupport::Concern
12
+
13
+ included do
14
+ layout ->(c) { ajax_modal_request? ? ajax_modal_layout : nil }
15
+
16
+ def form_html_options
17
+ Hash.new.tap do |result|
18
+ result['data-submits-to-pjax-modal'] = true if ajax_modal_request?
19
+ end
20
+ end
21
+ helper_method :form_html_options
22
+
23
+ def redirect_to_with_xhr_redirect(*args)
24
+ if ajax_modal_request?
25
+ flash.merge! args.last if args.length > 1
26
+ render "ajax_modal_rails/redirect_via_js", layout: ajax_modal_layout, locals: {redirect: url_for(args.first)}
27
+ else
28
+ redirect_to_without_xhr_redirect(*args)
29
+ end
30
+ end
31
+ alias_method :redirect_to_without_xhr_redirect, :redirect_to
32
+ alias_method :redirect_to, :redirect_to_with_xhr_redirect
33
+
34
+ end
35
+
36
+ private
37
+
38
+ def ajax_modal_layout
39
+ 'ajax_modal_rails/content'
40
+ end
41
+
42
+ def ajax_modal_request?
43
+ # TODO implement w/ special request header just catch all xhr for now
44
+ # can always override in controller if necessary
45
+ # request.env['HTTP_X_AJAX_MODAL'].present?
46
+ request.xhr?
47
+ end
48
+
49
+ end
@@ -0,0 +1,22 @@
1
+ <div class="modal fade" id="ajax-modal" data-ajax-modal tabindex="-1" role="dialog">
2
+ <div class="modal-dialog modal-lg" role="document">
3
+ <div class="modal-content" data-ajax-modal-content>
4
+ <div class="modal-header">
5
+ <button class="close" aria-label="Close" data-ajax-modal-close type="button">
6
+ &times;
7
+ </button>
8
+ <h4 class="modal-title" data-ajax-modal-title></h4>
9
+ </div>
10
+ <div class="modal-body clearfix">
11
+ <div data-ajax-modal-loading>
12
+ Loading&hellip;
13
+ </div>
14
+ </div>
15
+ </div>
16
+ </div>
17
+ </div>
18
+
19
+
20
+ <script>
21
+ new AjaxModal()
22
+ </script>
@@ -0,0 +1,4 @@
1
+ <script>
2
+ window.location.href = '<%= redirect %>'
3
+ </script>
4
+
@@ -0,0 +1,23 @@
1
+ <div class="modal-header">
2
+ <h4 class="modal-title" data-ajax-modal-title>
3
+ <%= yield :modal_title %>
4
+ </h4>
5
+ <button class="close" aria-label="Close" data-ajax-modal-close type="button">
6
+ &times;
7
+ </button>
8
+ </div>
9
+ <div class="modal-body clearfix">
10
+ <div data-ajax-modal-body>
11
+ <%= yield %>
12
+ </div>
13
+ <div data-ajax-modal-loading style="display: none;">
14
+ Loading&hellip;
15
+ </div>
16
+ </div>
17
+ <% if content_for?(:modal_footer) %>
18
+ <div class="modal-footer" data-pjax-modal-footer>
19
+ <%= yield :modal_footer %>
20
+ </div>
21
+ <% end %>
22
+
23
+ <%= yield :page_js %>
data/config/routes.rb ADDED
@@ -0,0 +1,2 @@
1
+ Rails.application.routes.draw do
2
+ end
@@ -0,0 +1,8 @@
1
+ module AjaxModalRails
2
+ class Engine < ::Rails::Engine
3
+
4
+ config.generators do |g|
5
+ g.test_framework :rspec
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,3 @@
1
+ module AjaxModalRails
2
+ VERSION = '1.0.1'
3
+ end
@@ -0,0 +1,5 @@
1
+ require "ajax_modal_rails/engine"
2
+
3
+ module AjaxModalRails
4
+ # Your code goes here...
5
+ end
@@ -0,0 +1,5 @@
1
+ Description:
2
+ Copy AjaxModalRails files into your application for customization
3
+
4
+ Example:
5
+ rails generate ajax_modal_rails:customize
@@ -0,0 +1,31 @@
1
+ class AjaxModalRails::CustomizeGenerator < Rails::Generators::Base
2
+
3
+ class_option :skip_controllers, type: 'boolean', default: false, desc: "Don't copy controller mixin"
4
+ class_option :skip_views, type: 'boolean', default: false, desc: "Don't copy views"
5
+ class_option :skip_javascripts, type: 'boolean', default: false, desc: "Don't copy javascripts"
6
+
7
+
8
+ source_root File.expand_path('../../../../../app', __FILE__)
9
+
10
+ def copy!
11
+ copy_controllers unless options.skip_controllers?
12
+ copy_views unless options.skip_views?
13
+ copy_javascripts unless options.skip_javascripts?
14
+ end
15
+
16
+ private
17
+
18
+ def copy_controllers
19
+ directory 'controllers/ajax_modal_rails', 'app/controllers/ajax_modal_rails'
20
+ end
21
+
22
+ def copy_views
23
+ directory 'views/ajax_modal_rails', 'app/views/ajax_modal_rails'
24
+ end
25
+
26
+ def copy_javascripts
27
+ directory 'assets/javascripts/ajax_modal_rails', 'app/assets/javascripts/ajax_modal_rails'
28
+ end
29
+
30
+
31
+ end
@@ -0,0 +1,5 @@
1
+ Description:
2
+ Add ajax_modal_rails to the application layout
3
+
4
+ Example:
5
+ rails generate ajax_modal_rails:install
@@ -0,0 +1,42 @@
1
+ class AjaxModalRails::InstallGenerator < Rails::Generators::Base
2
+
3
+ def self.default_layout
4
+ begin
5
+ Haml
6
+ return find_default_haml_layout
7
+ rescue NameError
8
+ return 'app/views/layouts/application.html.erb'
9
+ end
10
+ end
11
+
12
+ class_option :skip_layout, type: 'boolean', default: false, desc: "Don't modify application layout"
13
+ class_option :skip_javascript, type: 'boolean', default: false, desc: "Don't modify application javascript"
14
+ class_option :layout, type: 'string', default: default_layout, desc: 'Specify a custom layout'
15
+ class_option :javascript, type: 'string', default: 'app/views/layouts/application.js', desc: 'Specify a custom javascript'
16
+
17
+
18
+ def install
19
+ install_layout unless options.skip_layout?
20
+ install_javascript unless options.skip_javascript?
21
+ end
22
+
23
+ private
24
+
25
+ def install_layout
26
+ begin
27
+ Haml
28
+ append_to_file options.layout, File.read(File.expand_path('../templates/render_ajax_modal_frame.html.haml', __FILE__))
29
+ rescue NameError
30
+ insert_into_file options.layout, File.read(File.expand_path('../templates/render_ajax_modal_frame.html.erb', __FILE__)), before: '</body>'
31
+ end
32
+ end
33
+
34
+ def install_javascript
35
+ append_to_file 'app/assets/javascripts/application.js', '//= require ajax_modal_rails'
36
+ end
37
+
38
+ def self.find_default_haml_layout
39
+ Dir.glob('app/views/layouts/application{.html,}.haml').first
40
+ end
41
+
42
+ end
@@ -0,0 +1 @@
1
+ <%= render 'ajax_modal_rails/frame' %>
@@ -0,0 +1 @@
1
+ = render 'ajax_modal_rails/frame'
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :ajax_modal_rails do
3
+ # # Task goes here
4
+ # end
metadata ADDED
@@ -0,0 +1,176 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ajax_modal_rails
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Rafe Rosen
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-09-27 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 5.1.4
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 5.1.4
27
+ - !ruby/object:Gem::Dependency
28
+ name: coffee-rails
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: sass-rails
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '5.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '5.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: turbolinks
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '5'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '5'
69
+ - !ruby/object:Gem::Dependency
70
+ name: jquery-rails
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: bootstrap
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: 4.0.0.beta
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: 4.0.0.beta
97
+ - !ruby/object:Gem::Dependency
98
+ name: rspec-rails
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '3.6'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '3.6'
111
+ - !ruby/object:Gem::Dependency
112
+ name: sqlite3
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ description:
126
+ email:
127
+ - existentialmutt@gmail.com
128
+ executables: []
129
+ extensions: []
130
+ extra_rdoc_files: []
131
+ files:
132
+ - MIT-LICENSE
133
+ - README.md
134
+ - Rakefile
135
+ - app/assets/config/ajax_modal_rails_manifest.js
136
+ - app/assets/javascripts/ajax_modal_rails/index.coffee
137
+ - app/controllers/ajax_modal_rails/controller.rb
138
+ - app/views/ajax_modal_rails/_frame.html.erb
139
+ - app/views/ajax_modal_rails/redirect_via_js.html.erb
140
+ - app/views/layouts/ajax_modal_rails/content.html.erb
141
+ - config/routes.rb
142
+ - lib/ajax_modal_rails.rb
143
+ - lib/ajax_modal_rails/engine.rb
144
+ - lib/ajax_modal_rails/version.rb
145
+ - lib/generators/ajax_modal_rails/customize/USAGE
146
+ - lib/generators/ajax_modal_rails/customize/customize_generator.rb
147
+ - lib/generators/ajax_modal_rails/install/USAGE
148
+ - lib/generators/ajax_modal_rails/install/install_generator.rb
149
+ - lib/generators/ajax_modal_rails/install/templates/render_ajax_modal_frame.html.erb
150
+ - lib/generators/ajax_modal_rails/install/templates/render_ajax_modal_frame.html.haml
151
+ - lib/tasks/ajax_modal_rails_tasks.rake
152
+ homepage: http://github.com/existentialmutt/ajax_modal_rails
153
+ licenses:
154
+ - MIT
155
+ metadata: {}
156
+ post_install_message:
157
+ rdoc_options: []
158
+ require_paths:
159
+ - lib
160
+ required_ruby_version: !ruby/object:Gem::Requirement
161
+ requirements:
162
+ - - ">="
163
+ - !ruby/object:Gem::Version
164
+ version: '0'
165
+ required_rubygems_version: !ruby/object:Gem::Requirement
166
+ requirements:
167
+ - - ">="
168
+ - !ruby/object:Gem::Version
169
+ version: '0'
170
+ requirements: []
171
+ rubyforge_project:
172
+ rubygems_version: 2.6.11
173
+ signing_key:
174
+ specification_version: 4
175
+ summary: simple ajax-driven modals for rails
176
+ test_files: []