diff --git a/Gemfile.lock b/Gemfile.lock index 8faca137..eec0d2f7 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,7 +1,7 @@ PATH remote: . specs: - rolemodel_rails (0.21.0) + rolemodel_rails (0.22.0) GEM remote: https://rubygems.org/ diff --git a/lib/generators/rolemodel/saas/devise/devise_generator.rb b/lib/generators/rolemodel/saas/devise/devise_generator.rb index 2f9afdef..54629258 100644 --- a/lib/generators/rolemodel/saas/devise/devise_generator.rb +++ b/lib/generators/rolemodel/saas/devise/devise_generator.rb @@ -15,7 +15,7 @@ def install_devise # Devise tries to install bcrypt, but it fails because the bundle command # is run using the wrong platform causing the native extension to fail. # Using clean env fixes this. - Bundler.with_clean_env do + Bundler.with_unbundled_env do run 'bundle add devise' end diff --git a/lib/generators/rolemodel/simple_form/simple_form_generator.rb b/lib/generators/rolemodel/simple_form/simple_form_generator.rb index 9e9c443c..60a43701 100644 --- a/lib/generators/rolemodel/simple_form/simple_form_generator.rb +++ b/lib/generators/rolemodel/simple_form/simple_form_generator.rb @@ -6,7 +6,9 @@ class SimpleFormGenerator < Rails::Generators::Base source_root File.expand_path('templates', __dir__) def add_gem - run 'bundle add simple_form' + Bundler.with_unbundled_env do + run 'bundle add simple_form' + end end def add_files diff --git a/lib/generators/rolemodel/simple_form/templates/app/inputs/segmented_control_input.rb b/lib/generators/rolemodel/simple_form/templates/app/inputs/segmented_control_input.rb new file mode 100644 index 00000000..ab19b84e --- /dev/null +++ b/lib/generators/rolemodel/simple_form/templates/app/inputs/segmented_control_input.rb @@ -0,0 +1,50 @@ +# frozen_string_literal: true + +# SegmentedControlInput is a custom input type for SimpleForm that renders a radio group as an optics (v2.2.0+) segmented control +# +# Options: +# :size (:small, :medium, :large) - renders the control at different sizes +# :full_width - renders the control to take the full width of its container +# +# Usage: +# <%= f.input :my_field, as: :segmented_control, collection: collection_options %> +# <%= f.input :my_field, as: :segmented_control, collection: collection_options, size: :small %> +# <%= f.input :my_field, as: :segmented_control, collection: collection_options, disabled: true %> +# <%= f.input :my_field, as: :segmented_control, collection: collection_options, full_width: true %> +class SegmentedControlInput < SimpleForm::Inputs::CollectionRadioButtonsInput + def input_type + 'radio_buttons' + end + + def input_options + options = super + options[:item_wrapper_tag] = false + options[:collection_wrapper_tag] = 'div' + options[:collection_wrapper_class] = collection_wrapper_classes + options[:item_label_class] = 'segmented-control__label' + options + end + + def input_html_options + super.tap do |options| + options[:class].delete('form-control') + end + end + + def input_html_classes + classes = super + classes.push('segmented-control__input') + classes + end + + private + + def collection_wrapper_classes + class_names( + 'segmented-control', + options[:class], + "segmented-control--#{options[:size]}": options[:size].present?, + 'segmented-control--full-width': options[:full_width].present? + ) + end +end diff --git a/lib/generators/rolemodel/simple_form/templates/app/inputs/switch_checkbox_input.rb b/lib/generators/rolemodel/simple_form/templates/app/inputs/switch_checkbox_input.rb index 383eb266..413c13f4 100644 --- a/lib/generators/rolemodel/simple_form/templates/app/inputs/switch_checkbox_input.rb +++ b/lib/generators/rolemodel/simple_form/templates/app/inputs/switch_checkbox_input.rb @@ -1,6 +1,6 @@ # frozen_string_literal: true -# SwitchCheckboxInput is a custom input type for SimpleForm that renders a checkbox as a switch control +# SwitchCheckboxInput is a custom input type for SimpleForm that renders a checkbox as an optics (v0.5.0+) switch control # # Options: # :small - renders a smaller switch control diff --git a/lib/rolemodel_rails/version.rb b/lib/rolemodel_rails/version.rb index 9c255072..0754cd45 100644 --- a/lib/rolemodel_rails/version.rb +++ b/lib/rolemodel_rails/version.rb @@ -1,5 +1,5 @@ # frozen_string_literal: true module RolemodelRails - VERSION = '0.21.0' + VERSION = '0.22.0' end diff --git a/spec/generators/rolemodel/simple_form_generator_spec.rb b/spec/generators/rolemodel/simple_form_generator_spec.rb new file mode 100644 index 00000000..8674a112 --- /dev/null +++ b/spec/generators/rolemodel/simple_form_generator_spec.rb @@ -0,0 +1,18 @@ +require 'spec_helper' + +RSpec.describe Rolemodel::SimpleFormGenerator, type: :generator do + before { run_generator_against_test_app } + + it 'generates a simple form initializer' do + assert_file 'config/initializers/simple_form.rb' + end + + it 'generates custom input files' do + assert_file 'app/inputs/collection_check_boxes_input.rb' + assert_file 'app/inputs/collection_select_input.rb' + assert_file 'app/inputs/grouped_collection_select_input.rb' + assert_file 'app/inputs/segmented_control_input.rb' + assert_file 'app/inputs/switch_checkbox_input.rb' + assert_file 'app/inputs/tailored_select_input.rb' + end +end