From a7c927cb79e80c6cbb3c44b8117ee0e9eef1b119 Mon Sep 17 00:00:00 2001 From: Xavier Date: Thu, 6 Apr 2017 11:42:18 +0200 Subject: [PATCH 1/2] Add marsman/boostrap/default styles for simple form --- recipes/simple_form.rb | 36 +++++++++++++++++++++++++++-- recipes/simple_form/en.yml | 20 +++++++++++++++++ recipes/simple_form/marsman.rb | 41 ++++++++++++++++++++++++++++++++++ 3 files changed, 95 insertions(+), 2 deletions(-) create mode 100644 recipes/simple_form/en.yml create mode 100644 recipes/simple_form/marsman.rb diff --git a/recipes/simple_form.rb b/recipes/simple_form.rb index 05d18b9..f64a06f 100644 --- a/recipes/simple_form.rb +++ b/recipes/simple_form.rb @@ -2,12 +2,44 @@ module Recipes class SimpleForm < Base def gems - # Forms @template.gem 'simple_form' end def init_file - @template.generate 'simple_form:install' + run_generators + add_sample_i18n + end + + private + + def run_generators + case ask_framework_for_forms + when 'marsman' + @template.initializer 'simple_form.rb', File.read(simple_form_marsman_template) + when 'bootstrap' + @template.generate 'simple_form:install --bootstrap' + else + @template.generate 'simple_form:install' + end + end + + def add_sample_i18n + @template.run 'rm config/locales/simple_form.en.yml' + @template.append_to_file 'config/locales/en.yml', + File.read(simple_form_locale_sample) + end + + def ask_framework_for_forms + @template.ask('What framework do you want to use for your forms?', + limited_to: %w(marsman bootstrap default)) + end + + def simple_form_marsman_template + File.join(File.dirname(__FILE__), 'simple_form', 'marsman.rb') + end + + def simple_form_locale_sample + File.join(File.dirname(__FILE__), 'simple_form', 'en.yml') end end end diff --git a/recipes/simple_form/en.yml b/recipes/simple_form/en.yml new file mode 100644 index 0000000..8aa57c8 --- /dev/null +++ b/recipes/simple_form/en.yml @@ -0,0 +1,20 @@ + ### Simple form examples + # simple_form: + # placeholders: + # user: + # email: 'david.bowie@marsbased.com' + # labels: + # defaults: + # email: 'Email' + # user: + # email: 'Your email' + # options: + # user: + # role: + # admin: 'Administrator' + # prompts: + # user: + # role: 'Select a Role' + # hints: + # user: + # email: 'Must be a valid email' diff --git a/recipes/simple_form/marsman.rb b/recipes/simple_form/marsman.rb new file mode 100644 index 0000000..15dba7b --- /dev/null +++ b/recipes/simple_form/marsman.rb @@ -0,0 +1,41 @@ +SimpleForm.setup do |config| + config.wrappers :default, tag: 'div', + class: 'form-group', + error_class: :warning do |b| + b.use :html5 + b.use :placeholder + b.use :maxlength + b.optional :pattern + b.optional :min_max + b.optional :readonly + + b.use :label_input + b.use :hint, wrap_with: { tag: :div, class: 'info-field' } + b.use :error, wrap_with: { tag: :div, class: 'info-field' } + end + + config.wrappers :inline_radio_and_checkbox, tag: 'div', + class: 'form-group', + error_class: 'warning' do |b| + b.use :html5 + b.optional :readonly + + b.use :label + b.use :input + end + + config.default_wrapper = :default + config.wrapper_mappings = { + check_boxes: :inline_radio_and_checkbox, + radio_buttons: :inline_radio_and_checkbox + } + + config.item_wrapper_tag = false + config.boolean_style = :inline + config.button_class = 'btn' + config.error_notification_tag = :div + config.error_notification_class = 'error_notification' + config.label_text = lambda { |label, _required, _explicit_label| label } + config.browser_validations = false + config.boolean_label_class = 'checkbox' +end From c9083301385e68a6f86ee214280ef2919ec5fbe7 Mon Sep 17 00:00:00 2001 From: Xavier Date: Thu, 6 Apr 2017 13:26:28 +0200 Subject: [PATCH 2/2] Apply code style fixes --- recipes/simple_form.rb | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/recipes/simple_form.rb b/recipes/simple_form.rb index f64a06f..3a46b58 100644 --- a/recipes/simple_form.rb +++ b/recipes/simple_form.rb @@ -15,7 +15,7 @@ def init_file def run_generators case ask_framework_for_forms when 'marsman' - @template.initializer 'simple_form.rb', File.read(simple_form_marsman_template) + @template.initializer 'simple_form.rb', simple_form_template('marsman.rb') when 'bootstrap' @template.generate 'simple_form:install --bootstrap' else @@ -25,8 +25,7 @@ def run_generators def add_sample_i18n @template.run 'rm config/locales/simple_form.en.yml' - @template.append_to_file 'config/locales/en.yml', - File.read(simple_form_locale_sample) + @template.append_to_file 'config/locales/en.yml', simple_form_template('en.yml') end def ask_framework_for_forms @@ -34,12 +33,8 @@ def ask_framework_for_forms limited_to: %w(marsman bootstrap default)) end - def simple_form_marsman_template - File.join(File.dirname(__FILE__), 'simple_form', 'marsman.rb') - end - - def simple_form_locale_sample - File.join(File.dirname(__FILE__), 'simple_form', 'en.yml') + def simple_form_template(filename) + File.read(File.join(File.dirname(__FILE__), 'simple_form', filename)) end end end