bemer-simple_form 0.1.0 → 0.2.0
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 +4 -4
- data/README.md +173 -13
- data/lib/bemer/simple_form.rb +10 -1
- data/lib/bemer/simple_form/builder.rb +3 -2
- data/lib/bemer/simple_form/configuration.rb +4 -2
- data/lib/bemer/simple_form/inputs.rb +20 -8
- data/lib/bemer/simple_form/version.rb +1 -1
- data/spec/action_view/{block_builder_with_a_name_as_a_passed_object_spec.rb → block_builder_with_a_name_as_a_passed_object_spec_old.rb} +8 -0
- data/spec/action_view/helpers/simple_form_spec.rb +154 -0
- data/spec/action_view/simple_form_for_with_a_symbolic_name_as_a_passed_object_spec.rb +86 -0
- data/spec/bemer/simple_form/configuration_spec.rb +4 -0
- data/spec/dummy/config/application.rb +2 -2
- data/spec/dummy/config/initializers/simple_form.rb +1 -2
- data/spec/dummy/config/initializers/sqlite3.rb +1 -1
- data/spec/rails_helper.rb +1 -0
- data/spec/support/active_record.rb +7 -7
- data/spec/support/helpers/simple_form.rb +1 -1
- metadata +26 -23
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d3cbbb059d2eb7325fdcea05c4af9a9346d51dbff2786438aafb04e59cd00c00
|
4
|
+
data.tar.gz: 4fcb1db714851595aac459463f466e34cdca6c2b25ed6ac7b0a625571cfb17dd
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 489518936e9c7d76070c51d04346018f75c9bde67d6f549a8616a4e06123682ab739cc6178f43a5a34597711922c3dd816ef348babf9a2b083ceed51e266aa4c
|
7
|
+
data.tar.gz: 9fed8d2631f1451c059b60b9360c22188737aa43d65a01986af544ad0a255c2502ab63c97c05ed97fe3685db4243178dd6bac3c9ca1aff7dc3b950401a04f875
|
data/README.md
CHANGED
@@ -1,31 +1,191 @@
|
|
1
|
-
#
|
1
|
+
# Использование соглашений из БЭМ методологии в формах Simple Form
|
2
2
|
|
3
|
-
##
|
3
|
+
## Установка
|
4
4
|
|
5
|
-
|
5
|
+
Добавить в `Gemfile`:
|
6
6
|
|
7
7
|
```ruby
|
8
8
|
gem 'bemer-simple_form'
|
9
9
|
```
|
10
10
|
|
11
|
-
|
11
|
+
Выполнить в терминале команду:
|
12
12
|
|
13
13
|
$ bundle
|
14
14
|
|
15
|
-
|
15
|
+
## Настройки
|
16
|
+
### Конфигурация `bemer-simple_form`
|
17
|
+
Параметры конфигурации по умолчанию:
|
18
|
+
```ruby
|
19
|
+
# config/initializers/bemer_simple_form.rb
|
20
|
+
Bemer::SimpleForm.setup do |config|
|
21
|
+
config.bemify_namespaces = %i[error hint input wrapper label]
|
22
|
+
config.element_name_transformer = nil # lambda { |namespace, block, namespaced_elem, initial_elem| ... }
|
23
|
+
config.input_type_modifiers_for_namespaces = %i[input wrapper label]
|
24
|
+
end
|
25
|
+
```
|
26
|
+
|
27
|
+
### Конфигурация `bemer`
|
28
|
+
Все параметры конфигурации использующие по умолчанию досутпны [здесь](https://github.com/vill/bemer/blob/master/docs/%D0%9A%D0%BE%D0%BD%D1%84%D0%B8%D0%B3%D1%83%D1%80%D0%B0%D1%86%D0%B8%D1%8F.md).
|
29
|
+
```ruby
|
30
|
+
# config/initializers/bemer.rb
|
31
|
+
Bemer.setup do |config|
|
32
|
+
config.bem = true
|
33
|
+
config.element_name_separator = '__'
|
34
|
+
config.modifier_name_separator = '--'
|
35
|
+
config.modifier_value_separator = '_'
|
36
|
+
end
|
37
|
+
```
|
38
|
+
|
39
|
+
### Конфигурация `simple_form`
|
40
|
+
Для того, чтобы отключить генерацию ненужных CSS классов от Simple Form, достаточно изменить следующие параметры конфигурации:
|
41
|
+
```ruby
|
42
|
+
# config/initializers/simple_form.rb
|
43
|
+
SimpleForm.setup do |config|
|
44
|
+
config.wrappers :default do |b|
|
45
|
+
# ...
|
46
|
+
|
47
|
+
# Inputs
|
48
|
+
b.use :hint, wrap_with: { tag: :span }
|
49
|
+
b.use :error, wrap_with: { tag: :span }
|
50
|
+
|
51
|
+
# ...
|
52
|
+
end
|
53
|
+
|
54
|
+
# ...
|
55
|
+
|
56
|
+
# DEPRECATED: You can define the class to be used on all forms.
|
57
|
+
config.form_class = nil
|
58
|
+
|
59
|
+
# Default class for buttons
|
60
|
+
config.button_class = nil
|
61
|
+
|
62
|
+
# CSS class to add for error notification helper.
|
63
|
+
config.error_notification_class = nil
|
64
|
+
|
65
|
+
# You can define which elements should obtain additional classes.
|
66
|
+
config.generate_additional_classes_for = []
|
16
67
|
|
17
|
-
|
68
|
+
# Define the default class of the input wrapper of the boolean input.
|
69
|
+
config.boolean_label_class = nil
|
18
70
|
|
19
|
-
|
71
|
+
# Defines if the default input wrapper class should be included in radio collection wrappers.
|
72
|
+
config.include_default_input_wrapper_class = false
|
20
73
|
|
21
|
-
|
74
|
+
# ...
|
75
|
+
end
|
76
|
+
```
|
77
|
+
Дополнительно при вызове можно указать `class: nil`:
|
78
|
+
```slim
|
79
|
+
= simple_form_for resource, url: registration_path(resource_name), html: { class: nil } do |f|
|
80
|
+
/ ...
|
81
|
+
```
|
82
|
+
## Использование
|
83
|
+
|
84
|
+
Для `simple_form_for` доступны такие же параметры как и для [хелпера `block_tag`](https://github.com/vill/bemer/blob/master/docs/%D0%A5%D0%B5%D0%BB%D0%BF%D0%B5%D1%80-block_tag.md).
|
85
|
+
|
86
|
+
Для элементов формы доступны такие же параметры как и для элементов [хелпера `block_tag`](https://github.com/vill/bemer/blob/master/docs/%D0%A5%D0%B5%D0%BB%D0%BF%D0%B5%D1%80-block_tag.md#%D0%BF%D0%B0%D1%80%D0%B0%D0%BC%D0%B5%D1%82%D1%80-content) создаваемых с помощью `content`, переданного в виде Ruby `&block`.
|
87
|
+
|
88
|
+
Создание формы с параметрами по умолчанию:
|
89
|
+
|
90
|
+
```slim
|
91
|
+
= simple_form_for resource, url: registration_path(resource_name), html: { class: nil } do |f|
|
92
|
+
= f.input :email, required: true, autofocus: true
|
93
|
+
= f.input :password, required: true, input_html: { autocomplete: 'off' }
|
94
|
+
= f.input :password_confirmation, required: true, input_html: { autocomplete: 'off' }
|
95
|
+
=> f.button :submit, t('.sign_up')
|
96
|
+
```
|
22
97
|
|
23
|
-
|
98
|
+
Название блока передается через параметр `as`:
|
99
|
+
```slim
|
100
|
+
= simple_form_for resource, as: :user, url: registration_path(resource_name), html: { class: nil } do |f|
|
101
|
+
= f.input :email, required: true, autofocus: true
|
102
|
+
= f.input :password, required: true, input_html: { autocomplete: 'off' }
|
103
|
+
= f.input :password_confirmation, required: true, input_html: { autocomplete: 'off' }
|
104
|
+
= f.button :submit, t('.sign_up')
|
105
|
+
```
|
106
|
+
|
107
|
+
Название блока передается через параметр `block`:
|
108
|
+
```slim
|
109
|
+
= simple_form_for resource, block: :user, url: registration_path(resource_name), html: { class: nil } do |f|
|
110
|
+
= f.input :email, required: true, autofocus: true
|
111
|
+
= f.input :password, required: true, input_html: { autocomplete: 'off' }
|
112
|
+
= f.input :password_confirmation, required: true, input_html: { autocomplete: 'off' }
|
113
|
+
= f.button :submit, t('.sign_up')
|
114
|
+
```
|
24
115
|
|
25
|
-
|
116
|
+
Добавить модификатор `enabled` в элемент `email`:
|
117
|
+
```slim
|
118
|
+
= simple_form_for resource, block: :user, url: registration_path(resource_name), html: { class: nil } do |f|
|
119
|
+
= f.input :email, required: true, autofocus: true, input_html: { mods: :enabled }
|
120
|
+
= f.input :password, required: true, input_html: { autocomplete: 'off' }
|
121
|
+
= f.input :password_confirmation, required: true, input_html: { autocomplete: 'off' }
|
122
|
+
= f.button :submit, t('.sign_up')
|
123
|
+
```
|
124
|
+
|
125
|
+
Результат рендеринга формы с параметрами по умолчанию:
|
126
|
+
```html
|
127
|
+
<form id="new_admin_user" novalidate="novalidate" class="admin-user" action="/user" accept-charset="UTF-8" method="post">
|
128
|
+
<input type="hidden" name="authenticity_token" value="dXgQo5PvtM5g01pFiQmpMDTb8BYVxsMvzS8n+6YN/UhjFR/tCf4ym7bZzMgs/E/ECxvXZcbr9uzMPcUUIj43jA==">
|
129
|
+
<div class="admin-user__email-wrapper admin-user__email-wrapper--email admin-user__email-wrapper--required">
|
130
|
+
<label class="admin-user__email-label admin-user__email-label--email admin-user__email-label--required" for="admin_user_email">
|
131
|
+
<abbr title="Обязательное">*</abbr>
|
132
|
+
Электронный адрес
|
133
|
+
</label>
|
134
|
+
<input autofocus="autofocus" class="admin-user__email admin-user__email--email admin-user__email--required" required="required" aria-required="true" type="email" value="" name="admin_user[email]" id="admin_user_email">
|
135
|
+
</div>
|
136
|
+
<div class="admin-user__password-wrapper admin-user__password-wrapper--password admin-user__password-wrapper--required">
|
137
|
+
<label class="admin-user__password-label admin-user__password-label--password admin-user__password-label--required" for="admin_user_password">
|
138
|
+
<abbr title="Обязательное">*</abbr>
|
139
|
+
Пароль
|
140
|
+
</label>
|
141
|
+
<input autocomplete="off" class="admin-user__password admin-user__password--password admin-user__password--required" required="required" aria-required="true" type="password" name="admin_user[password]" id="admin_user_password">
|
142
|
+
</div>
|
143
|
+
<div class="admin-user__password-confirmation-wrapper admin-user__password-confirmation-wrapper--password admin-user__password-confirmation-wrapper--required">
|
144
|
+
<label class="admin-user__password-confirmation-label admin-user__password-confirmation-label--password admin-user__password-confirmation-label--required" for="admin_user_password_confirmation">
|
145
|
+
<abbr title="Обязательное">*</abbr>
|
146
|
+
Подтверждение пароля
|
147
|
+
</label>
|
148
|
+
<input autocomplete="off" class="admin-user__password-confirmation admin-user__password-confirmation--password admin-user__password-confirmation--required" required="required" aria-required="true" type="password" name="admin_user[password_confirmation]" id="admin_user_password_confirmation">
|
149
|
+
</div>
|
150
|
+
<input type="submit" name="commit" value="Зарегистрироваться" class="admin-user__submit" data-disable-with="Зарегистрироваться">
|
151
|
+
</form>
|
152
|
+
```
|
153
|
+
Результат рендеринга формы содержащей ошибки валидации:
|
154
|
+
```html
|
155
|
+
<form novalidate="novalidate" class="admin-user" action="/user" accept-charset="UTF-8" method="post">
|
156
|
+
<input type="hidden" name="authenticity_token" value="5GJ9rIPD1XVg8BDKy2ZBpDt3qYlBuAh/z7u00qw8bzfyD3LiGdJTILb6hkduk6dQBLeO+pKVPbzOqVY9KA+l8w==">
|
157
|
+
<div class="admin-user__email-wrapper admin-user__email-wrapper--email admin-user__email-wrapper--required">
|
158
|
+
<label class="admin-user__email-label admin-user__email-label--email admin-user__email-label--required" for="admin_user_email">
|
159
|
+
<abbr title="Обязательное">*</abbr>
|
160
|
+
Электронный адрес
|
161
|
+
</label>
|
162
|
+
<input autofocus="autofocus" class="admin-user__email admin-user__email--email admin-user__email--required" required="required" aria-required="true" aria-invalid="true" type="email" value="" name="admin_user[email]" id="admin_user_email">
|
163
|
+
<span class="admin-user__email-error">не может быть пустым</span>
|
164
|
+
</div>
|
165
|
+
<div class="admin-user__password-wrapper admin-user__password-wrapper--password admin-user__password-wrapper--required">
|
166
|
+
<label class="admin-user__password-label admin-user__password-label--password admin-user__password-label--required" for="admin_user_password">
|
167
|
+
<abbr title="Обязательное">*</abbr>
|
168
|
+
Пароль
|
169
|
+
</label>
|
170
|
+
<input autocomplete="off" class="admin-user__password admin-user__password--password admin-user__password--required" required="required" aria-required="true" aria-invalid="true" type="password" name="admin_user[password]" id="admin_user_password">
|
171
|
+
<span class="admin-user__password-error">не может быть пустым</span>
|
172
|
+
</div>
|
173
|
+
<div class="admin-user__password-confirmation-wrapper admin-user__password-confirmation-wrapper--password admin-user__password-confirmation-wrapper--required">
|
174
|
+
<label class="admin-user__password-confirmation-label admin-user__password-confirmation-label--password admin-user__password-confirmation-label--required" for="admin_user_password_confirmation">
|
175
|
+
<abbr title="Обязательное">*</abbr>
|
176
|
+
Подтверждение пароля
|
177
|
+
</label>
|
178
|
+
<input autocomplete="off" class="admin-user__password-confirmation admin-user__password-confirmation--password admin-user__password-confirmation--required" required="required" aria-required="true" type="password" name="admin_user[password_confirmation]" id="admin_user_password_confirmation">
|
179
|
+
</div>
|
180
|
+
<input type="submit" name="commit" value="Зарегистрироваться" class="admin-user__submit" data-disable-with="Зарегистрироваться">
|
181
|
+
</form>
|
182
|
+
```
|
183
|
+
## Ссылки
|
26
184
|
|
27
|
-
|
185
|
+
1. https://ru.bem.info/methodology/
|
186
|
+
1. https://github.com/bem/bem-xjst
|
187
|
+
1. https://github.com/bem/bem-core
|
28
188
|
|
29
|
-
##
|
189
|
+
## Лицензия
|
30
190
|
|
31
|
-
|
191
|
+
Copyright (c) 2019 - 2020 Александр Григорьев. Более подробную информацию о лицензии можно получить в файле [LICENSE-RU.txt](LICENSE-RU.txt).
|
data/lib/bemer/simple_form.rb
CHANGED
@@ -19,7 +19,8 @@ module Bemer
|
|
19
19
|
end
|
20
20
|
|
21
21
|
class << self
|
22
|
-
delegate :bemify_suffix_namespaces, :input_type_modifiers_for_suffix_namespaces,
|
22
|
+
delegate :bemify_suffix_namespaces, :input_type_modifiers_for_suffix_namespaces,
|
23
|
+
:element_name_transformer, to: :config
|
23
24
|
|
24
25
|
def config
|
25
26
|
Bemer::SimpleForm::Configuration.instance
|
@@ -28,6 +29,14 @@ module Bemer
|
|
28
29
|
def setup
|
29
30
|
yield config
|
30
31
|
end
|
32
|
+
|
33
|
+
def transform_element_name(namespace, block, namespaced_elem, initial_elem)
|
34
|
+
return namespaced_elem unless element_name_transformer.respond_to?(:call)
|
35
|
+
|
36
|
+
namespace = namespace.to_s.chomp('_html').to_sym unless namespace.nil?
|
37
|
+
|
38
|
+
element_name_transformer.call(namespace, block, namespaced_elem, initial_elem)
|
39
|
+
end
|
31
40
|
end
|
32
41
|
end
|
33
42
|
end
|
@@ -14,6 +14,7 @@ module Bemer
|
|
14
14
|
def button(type, *args, &block)
|
15
15
|
options = args.extract_options!
|
16
16
|
elem = extract_name_for!(:elem, type, options)
|
17
|
+
elem = Bemer::SimpleForm.transform_element_name(nil, block_entity.block, elem, elem)
|
17
18
|
entity = Bemer::EntityBuilder.new(block_entity.block, elem, extract_bem_options!(options))
|
18
19
|
|
19
20
|
options.delete(:block)
|
@@ -57,7 +58,7 @@ module Bemer
|
|
57
58
|
def extract_name_for!(key, default_name, options)
|
58
59
|
name = options.delete(key)
|
59
60
|
|
60
|
-
name.nil? ? default_name.
|
61
|
+
name.nil? ? default_name.to_sym : name
|
61
62
|
end
|
62
63
|
|
63
64
|
def extract_bem_cascade!(options)
|
@@ -65,7 +66,7 @@ module Bemer
|
|
65
66
|
|
66
67
|
return bem_cascade if block_entity.nil?
|
67
68
|
|
68
|
-
bem_cascade.nil? ? block_entity.bem_cascade : bem_cascade
|
69
|
+
bem_cascade.nil? ? block_entity.instance_variable_get(:@bem_cascade) : bem_cascade
|
69
70
|
end
|
70
71
|
end
|
71
72
|
end
|
@@ -8,10 +8,12 @@ module Bemer
|
|
8
8
|
class Configuration
|
9
9
|
include Singleton
|
10
10
|
|
11
|
-
attr_accessor :bemify_namespaces, :input_type_modifiers_for_namespaces
|
11
|
+
attr_accessor :bemify_namespaces, :input_type_modifiers_for_namespaces,
|
12
|
+
:element_name_transformer
|
12
13
|
|
13
14
|
def initialize
|
14
15
|
@bemify_namespaces = %i[error hint input wrapper label]
|
16
|
+
@element_name_transformer = nil
|
15
17
|
@input_type_modifiers_for_namespaces = %i[input wrapper label]
|
16
18
|
end
|
17
19
|
|
@@ -20,7 +22,7 @@ module Bemer
|
|
20
22
|
end
|
21
23
|
|
22
24
|
def input_type_modifiers_for_suffix_namespaces
|
23
|
-
@input_type_modifiers_for_suffix_namespaces ||= Array.wrap(input_type_modifiers_for_namespaces).uniq.map { |n| add_sufix(n) }
|
25
|
+
@input_type_modifiers_for_suffix_namespaces ||= Array.wrap(input_type_modifiers_for_namespaces).uniq.map { |n| add_sufix(n) } # rubocop:disable Metrics/LineLength
|
24
26
|
end
|
25
27
|
|
26
28
|
protected
|
@@ -1,5 +1,8 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
+
require 'active_support/core_ext/array/wrap'
|
4
|
+
require 'active_support/core_ext/object/blank'
|
5
|
+
|
3
6
|
module Bemer
|
4
7
|
module SimpleForm
|
5
8
|
module Inputs
|
@@ -15,11 +18,13 @@ module Bemer
|
|
15
18
|
@block_entity ||= @options.delete(:block_entity)
|
16
19
|
end
|
17
20
|
|
18
|
-
def bemify_input!
|
21
|
+
def bemify_input! # rubocop:disable Metrics/AbcSize
|
19
22
|
return unless block_entity
|
20
23
|
|
24
|
+
default_name = options.delete(:elem)
|
25
|
+
|
21
26
|
Bemer::SimpleForm.bemify_suffix_namespaces.each do |namespace|
|
22
|
-
elem = extract_elem_name_for!(namespace)
|
27
|
+
elem = extract_elem_name_for!(namespace, default_name)
|
23
28
|
options = extract_bem_options_for!(namespace)
|
24
29
|
|
25
30
|
add_input_type_modifiers!(namespace, options)
|
@@ -30,16 +35,23 @@ module Bemer
|
|
30
35
|
end
|
31
36
|
end
|
32
37
|
|
33
|
-
|
34
|
-
|
35
|
-
elem
|
38
|
+
# rubocop:disable Metrics/LineLength
|
39
|
+
def extract_elem_name_for!(namespace, default_name) # rubocop:disable Metrics/AbcSize
|
40
|
+
elem = bem_options_for(namespace).delete(:elem)
|
36
41
|
|
37
|
-
return elem unless elem.nil?
|
42
|
+
return Bemer::SimpleForm.transform_element_name(namespace, block_entity.block, elem, elem) unless elem.nil?
|
43
|
+
|
44
|
+
elem = default_name.nil? ? reflection_or_attribute_name : default_name
|
45
|
+
|
46
|
+
return Bemer::SimpleForm.transform_element_name(namespace, block_entity.block, elem, elem) if Bemer.bem_class(block_entity.block, elem).blank?
|
38
47
|
|
39
48
|
suffix = namespace.to_s.chomp('_html') unless namespace.eql?(:input_html)
|
40
49
|
|
41
|
-
[
|
50
|
+
namespaced_elem = [elem, suffix].compact.join('_').to_sym
|
51
|
+
|
52
|
+
Bemer::SimpleForm.transform_element_name(namespace, block_entity.block, namespaced_elem, elem)
|
42
53
|
end
|
54
|
+
# rubocop:enable Metrics/LineLength
|
43
55
|
|
44
56
|
def add_input_type_modifiers!(namespace, options)
|
45
57
|
return if Bemer::SimpleForm.input_type_modifiers_for_suffix_namespaces.exclude?(namespace)
|
@@ -68,7 +80,7 @@ module Bemer
|
|
68
80
|
def extract_bem_cascade!(options)
|
69
81
|
bem_cascade = options.delete(:bem_cascade)
|
70
82
|
|
71
|
-
bem_cascade.nil? ? block_entity.bem_cascade : bem_cascade
|
83
|
+
bem_cascade.nil? ? block_entity.instance_variable_get(:@bem_cascade) : bem_cascade
|
72
84
|
end
|
73
85
|
end
|
74
86
|
end
|
@@ -1,6 +1,14 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
RSpec.describe 'block builder with a name as a passed object' do
|
4
|
+
# Можно сделать заглушку и не создавать миграции и т.д. https://github.com/rails/rails/blob/855cb4bef21f3f658b8cd192cfd087bcbe70006a/actionview/test/template/form_helper/form_with_test.rb#L261-L268
|
5
|
+
|
6
|
+
# Не имеет значение что передается в `simple_form_for` первым параметром или через `as`, все преобразовывается в symbol
|
7
|
+
# Поэтому тесты можно объединить в один файл? Для active record и active model сделать только по несколько основных а остальные для symbol?
|
8
|
+
|
9
|
+
# Логика для form_for
|
10
|
+
# https://github.com/rails/rails/blob/eca6c273fe2729b9634907562c2717cf86443b6b/actionview/lib/action_view/helpers/form_helper.rb#L430-L443
|
11
|
+
|
4
12
|
#########################
|
5
13
|
# Покрыть тестами:
|
6
14
|
#########################
|
@@ -0,0 +1,154 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
RSpec.describe 'simple_form_for helper' do
|
4
|
+
it do
|
5
|
+
form = simple_form_for :block do |f|
|
6
|
+
f.input :elem
|
7
|
+
end
|
8
|
+
|
9
|
+
expect(form).to have_tag(:form, with: { class: 'block' }, count: 1) do
|
10
|
+
with_tag :div, with: { class: 'block__elem-wrapper' }, count: 1
|
11
|
+
with_tag :label, with: { class: 'block__elem-label' }, count: 1
|
12
|
+
with_tag :input, with: { class: 'block__elem' }, count: 1
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
it do
|
17
|
+
form = simple_form_for :form do |f|
|
18
|
+
f.button :submit
|
19
|
+
end
|
20
|
+
|
21
|
+
expect(form).to have_tag(:form, with: { class: 'form' }, count: 1) do
|
22
|
+
with_tag :input, with: { class: 'form__submit' }, count: 1
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
it do
|
27
|
+
form = simple_form_for :form do |f|
|
28
|
+
f.button :button
|
29
|
+
end
|
30
|
+
|
31
|
+
expect(form).to have_tag(:form, with: { class: 'form' }, count: 1) do
|
32
|
+
with_tag :button, with: { class: 'form__button' }, count: 1
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
context 'when the element name is passed through input parameters' do
|
37
|
+
it do
|
38
|
+
form = simple_form_for :block do |f|
|
39
|
+
f.input :elem, elem: nil
|
40
|
+
end
|
41
|
+
|
42
|
+
expect(form).to have_tag(:form, with: { class: 'block' }, count: 1) do
|
43
|
+
with_tag :div, with: { class: 'block__elem-wrapper' }, count: 1
|
44
|
+
with_tag :label, with: { class: 'block__elem-label' }, count: 1
|
45
|
+
with_tag :input, with: { class: 'block__elem' }, count: 1
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
it do
|
50
|
+
form = simple_form_for :block do |f|
|
51
|
+
f.input :elem, elem: false
|
52
|
+
end
|
53
|
+
|
54
|
+
expect(form).to have_tag(:form, with: { class: 'block' }, count: 1) do
|
55
|
+
without_tag 'div[class]'
|
56
|
+
without_tag 'label[class]'
|
57
|
+
without_tag 'input[class]'
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
it do
|
62
|
+
form = simple_form_for :block do |f|
|
63
|
+
f.input :elem, elem: ''
|
64
|
+
end
|
65
|
+
|
66
|
+
expect(form).to have_tag(:form, with: { class: 'block' }, count: 1) do
|
67
|
+
without_tag 'div[class]'
|
68
|
+
without_tag 'label[class]'
|
69
|
+
without_tag 'input[class]'
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
it do
|
74
|
+
form = simple_form_for :block do |f|
|
75
|
+
f.input :elem, elem: :elem_name
|
76
|
+
end
|
77
|
+
|
78
|
+
expect(form).to have_tag(:form, with: { class: 'block' }, count: 1) do
|
79
|
+
with_tag :div, with: { class: 'block__elem-name-wrapper' }, count: 1
|
80
|
+
with_tag :label, with: { class: 'block__elem-name-label' }, count: 1
|
81
|
+
with_tag :input, with: { class: 'block__elem-name' }, count: 1
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
it do
|
86
|
+
form = simple_form_for :block do |f|
|
87
|
+
f.input :elem, elem: :elem_name, input_html: { elem: :input }, label_html: { elem: :label }, wrapper_html: { elem: :wrapper }
|
88
|
+
end
|
89
|
+
|
90
|
+
expect(form).to have_tag(:form, with: { class: 'block' }, count: 1) do
|
91
|
+
with_tag :div, with: { class: 'block__wrapper' }, count: 1
|
92
|
+
with_tag :label, with: { class: 'block__label' }, count: 1
|
93
|
+
with_tag :input, with: { class: 'block__input' }, count: 1
|
94
|
+
end
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
context 'when `element_name_transformer` is specified in the configuration' do
|
99
|
+
before do
|
100
|
+
Bemer::SimpleForm.setup do |config|
|
101
|
+
config.element_name_transformer = proc { |namespace, _block, namespaced_elem, initial_elem|
|
102
|
+
elem = initial_elem.to_s
|
103
|
+
|
104
|
+
next namespaced_elem unless elem.end_with?('_id', '_ids') || elem.eql?('submit')
|
105
|
+
|
106
|
+
elem =
|
107
|
+
if elem.end_with?('_ids')
|
108
|
+
elem.chomp('_ids').pluralize
|
109
|
+
elsif elem.end_with?('_id')
|
110
|
+
elem.chomp('_id')
|
111
|
+
else
|
112
|
+
:submit_button
|
113
|
+
end
|
114
|
+
|
115
|
+
namespace.eql?(:input) ? elem.to_sym : [elem, namespace].compact.join('_').to_sym
|
116
|
+
}
|
117
|
+
end
|
118
|
+
end
|
119
|
+
|
120
|
+
it do
|
121
|
+
form = simple_form_for :form do |f|
|
122
|
+
f.input :user_ids
|
123
|
+
end
|
124
|
+
|
125
|
+
expect(form).to have_tag(:form, with: { class: 'form' }, count: 1) do
|
126
|
+
with_tag :div, with: { class: 'form__users-wrapper' }, count: 1
|
127
|
+
with_tag :label, with: { class: 'form__users-label' }, count: 1
|
128
|
+
with_tag :input, with: { class: 'form__users' }, count: 1
|
129
|
+
end
|
130
|
+
end
|
131
|
+
|
132
|
+
it do
|
133
|
+
form = simple_form_for :form do |f|
|
134
|
+
f.input :user_id
|
135
|
+
end
|
136
|
+
|
137
|
+
expect(form).to have_tag(:form, with: { class: 'form' }, count: 1) do
|
138
|
+
with_tag :div, with: { class: 'form__user-wrapper' }, count: 1
|
139
|
+
with_tag :label, with: { class: 'form__user-label' }, count: 1
|
140
|
+
with_tag :input, with: { class: 'form__user' }, count: 1
|
141
|
+
end
|
142
|
+
end
|
143
|
+
|
144
|
+
it do
|
145
|
+
form = simple_form_for :form do |f|
|
146
|
+
f.button :submit
|
147
|
+
end
|
148
|
+
|
149
|
+
expect(form).to have_tag(:form, with: { class: 'form' }, count: 1) do
|
150
|
+
with_tag :input, with: { class: 'form__submit-button' }, count: 1
|
151
|
+
end
|
152
|
+
end
|
153
|
+
end
|
154
|
+
end
|
@@ -0,0 +1,86 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
RSpec.describe 'simple_form_for with a symbolic name as a passed object' do
|
4
|
+
context 'when the `element_name_separator` is set to `___`' do
|
5
|
+
before do
|
6
|
+
Bemer.config.element_name_separator = '___'
|
7
|
+
end
|
8
|
+
|
9
|
+
it do
|
10
|
+
form = simple_form_for :block do |f|
|
11
|
+
f.input :elem
|
12
|
+
end
|
13
|
+
|
14
|
+
expect(form).to have_tag(:form, with: { class: 'block' }, count: 1) do
|
15
|
+
with_tag :input, with: { class: 'block___elem' }, count: 1
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
context 'when the `modifier_name_separator` is set to `--`' do
|
21
|
+
before do
|
22
|
+
Bemer.config.modifier_name_separator = '--'
|
23
|
+
end
|
24
|
+
|
25
|
+
it do
|
26
|
+
form = simple_form_for :block, mods: :enabled do |f|
|
27
|
+
f.input :elem
|
28
|
+
end
|
29
|
+
|
30
|
+
expect(form).to have_tag(:form, with: { class: 'block block--enabled' }, count: 1) do
|
31
|
+
with_tag :input, with: { class: 'block__elem block__elem--string' }, count: 1
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
context 'when the `modifier_value_separator` is set to `___`' do
|
37
|
+
before do
|
38
|
+
Bemer.config.modifier_value_separator = '___'
|
39
|
+
end
|
40
|
+
|
41
|
+
it do
|
42
|
+
form = simple_form_for :block, mods: { size: :large } do |f|
|
43
|
+
f.input :elem, input_html: { mods: { disabled: :yes } }
|
44
|
+
end
|
45
|
+
|
46
|
+
expect(form).to have_tag(:form, with: { class: 'block block_size___large' }, count: 1) do
|
47
|
+
with_tag :input, with: { class: 'block__elem block__elem_disabled___yes' }, count: 1
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
context 'when the `bem` is globally set to `false`' do
|
53
|
+
before do
|
54
|
+
Bemer.config.bem = false
|
55
|
+
end
|
56
|
+
|
57
|
+
it do
|
58
|
+
form = simple_form_for :block do |f|
|
59
|
+
f.input :elem
|
60
|
+
end
|
61
|
+
|
62
|
+
expect(form).to have_tag(:form, count: 1) do
|
63
|
+
without_tag 'form[class]'
|
64
|
+
without_tag 'input[class]'
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
it do
|
70
|
+
form = simple_form_for Article.new, as: 'sss_sss' do |f|
|
71
|
+
# f.input 'elem_name'
|
72
|
+
f.input :title
|
73
|
+
end
|
74
|
+
|
75
|
+
form = simple_form_for 'eferg_adFdb', as: 'sss_sss' do |f|
|
76
|
+
f.input 'elem_name'
|
77
|
+
# f.input :title
|
78
|
+
end
|
79
|
+
|
80
|
+
p form
|
81
|
+
# expect(form).to have_tag(:form, count: 1) do
|
82
|
+
# without_tag 'form[class]'
|
83
|
+
# without_tag 'input[class]'
|
84
|
+
# end
|
85
|
+
end
|
86
|
+
end
|
@@ -10,4 +10,8 @@ RSpec.describe Bemer::SimpleForm::Configuration do
|
|
10
10
|
describe '#input_type_modifiers_for_namespaces' do
|
11
11
|
it { expect(configuration.input_type_modifiers_for_namespaces).to match_array %i[input wrapper label] }
|
12
12
|
end
|
13
|
+
|
14
|
+
describe '#element_name_transformer' do
|
15
|
+
it { expect(configuration.element_name_transformer).to be_nil }
|
16
|
+
end
|
13
17
|
end
|
@@ -13,8 +13,7 @@ SimpleForm.setup do |config|
|
|
13
13
|
# wrapper, change the order or even add your own to the
|
14
14
|
# stack. The options given below are used to wrap the
|
15
15
|
# whole input.
|
16
|
-
config.wrappers :default
|
17
|
-
valid_class: :field_without_errors do |b|
|
16
|
+
config.wrappers :default do |b|
|
18
17
|
## Extensions enabled by default
|
19
18
|
# Any of these extensions can be disabled for a
|
20
19
|
# given input by passing: `f.input EXTENSION_NAME => false`.
|
@@ -1 +1 @@
|
|
1
|
-
Rails.application.config.active_record.sqlite3.represent_boolean_as_integer = true
|
1
|
+
# Rails.application.config.active_record.sqlite3.represent_boolean_as_integer = true
|
data/spec/rails_helper.rb
CHANGED
@@ -1,11 +1,11 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
RSpec.configure do |config|
|
4
|
-
|
5
|
-
|
3
|
+
# RSpec.configure do |config|
|
4
|
+
# config.before(:all) do
|
5
|
+
# ActiveRecord::Migration.verbose = false
|
6
6
|
|
7
|
-
|
7
|
+
# ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: ':memory:')
|
8
8
|
|
9
|
-
|
10
|
-
|
11
|
-
end
|
9
|
+
# load Rails.root.join('db', 'schema.rb')
|
10
|
+
# end
|
11
|
+
# end
|
@@ -3,7 +3,7 @@
|
|
3
3
|
module Helpers
|
4
4
|
module SimpleForm
|
5
5
|
def simple_form_for(record, options = {}, &block)
|
6
|
-
options = { url: '', html: { class:
|
6
|
+
options = { url: '', html: { class: nil } }.deep_merge(options)
|
7
7
|
|
8
8
|
view.simple_form_for(record, options, &(block || proc {}))
|
9
9
|
end
|
metadata
CHANGED
@@ -1,29 +1,29 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: bemer-simple_form
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Alexander Grigorev
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-
|
11
|
+
date: 2020-05-10 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
|
-
name:
|
14
|
+
name: appraisal
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
17
|
- - "~>"
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version:
|
19
|
+
version: 2.2.0
|
20
20
|
type: :development
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
24
|
- - "~>"
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version:
|
26
|
+
version: 2.2.0
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: bundler-audit
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
@@ -66,20 +66,6 @@ dependencies:
|
|
66
66
|
- - "~>"
|
67
67
|
- !ruby/object:Gem::Version
|
68
68
|
version: 2.5.0
|
69
|
-
- !ruby/object:Gem::Dependency
|
70
|
-
name: overcommit
|
71
|
-
requirement: !ruby/object:Gem::Requirement
|
72
|
-
requirements:
|
73
|
-
- - "~>"
|
74
|
-
- !ruby/object:Gem::Version
|
75
|
-
version: 0.48.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.48.0
|
83
69
|
- !ruby/object:Gem::Dependency
|
84
70
|
name: rake
|
85
71
|
requirement: !ruby/object:Gem::Requirement
|
@@ -164,6 +150,20 @@ dependencies:
|
|
164
150
|
- - "~>"
|
165
151
|
- !ruby/object:Gem::Version
|
166
152
|
version: 1.33.0
|
153
|
+
- !ruby/object:Gem::Dependency
|
154
|
+
name: wwtd
|
155
|
+
requirement: !ruby/object:Gem::Requirement
|
156
|
+
requirements:
|
157
|
+
- - "~>"
|
158
|
+
- !ruby/object:Gem::Version
|
159
|
+
version: 1.4.1
|
160
|
+
type: :development
|
161
|
+
prerelease: false
|
162
|
+
version_requirements: !ruby/object:Gem::Requirement
|
163
|
+
requirements:
|
164
|
+
- - "~>"
|
165
|
+
- !ruby/object:Gem::Version
|
166
|
+
version: 1.4.1
|
167
167
|
- !ruby/object:Gem::Dependency
|
168
168
|
name: activesupport
|
169
169
|
requirement: !ruby/object:Gem::Requirement
|
@@ -229,9 +229,11 @@ files:
|
|
229
229
|
- lib/bemer/simple_form/inputs.rb
|
230
230
|
- lib/bemer/simple_form/test/configuration_helpers.rb
|
231
231
|
- lib/bemer/simple_form/version.rb
|
232
|
-
- spec/action_view/
|
232
|
+
- spec/action_view/block_builder_with_a_name_as_a_passed_object_spec_old.rb
|
233
233
|
- spec/action_view/first_spec.rb
|
234
|
+
- spec/action_view/helpers/simple_form_spec.rb
|
234
235
|
- spec/action_view/simple_form_for_as_a_block_builder_spec.rb
|
236
|
+
- spec/action_view/simple_form_for_with_a_symbolic_name_as_a_passed_object_spec.rb
|
235
237
|
- spec/bemer/simple_form/configuration_spec.rb
|
236
238
|
- spec/dummy/Rakefile
|
237
239
|
- spec/dummy/app/forms/search_form.rb
|
@@ -275,15 +277,16 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
275
277
|
- !ruby/object:Gem::Version
|
276
278
|
version: 2.2.0
|
277
279
|
requirements: []
|
278
|
-
|
279
|
-
rubygems_version: 2.7.7
|
280
|
+
rubygems_version: 3.0.3
|
280
281
|
signing_key:
|
281
282
|
specification_version: 4
|
282
283
|
summary: Add the BEM methodology to your SimpleForm forms.
|
283
284
|
test_files:
|
284
|
-
- spec/action_view/
|
285
|
+
- spec/action_view/block_builder_with_a_name_as_a_passed_object_spec_old.rb
|
285
286
|
- spec/action_view/first_spec.rb
|
287
|
+
- spec/action_view/helpers/simple_form_spec.rb
|
286
288
|
- spec/action_view/simple_form_for_as_a_block_builder_spec.rb
|
289
|
+
- spec/action_view/simple_form_for_with_a_symbolic_name_as_a_passed_object_spec.rb
|
287
290
|
- spec/bemer/simple_form/configuration_spec.rb
|
288
291
|
- spec/dummy/app/forms/search_form.rb
|
289
292
|
- spec/dummy/app/models/article.rb
|