Skip to content

Commit 52e9988

Browse files
committed
feat: more custom form inputs
1 parent 9717ff5 commit 52e9988

File tree

8 files changed

+79
-17
lines changed

8 files changed

+79
-17
lines changed

app/assets/stylesheets/admin/gentelella-custom.scss

+4
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,7 @@ form.form-horizontal .checkbox > label {
3636
.form-horizontal .checkbox, .form-horizontal .checkbox-inline, .form-horizontal .radio, .form-horizontal .radio-inline {
3737
padding-top: 4px;
3838
}
39+
40+
.icheckbox_flat-green, .iradio_flat-green {
41+
margin-right: 5px;
42+
}

app/helpers/admin_helper.rb

+20-6
Original file line numberDiff line numberDiff line change
@@ -24,19 +24,14 @@ def convert_changes_string(string, hstore_columns: nil)
2424
diffs
2525
end
2626

27-
def collection_for_delete_state
28-
[['Deleted', :only_deleted], ['All', :with_deleted]]
29-
end
30-
3127
def render_admin_sorting_buttons(instance, column: :sort, html_attrs: nil)
3228
scope = instance.class.to_s.split('::').last.underscore
3329
html = [:first, :up, :down, :last, :remove].map do |action|
3430
if action == :remove && instance.try(column).nil?
3531
''
3632
else
3733
html_opts = {
38-
method: :put,
39-
34+
method: :put
4035
}.merge(html_attrs || {})
4136
link_to action.to_s.camelize, send("admin_#{scope}_path", instance, "#{scope}[#{column}]" => action, redirect_to: url_for), html_opts
4237
end
@@ -120,4 +115,23 @@ def render_admin_data_table(data: nil, total: nil, bordered: true, striped: true
120115
}
121116
render partial: 'admin/base/data_table', locals: locals
122117
end
118+
119+
def admin_link_to(text, link, icon: nil, size: nil, color: nil, round: false, **html_opts)
120+
html_opts ||= {}
121+
classes = html_opts[:class].to_s.split(' ')
122+
classes << 'btn'
123+
classes << { l: 'btn-lg', m: 'btn-sm', s: 'btn-xs' }[size.to_sym] if size
124+
classes << 'btn-round' if round
125+
classes << "btn-#{color}" if color
126+
html_opts[:class] = classes.select(&:present?).join(' ')
127+
link_to link, html_opts do
128+
text = "<i class=\"fa fa-#{icon}\"></i>&nbsp;#{text}" if icon
129+
text.html_safe
130+
end
131+
end
132+
133+
def admin_app_button_to(text, link, icon: , **html_opts)
134+
html_opts ||= {}
135+
admin_link_to(text, link, icon: icon, class: "btn-app #{html_opts.delete(:class)}", **html_opts)
136+
end
123137
end

app/inputs/btn_group_input.rb

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class BtnGroupInput < SimpleForm::Inputs::CollectionRadioButtonsInput
2+
3+
# https://github.com/plataformatec/simple_form/blob/master/lib/simple_form/inputs/collection_radio_buttons_input.rb
4+
def input(wrapper_options = nil)
5+
6+
label_method, value_method = detect_collection_methods
7+
8+
template.content_tag(:div, class: 'btn-group', 'data-toggle' => 'buttons') do
9+
collection.map do |item|
10+
value = item.send(value_method)
11+
checked = value.to_s == @builder.object.send(attribute_name).to_s
12+
input_name = "#{@builder.object_name}[#{attribute_name}]"
13+
template.content_tag(:label, class: "btn btn-default #{checked ? 'active' : ''}") do
14+
[
15+
"<input type=\"radio\" name=\"#{input_name}\" value=\"#{value}\" #{checked ? 'checked="yes"' : ''} />",
16+
item.send(label_method)
17+
].join.html_safe
18+
end
19+
end.join.html_safe
20+
end
21+
end
22+
23+
end

app/inputs/is_deleted_input.rb

+1-9
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,5 @@
1-
class IsDeletedInput < SimpleForm::Inputs::CollectionRadioButtonsInput
1+
class IsDeletedInput < BtnGroupInput
22
# TODO: i18n
3-
def input(wrapper_options = nil)
4-
wrapper_options = (wrapper_options || {}).merge(
5-
include_blank: false,
6-
wrapper: :admin_radio
7-
)
8-
@input_type = 'radio_buttons'
9-
super(wrapper_options)
10-
end
113

124
private
135

app/inputs/radios_input.rb

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class RadiosInput < SimpleForm::Inputs::CollectionRadioButtonsInput
2+
3+
def input(wrapper_options = nil)
4+
wrapper_options = (wrapper_options || {}).merge(
5+
include_blank: false,
6+
wrapper: :admin_radios
7+
)
8+
@input_type = 'radio_buttons'
9+
super(wrapper_options)
10+
end
11+
12+
end

app/inputs/select2_input.rb

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Select2Input < SimpleForm::Inputs::CollectionSelectInput
2+
3+
def input_options
4+
super.merge(include_blank: false)
5+
end
6+
7+
private
8+
9+
# TODO: more attrs for select2
10+
def input_html_options
11+
super.merge(class: 'js-select2')
12+
end
13+
end
+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
= admin_link_to 'See more examples', 'https://colorlib.com/polygon/gentelella/', target: '_blank', size: 'l', color: :primary, icon: :reply, round: true, class: 'zzz', rel: 'no-follow' do
2+
3+
h3= 'admin_app_button_to'
4+
= admin_app_button_to 'Go To Google', 'https://google.com', icon: :pencil

config/initializers/simple_form_admin.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
end
3333
end
3434

35-
config.wrappers :admin_radio, tag: :div, class: 'form-group', error_class: 'parsley-error' do |b|
35+
config.wrappers :admin_radios, tag: :div, class: 'form-group', error_class: 'parsley-error' do |b|
3636
b.use :label, class: 'control-label col-md-3 col-sm-3 col-xs-12'
3737
b.wrapper tag: :div, class: 'col-md-9 col-sm-9 col-xs-12' do |ba|
3838
ba.use :input, class: 'flat', wrap_with: { tag: 'div', class: 'radio' }
@@ -42,7 +42,7 @@
4242
config.wrapper_mappings = {
4343
boolean: :admin_boolean,
4444
switch: :admin_boolean_switch,
45-
is_deleted: :admin_radio
45+
radios: :admin_radios
4646
}
4747
end
4848

0 commit comments

Comments
 (0)