ajax_error_renderer 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 958d956880a60a471ed3beca4298c61b36c2ff18dd214df644e7903ad0021508
4
+ data.tar.gz: e800de870af5c1236d08df7fd03a4e54d8d3989faebd605d5148896c0598bfc7
5
+ SHA512:
6
+ metadata.gz: 0e9d4ec34b94ee4696e22f7d217b9f3a2b8958a192155784bda7ee6ae4890e9542b71f0a8c8bfcd94085ff023b7bbd5f97a72250526e2266c2cb95a11c61299a
7
+ data.tar.gz: b0b7e3ba2393a731fa7fa0f45a8ee087b36b566a18434eb856b69b999dab046afaefc0d6e763922dfd35b130f5b113c1b4a01bca1a011d3cde1d5cd96b64b8dd
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2018 willnet
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,90 @@
1
+ # AjaxErrorRenderer
2
+
3
+ [![CircleCI](https://circleci.com/gh/willnet/ajax_error_renderer/tree/master.svg?style=svg)](https://circleci.com/gh/willnet/ajax_error_renderer/tree/master)
4
+
5
+ AjaxErrorRenderer is a validation error renderer for ajax request.
6
+
7
+ If you use turbolinks and form_with, you may need SJR(Server-generated JavaScript Responses) on validation errors. But it's a pain to add similar SJR code with every forms. AjaxErrorRenderer gives you render_ajax_error method to remove duplicated SJR.
8
+
9
+ ## Installation
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ ```ruby
14
+ gem 'ajax_error_renderer'
15
+ ```
16
+
17
+ And then execute:
18
+
19
+ ```bash
20
+ $ bundle
21
+ ```
22
+
23
+ ## Usage
24
+
25
+ First, `include AjaxErrorRenderer` in your controller like following.
26
+
27
+ ```ruby
28
+ class ApplicationController < ActionController::Base
29
+ include AjaxErrorRenderer
30
+ end
31
+ ```
32
+
33
+ Then you can use render_ajax_error method like following.
34
+
35
+ ```ruby
36
+ class UsersController < ApplicationController
37
+ def new
38
+ @user = User.new
39
+ end
40
+
41
+ def create
42
+ @user = User.new(params.require(:user).permit(:name))
43
+ if @user.save
44
+ redirect_to users_path, notice: 'You created a user successfully!'
45
+ else
46
+ render_ajax_error model: @user
47
+ end
48
+ end
49
+ end
50
+ ```
51
+
52
+ So error messages display within `#error` by default.
53
+
54
+ ```erb
55
+ <%= form_with(model: @user) do |form| %>
56
+ <div id="error">
57
+ <%# Error Messages display here! %>
58
+ </div>
59
+
60
+ <div class="field">
61
+ <%= form.label :name %>
62
+ <%= form.text_field :name %>
63
+ </div>
64
+
65
+ <div class="actions">
66
+ <%= form.submit %>
67
+ </div>
68
+ <% end %>
69
+ ```
70
+
71
+ If you want to display the messages in other place, you can do it by passing `location` option to `render_ajax_error` with css like following.
72
+
73
+
74
+ ```ruby
75
+ render_ajax_error model: @user, location: '.error_messages'
76
+ ```
77
+
78
+ ### Custom Template
79
+
80
+ You can create custome template with a generator like following.
81
+
82
+ ```sh
83
+ ./bin/rails g ajax_error_renderer:view
84
+ ```
85
+
86
+ This generator command generates `app/views/ajax_errors/_error.html.erb`, and you can customize it freely.
87
+
88
+ ## License
89
+
90
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,32 @@
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 = 'AjaxErrorRenderer'
12
+ rdoc.options << '--line-numbers'
13
+ rdoc.rdoc_files.include('README.md')
14
+ rdoc.rdoc_files.include('lib/**/*.rb')
15
+ end
16
+
17
+ APP_RAKEFILE = File.expand_path("test/dummy/Rakefile", __dir__)
18
+ load 'rails/tasks/engine.rake'
19
+
20
+ load 'rails/tasks/statistics.rake'
21
+
22
+ require 'bundler/gem_tasks'
23
+
24
+ require 'rake/testtask'
25
+
26
+ Rake::TestTask.new(:test) do |t|
27
+ t.libs << 'test'
28
+ t.pattern = 'test/**/*_test.rb'
29
+ t.verbose = false
30
+ end
31
+
32
+ task default: :test
@@ -0,0 +1,9 @@
1
+ <% if model.errors.any? %>
2
+ <div id="error_explanation" class="alert alert-danger">
3
+ <ul>
4
+ <% model.errors.full_messages.each do |msg| %>
5
+ <li><%= msg %></li>
6
+ <% end %>
7
+ </ul>
8
+ </div>
9
+ <% end %>
@@ -0,0 +1 @@
1
+ document.querySelector('<%= @location %>').innerHTML = "<%= j(render('ajax_errors/error', model: @model)) %>";
@@ -0,0 +1,9 @@
1
+ require "ajax_error_renderer/engine"
2
+
3
+ module AjaxErrorRenderer
4
+ def render_ajax_error(location: '#error', model:)
5
+ @location = location
6
+ @model = model
7
+ render 'ajax_errors/error', formats: :js
8
+ end
9
+ end
@@ -0,0 +1,4 @@
1
+ module AjaxErrorRenderer
2
+ class Engine < ::Rails::Engine
3
+ end
4
+ end
@@ -0,0 +1,3 @@
1
+ module AjaxErrorRenderer
2
+ VERSION = '0.1.0'
3
+ end
@@ -0,0 +1,8 @@
1
+ Description:
2
+ Generate custom error template for AjaxErrorRenderer
3
+
4
+ Example:
5
+ rails generate ajax_error_renderer:view
6
+
7
+ This will create:
8
+ app/views/ajax_errors/_error.html.erb
@@ -0,0 +1,9 @@
1
+ <% if model.errors.any? %>
2
+ <div id="error_explanation" class="alert alert-danger">
3
+ <ul>
4
+ <% model.errors.full_messages.each do |msg| %>
5
+ <li><%= msg %></li>
6
+ <% end %>
7
+ </ul>
8
+ </div>
9
+ <% end %>
@@ -0,0 +1,11 @@
1
+ module AjaxErrorRenderer
2
+ class ViewGenerator < Rails::Generators::Base
3
+ source_root File.expand_path('templates', __dir__)
4
+
5
+ def copy_view_file
6
+ directory = Rails.root.join('app', 'views', 'ajax_errors').to_s
7
+ Dir.mkdir(directory) unless Dir.exist?(directory)
8
+ copy_file 'error.html.erb', File.join(directory, '_error.html.erb')
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :ajax_error_renderer do
3
+ # # Task goes here
4
+ # end
metadata ADDED
@@ -0,0 +1,70 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ajax_error_renderer
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - willnet
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-11-29 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.2.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 5.2.0
27
+ description: validation error renderer for ajax request
28
+ email:
29
+ - netwillnet@gmail.com
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - MIT-LICENSE
35
+ - README.md
36
+ - Rakefile
37
+ - app/views/ajax_errors/_error.html.erb
38
+ - app/views/ajax_errors/error.js.erb
39
+ - lib/ajax_error_renderer.rb
40
+ - lib/ajax_error_renderer/engine.rb
41
+ - lib/ajax_error_renderer/version.rb
42
+ - lib/generators/ajax_error_renderer/USAGE
43
+ - lib/generators/ajax_error_renderer/templates/error.html.erb
44
+ - lib/generators/ajax_error_renderer/view_generator.rb
45
+ - lib/tasks/ajax_error_renderer_tasks.rake
46
+ homepage: https://github.com/willnet/ajax_error_renderer
47
+ licenses:
48
+ - MIT
49
+ metadata: {}
50
+ post_install_message:
51
+ rdoc_options: []
52
+ require_paths:
53
+ - lib
54
+ required_ruby_version: !ruby/object:Gem::Requirement
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ version: '0'
59
+ required_rubygems_version: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ version: '0'
64
+ requirements: []
65
+ rubyforge_project:
66
+ rubygems_version: 2.7.6
67
+ signing_key:
68
+ specification_version: 4
69
+ summary: a friend with turbolinks and form_with
70
+ test_files: []