Skip to content

Commit 973db8f

Browse files
committed
#5 Convert forms to simple_form
1 parent 27932fe commit 973db8f

File tree

7 files changed

+216
-32
lines changed

7 files changed

+216
-32
lines changed

Gemfile.lock

+8-4
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@ PATH
22
remote: .
33
specs:
44
kebapage (1.0.0)
5-
bootstrap-wysihtml5-rails
5+
bootstrap-wysihtml5-rails (~> 0.3.1.23)
66
dropzonejs-rails (= 0.4.12)
7-
friendly_id
7+
friendly_id (~> 5.0.2)
88
haml (~> 4.0.2)
9-
haml-rails
9+
haml-rails (~> 0.5.3)
1010
i18n (~> 0.6.9)
1111
rails (~> 4.0.2)
1212
rails-i18n (~> 4.0.1)
13+
simple_form (~> 3.0.1)
1314

1415
GEM
1516
remote: https://rubygems.org/
@@ -84,6 +85,9 @@ GEM
8485
rake (>= 0.8.7)
8586
thor (>= 0.18.1, < 2.0)
8687
rake (10.1.1)
88+
simple_form (3.0.1)
89+
actionpack (>= 4.0.0, < 4.1)
90+
activemodel (>= 4.0.0, < 4.1)
8791
sprockets (2.10.1)
8892
hike (~> 1.2)
8993
multi_json (~> 1.0)
@@ -109,4 +113,4 @@ PLATFORMS
109113
DEPENDENCIES
110114
dropzonejs-rails (= 0.4.12)
111115
kebapage!
112-
sqlite3
116+
sqlite3 (~> 1.3.8)

app/views/kebapage/media/index.html.haml

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
=stylesheet_link_tag 'kebapage/application'
22

33
.form-group
4-
=form_for @medium, multipart: true, html: { class: :dropzone, id: 'media-dropzone' } do |f|
5-
=f.hidden_field :attachment
4+
=simple_form_for @medium, multipart: true, html: { class: :dropzone, id: 'media-dropzone' } do |f|
5+
=f.input :attachment, as: :hidden
66

77
.panel.panel-default.grid
88
.panel-heading
@@ -27,11 +27,11 @@
2727
%td
2828
= media.attachment_content_type
2929
%td
30-
= media.attachment_file_size.to_i / 1024
30+
= "#{(media.attachment_file_size.to_i / 1024)} KB"
3131
%td
3232
= media.attachment_updated_at
3333
%td.action
34-
%a.btn.btn-success{href: '#'}
34+
%a.btn.btn-success{href: media.attachment.url }
3535
%i.icon-zoom-in
3636

3737
=javascript_include_tag 'kebapage/application'
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,20 @@
1-
=stylesheet_link_tag "kebapage/application"
1+
=stylesheet_link_tag 'kebapage/application'
22

33
.panel.panel-default
44
.panel-heading
55
%i.icon-edit.icon-large
66
Form
77
.panel-body
88

9-
=form_for @static_page do |f|
9+
=simple_form_for @static_page do |f|
1010
.form-group
11-
= f.label I18n.t('kebapage.title'), class: 'control-label'
12-
= f.text_field :title, class: 'form-control', placeholder: I18n.t('kebapage.title_placeholder')
11+
= f.input :title, label: I18n.t('kebapage.title'), placeholder: I18n.t('kebapage.title_placeholder')
1312

1413
.form-group
15-
= f.label I18n.t('kebapage.content'), class: 'control-label'
16-
= f.text_area :content, class: 'form-control wysihtml5', placeholder: I18n.t('kebapage.content_placeholder'), rows: 15
14+
= f.input :content, input_html: {class: 'wysihtml5', rows: 15}, label: I18n.t('kebapage.content'), placeholder: I18n.t('kebapage.content_placeholder')
1715

1816
.form-actions
1917
%button.btn.btn-default{type: 'submit'} #{I18n.t('kebapage.submit')}
2018
%a.btn{href: static_pages_path} #{I18n.t('kebapage.cancel')}
2119

22-
=form_for @medium, multipart: true, html: { class: :dropzone, id: 'media-dropzone' } do |f|
23-
=f.hidden_field :attachment
24-
25-
=javascript_include_tag "kebapage/application"
20+
=javascript_include_tag 'kebapage/application'

config/initializers/simple_form.rb

+145
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
# Use this setup block to configure all options available in SimpleForm.
2+
SimpleForm.setup do |config|
3+
# Wrappers are used by the form builder to generate a
4+
# complete input. You can remove any component from the
5+
# wrapper, change the order or even add your own to the
6+
# stack. The options given below are used to wrap the
7+
# whole input.
8+
config.wrappers :default, class: :input,
9+
hint_class: :field_with_hint, error_class: :field_with_errors do |b|
10+
## Extensions enabled by default
11+
# Any of these extensions can be disabled for a
12+
# given input by passing: `f.input EXTENSION_NAME => false`.
13+
# You can make any of these extensions optional by
14+
# renaming `b.use` to `b.optional`.
15+
16+
# Determines whether to use HTML5 (:email, :url, ...)
17+
# and required attributes
18+
b.use :html5
19+
20+
# Calculates placeholders automatically from I18n
21+
# You can also pass a string as f.input placeholder: "Placeholder"
22+
b.use :placeholder
23+
24+
## Optional extensions
25+
# They are disabled unless you pass `f.input EXTENSION_NAME => :lookup`
26+
# to the input. If so, they will retrieve the values from the model
27+
# if any exists. If you want to enable the lookup for any of those
28+
# extensions by default, you can change `b.optional` to `b.use`.
29+
30+
# Calculates maxlength from length validations for string inputs
31+
b.optional :maxlength
32+
33+
# Calculates pattern from format validations for string inputs
34+
b.optional :pattern
35+
36+
# Calculates min and max from length validations for numeric inputs
37+
b.optional :min_max
38+
39+
# Calculates readonly automatically from readonly attributes
40+
b.optional :readonly
41+
42+
## Inputs
43+
b.use :label_input
44+
b.use :hint, wrap_with: { tag: :span, class: :hint }
45+
b.use :error, wrap_with: { tag: :span, class: :error }
46+
end
47+
48+
# The default wrapper to be used by the FormBuilder.
49+
config.default_wrapper = :default
50+
51+
# Define the way to render check boxes / radio buttons with labels.
52+
# Defaults to :nested for bootstrap config.
53+
# inline: input + label
54+
# nested: label > input
55+
config.boolean_style = :nested
56+
57+
# Default class for buttons
58+
config.button_class = 'btn'
59+
60+
# Method used to tidy up errors. Specify any Rails Array method.
61+
# :first lists the first message for each field.
62+
# Use :to_sentence to list all errors for each field.
63+
# config.error_method = :first
64+
65+
# Default tag used for error notification helper.
66+
config.error_notification_tag = :div
67+
68+
# CSS class to add for error notification helper.
69+
config.error_notification_class = 'alert alert-error'
70+
71+
# ID to add for error notification helper.
72+
# config.error_notification_id = nil
73+
74+
# Series of attempts to detect a default label method for collection.
75+
# config.collection_label_methods = [ :to_label, :name, :title, :to_s ]
76+
77+
# Series of attempts to detect a default value method for collection.
78+
# config.collection_value_methods = [ :id, :to_s ]
79+
80+
# You can wrap a collection of radio/check boxes in a pre-defined tag, defaulting to none.
81+
# config.collection_wrapper_tag = nil
82+
83+
# You can define the class to use on all collection wrappers. Defaulting to none.
84+
# config.collection_wrapper_class = nil
85+
86+
# You can wrap each item in a collection of radio/check boxes with a tag,
87+
# defaulting to :span. Please note that when using :boolean_style = :nested,
88+
# SimpleForm will force this option to be a label.
89+
# config.item_wrapper_tag = :span
90+
91+
# You can define a class to use in all item wrappers. Defaulting to none.
92+
# config.item_wrapper_class = nil
93+
94+
# How the label text should be generated altogether with the required text.
95+
# config.label_text = lambda { |label, required| "#{required} #{label}" }
96+
97+
# You can define the class to use on all labels. Default is nil.
98+
config.label_class = 'control-label'
99+
100+
# You can define the class to use on all forms. Default is simple_form.
101+
# config.form_class = :simple_form
102+
103+
# You can define which elements should obtain additional classes
104+
# config.generate_additional_classes_for = [:wrapper, :label, :input]
105+
106+
# Whether attributes are required by default (or not). Default is true.
107+
# config.required_by_default = true
108+
109+
# Tell browsers whether to use the native HTML5 validations (novalidate form option).
110+
# These validations are enabled in SimpleForm's internal config but disabled by default
111+
# in this configuration, which is recommended due to some quirks from different browsers.
112+
# To stop SimpleForm from generating the novalidate option, enabling the HTML5 validations,
113+
# change this configuration to true.
114+
config.browser_validations = false
115+
116+
# Collection of methods to detect if a file type was given.
117+
# config.file_methods = [ :mounted_as, :file?, :public_filename ]
118+
119+
# Custom mappings for input types. This should be a hash containing a regexp
120+
# to match as key, and the input type that will be used when the field name
121+
# matches the regexp as value.
122+
# config.input_mappings = { /count/ => :integer }
123+
124+
# Custom wrappers for input types. This should be a hash containing an input
125+
# type as key and the wrapper that will be used for all inputs with specified type.
126+
# config.wrapper_mappings = { string: :prepend }
127+
128+
# Default priority for time_zone inputs.
129+
# config.time_zone_priority = nil
130+
131+
# Default priority for country inputs.
132+
# config.country_priority = nil
133+
134+
# When false, do not use translations for labels.
135+
# config.translate_labels = true
136+
137+
# Automatically discover new inputs in Rails' autoload path.
138+
# config.inputs_discovery = true
139+
140+
# Cache SimpleForm inputs discovery
141+
# config.cache_discovery = !Rails.env.development?
142+
143+
# Default class for inputs
144+
# config.input_class = nil
145+
end

config/locales/simple_form.en.yml

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
en:
2+
simple_form:
3+
"yes": 'Yes'
4+
"no": 'No'
5+
required:
6+
text: 'required'
7+
mark: '*'
8+
# You can uncomment the line below if you need to overwrite the whole required html.
9+
# When using html, text and mark won't be used.
10+
# html: '<abbr title="required">*</abbr>'
11+
error_notification:
12+
default_message: "Please review the problems below:"
13+
# Labels and hints examples
14+
# labels:
15+
# defaults:
16+
# password: 'Password'
17+
# user:
18+
# new:
19+
# email: 'E-mail to sign in.'
20+
# edit:
21+
# email: 'E-mail.'
22+
# hints:
23+
# defaults:
24+
# username: 'User name to sign in.'
25+
# password: 'No special characters, please.'
26+

kebapage.gemspec

+15-14
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,29 @@
1-
$:.push File.expand_path("../lib", __FILE__)
1+
$:.push File.expand_path('../lib', __FILE__)
22

33
# Maintain your gem's version:
4-
require "kebapage/version"
4+
require 'kebapage/version'
55

66
# Describe your gem and declare its dependencies:
77
Gem::Specification.new do |s|
8-
s.name = "kebapage"
8+
s.name = 'kebapage'
99
s.version = Kebapage::VERSION
1010
s.authors = %w[lab2023]
1111
s.email = %w[info@lab2023.com]
12-
s.homepage = "https://github.com/kebab-project/kebapage"
12+
s.homepage = 'https://github.com/kebab-project/kebapage'
1313
s.summary = "It's a gem that manages static pages of a Cybele Rails application."
14-
s.description = "Kebapage is a static page manager for Cybele initialized Rails applications."
14+
s.description = 'Kebapage is a static page manager for Cybele initialized Rails applications.'
1515

1616
s.files = Dir['app,config,db,lib}/**/*', 'MIT-LICENSE', 'Rakefile', 'README.md']
1717

18-
s.add_dependency "rails", "~> 4.0.2"
19-
s.add_dependency "haml", "~> 4.0.2"
20-
s.add_dependency "rails-i18n", "~> 4.0.1"
21-
s.add_dependency "i18n", "~> 0.6.9"
22-
s.add_dependency "haml-rails"
23-
s.add_dependency "friendly_id"
24-
s.add_dependency "bootstrap-wysihtml5-rails"
25-
s.add_dependency "dropzonejs-rails", "0.4.12"
18+
s.add_dependency 'rails', '~> 4.0.2'
19+
s.add_dependency 'haml', '~> 4.0.2'
20+
s.add_dependency 'rails-i18n', '~> 4.0.1'
21+
s.add_dependency 'i18n', '~> 0.6.9'
22+
s.add_dependency 'haml-rails', '~> 0.5.3'
23+
s.add_dependency 'friendly_id', '~> 5.0.2'
24+
s.add_dependency 'bootstrap-wysihtml5-rails', '~> 0.3.1.23'
25+
s.add_dependency 'dropzonejs-rails', '0.4.12'
26+
s.add_dependency 'simple_form', '~> 3.0.1'
2627

27-
s.add_development_dependency "sqlite3"
28+
s.add_development_dependency 'sqlite3', '~> 1.3.8'
2829
end
+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<%%= simple_form_for(@<%= singular_table_name %>) do |f| %>
2+
<%%= f.error_notification %>
3+
4+
<div class="form-inputs">
5+
<%- attributes.each do |attribute| -%>
6+
<%%= f.<%= attribute.reference? ? :association : :input %> :<%= attribute.name %> %>
7+
<%- end -%>
8+
</div>
9+
10+
<div class="form-actions">
11+
<%%= f.button :submit %>
12+
</div>
13+
<%% end %>

0 commit comments

Comments
 (0)