ajax_error_renderer 0.1.1 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 74c3bea151360e5fd94ff5b4e4cf666dea499628e5a3985af94ad53ac0515aeb
4
- data.tar.gz: 2e4b67917e6e7005117c405fc082f8590c2eb3f70bf1a6bd8c071ac185f0f32e
3
+ metadata.gz: fb95969d3e15f4fb26de26dace8633b060866fc03b3df37dab175a70a05478ee
4
+ data.tar.gz: 0f4f489ec92dc0b6535fa47ac97db4460aa663d3a0daee56776ec3af49f78dd7
5
5
  SHA512:
6
- metadata.gz: 4b5b4e06d20d9544870e894233ca4c1b4e2776f7f14b6cb330b49c209ec42586fbf5cbe8fdf6fa8e9745864a192182d7364571cfb60190b885bc00dce7aff679
7
- data.tar.gz: de11bdf7531d75639ab2ef7e126984727cf5788a2631f445b99780d3f539a1aff1407a71521847afe38ebf13b0bf013efb0439b0dcfbcc9e5b2ea059f8f9ca8a
6
+ metadata.gz: f2dc7bc27ffb17acd7a32abb0aad2c9f24785a4092f8bb20b46aa4cf1bf95c6b15a7f49a04e9eba0bcbc0176428ca7423ca2b63eedf89ecb87ffd01cd14f8e57
7
+ data.tar.gz: 41910fff2e590bae123c5f0f6efbd1576f17945376ea20f5d4a38e41b98d3c0588565eb90b6cecbd9cdd78cfa63193ba4130a5a8d784b59b2623b606c8bdc67c
data/README.md CHANGED
@@ -77,6 +77,16 @@ If you want to display the messages in other place, you can do it by passing `lo
77
77
  render_ajax_error model: @user, location: '.error_messages'
78
78
  ```
79
79
 
80
+ ### Scroll to error messages
81
+
82
+ If you have a large form, users can miss validation error messages because the messages display above user screen. To solve the problem, render_ajax_error method has move option to scroll to error messages. It defaults to true.
83
+
84
+ If you won't scroll, you can write like following.
85
+
86
+ ```ruby
87
+ render_ajax_error model: @user, move: false
88
+ ```
89
+
80
90
  ### Custom Template
81
91
 
82
92
  You can create custome template with a generator like following.
@@ -1 +1,5 @@
1
- document.querySelector('<%= @location %>').innerHTML = "<%= j(render('ajax_errors/error', model: @model)) %>";
1
+ var element = document.querySelector('<%= @location %>');
2
+ element.innerHTML = "<%= j(render('ajax_errors/error', model: @model)) %>";
3
+ <% if @move %>
4
+ element.scrollIntoView(true);
5
+ <% end %>
@@ -0,0 +1,52 @@
1
+ module Reform::Form::MultiParameterAttributes
2
+ # TODO: implement this with parse_filter, so we don't have to manually walk through the hash, etc.
3
+ class DateTimeParamsFilter
4
+ def call(params)
5
+ params = params.dup # DISCUSS: not sure if that slows down form processing?
6
+ date_attributes = {}
7
+
8
+ params.each do |attribute, value|
9
+ if value.is_a?(Hash)
10
+ params[attribute] = call(value) # TODO: #validate should only handle local form params.
11
+ elsif matches = attribute.match(/^(\w+)\(.i\)$/)
12
+ date_attribute = matches[1]
13
+ date_attributes[date_attribute] = params_to_date(
14
+ params.delete("#{date_attribute}(1i)"),
15
+ params.delete("#{date_attribute}(2i)"),
16
+ params.delete("#{date_attribute}(3i)"),
17
+ params.delete("#{date_attribute}(4i)"),
18
+ params.delete("#{date_attribute}(5i)")
19
+ )
20
+ end
21
+ end
22
+
23
+ date_attributes.each do |attribute, date|
24
+ params[attribute] = date
25
+ end
26
+ params
27
+ end
28
+
29
+ private
30
+ def params_to_date(year, month, day, hour, minute)
31
+ date_fields = [year, month, day].map!(&:to_i)
32
+ time_fields = [hour, minute].map!(&:to_i)
33
+
34
+ if date_fields.any?(&:zero?) || !Date.valid_date?(*date_fields)
35
+ return nil
36
+ end
37
+
38
+ if hour.blank? && minute.blank?
39
+ Date.new(*date_fields)
40
+ else
41
+ args = date_fields + time_fields
42
+ Time.zone ? Time.zone.local(*args) :
43
+ Time.new(*args)
44
+ end
45
+ end
46
+ end
47
+
48
+ # this hooks into the format-specific #deserialize! method.
49
+ def deserialize!(params)
50
+ super DateTimeParamsFilter.new.call(params) # if params.is_a?(Hash) # this currently works for hash, only.
51
+ end
52
+ end
@@ -1,9 +1,12 @@
1
1
  require "ajax_error_renderer/engine"
2
2
 
3
3
  module AjaxErrorRenderer
4
- def render_ajax_error(location: '#error', model:, status: 422)
4
+ private
5
+
6
+ def render_ajax_error(location: '#error', model:, status: 422, move: true)
5
7
  @location = location
6
8
  @model = model
9
+ @move = move
7
10
  render 'ajax_errors/error', formats: :js, status: status
8
11
  end
9
12
  end
@@ -1,3 +1,3 @@
1
1
  module AjaxErrorRenderer
2
- VERSION = '0.1.1'
2
+ VERSION = '0.2.0'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ajax_error_renderer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - willnet
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-11-30 00:00:00.000000000 Z
11
+ date: 2019-05-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -36,6 +36,7 @@ files:
36
36
  - Rakefile
37
37
  - app/views/ajax_errors/_error.html.erb
38
38
  - app/views/ajax_errors/error.js.erb
39
+ - app/views/ajax_errors/multi_parameter_attributes.rb
39
40
  - lib/ajax_error_renderer.rb
40
41
  - lib/ajax_error_renderer/engine.rb
41
42
  - lib/ajax_error_renderer/version.rb
@@ -62,8 +63,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
62
63
  - !ruby/object:Gem::Version
63
64
  version: '0'
64
65
  requirements: []
65
- rubyforge_project:
66
- rubygems_version: 2.7.6
66
+ rubygems_version: 3.0.3
67
67
  signing_key:
68
68
  specification_version: 4
69
69
  summary: a friend with turbolinks and form_with