-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[RMS 2] Tailored Select Generator (#129)
This PR adds a new generator for the Tailored Select package. It also adds some developer tools around it for using it in tests and with simple form. --------- Co-authored-by: Braden Rich <[email protected]>
- Loading branch information
1 parent
f594ba2
commit 7a205cb
Showing
15 changed files
with
191 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
PATH | ||
remote: . | ||
specs: | ||
rolemodel_rails (0.9.0) | ||
rolemodel_rails (0.10.0) | ||
|
||
GEM | ||
remote: https://rubygems.org/ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
lib/generators/rolemodel/simple_form/templates/app/inputs/tailored_select_input.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
# frozen_string_literal: true | ||
|
||
# TailoredSelectInput is a custom input type for SimpleForm that renders a tailored select web component | ||
# | ||
# Options: | ||
# Any options available for SimpleForm's CollectionSelectInput (A.K.A a normal HTML select) | ||
# | ||
# Usage: | ||
# <%= f.input :my_field, as: :tailored_select %> | ||
|
||
module ActionView | ||
module Helpers | ||
class FormBuilder | ||
def tailored_select(method, collection, value_method, text_method, options = {}, html_options = {}) | ||
@template.tailored_select(@object_name, method, collection, value_method, text_method, | ||
objectify_options(options), @default_html_options.merge(html_options)) | ||
end | ||
end | ||
|
||
module FormOptionsHelper | ||
def tailored_select(object, method, collection, value_method, text_method, options = {}, html_options = {}) | ||
Tags::TailoredSelect.new(object, method, self, collection, value_method, text_method, options, | ||
html_options).render | ||
end | ||
end | ||
|
||
module Tags | ||
class TailoredSelect < CollectionSelect | ||
private | ||
|
||
def select_content_tag(option_tags, options, html_options) | ||
html_options = html_options.stringify_keys | ||
%i[required multiple size].each do |prop| | ||
html_options[prop.to_s] = options.delete(prop) if options.key?(prop) && !html_options.key?(prop.to_s) | ||
end | ||
|
||
add_default_name_and_id(html_options) | ||
|
||
if placeholder_required?(html_options) | ||
if options[:include_blank] == false | ||
raise ArgumentError, | ||
'include_blank cannot be false for a required field.' | ||
end | ||
|
||
options[:include_blank] ||= true unless options[:prompt] | ||
end | ||
|
||
value = options.fetch(:selected) { value() } | ||
# For real, this is the only line that changed from CollectionSelect, just changed the tag name | ||
select = content_tag('tailored-select', add_options(option_tags, options, value), html_options) | ||
|
||
if html_options['multiple'] && options.fetch(:include_hidden, true) | ||
tag('input', disabled: html_options['disabled'], name: html_options['name'], type: 'hidden', value: '', | ||
autocomplete: 'off') + select | ||
else | ||
select | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end | ||
|
||
class TailoredSelectInput < SimpleForm::Inputs::CollectionSelectInput | ||
def input(wrapper_options = nil) | ||
@builder.tailored_select( | ||
attribute_name, collection, *detect_collection_methods.reverse, input_options, | ||
merge_wrapper_options(input_html_options, wrapper_options) | ||
) | ||
end | ||
|
||
def input_html_classes | ||
[] | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# TailoredSelect Generator | ||
|
||
`rails g rolemodel:tailored_select` | ||
|
||
## What you get | ||
|
||
The [Tailored Select](https://github.com/RoleModel/tailored-select) web component |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
Description: | ||
runs the tailored select generator | ||
|
||
Example: | ||
rails generate rolemodel:tailored_select |
13 changes: 13 additions & 0 deletions
13
lib/generators/rolemodel/tailored_select/tailored_select_generator.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# frozen_string_literal: true | ||
|
||
module Rolemodel | ||
class TailoredSelectGenerator < Rails::Generators::Base | ||
source_root File.expand_path('templates', __dir__) | ||
|
||
def add_tailored_select_package | ||
say 'Installing Tailored Select package', :green | ||
|
||
run 'yarn add @rolemodel/tailored-select' | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
lib/generators/rolemodel/testing/rspec/templates/support/helpers/select_helper.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
# frozen_string_literal: true | ||
|
||
module SelectHelper | ||
include Capybara::DSL | ||
|
||
def smart_select(value, from:) | ||
label = find(:label, text: from, match: :first) | ||
input = find(id: label['for'], visible: :all) | ||
|
||
if input.tag_name == 'input' && input.native.attribute(:'aria-controls').include?('ts-dropdown') | ||
tom_select(input, value) | ||
elsif input.tag_name == 'tailored-select' | ||
tailored_select(input, value) | ||
else | ||
regular_select(value.to_s, from:) | ||
end | ||
end | ||
|
||
def tailored_select(input, value) | ||
option = input.find(:option, value, visible: :all) | ||
execute_script('arguments[0].click();', option) | ||
end | ||
|
||
def tom_select(input, value) | ||
input.ancestor('.ts-control').click | ||
input.send_keys(value) | ||
sleep 0.5 # required due to bug in tom-select | ||
input.send_keys(:enter) | ||
end | ||
|
||
alias regular_select select | ||
alias select smart_select | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
module RolemodelRails | ||
VERSION = '0.9.0' | ||
VERSION = '0.10.0' | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
require 'spec_helper' | ||
require 'generators/rolemodel/testing/rspec/rspec_generator' | ||
|
||
RSpec.describe Rolemodel::Testing::RspecGenerator, type: :generator do | ||
destination File.expand_path('tmp/', File.dirname(__FILE__)) | ||
|
||
before(:all) do | ||
prepare_test_app | ||
run_generator | ||
end | ||
|
||
after(:all) do | ||
cleanup_test_app | ||
end | ||
|
||
it 'adds the correct helpers' do | ||
assert_file 'spec/support/helpers/action_cable_helper.rb' | ||
assert_file 'spec/support/helpers/select_helper.rb' | ||
assert_file 'spec/support/helpers/test_element_helper.rb' | ||
end | ||
end |
21 changes: 21 additions & 0 deletions
21
spec/generators/rolemodel/tailored_select_generator_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
require 'spec_helper' | ||
require 'generators/rolemodel/tailored_select/tailored_select_generator' | ||
|
||
RSpec.describe Rolemodel::TailoredSelectGenerator, type: :generator do | ||
destination File.expand_path('tmp/', File.dirname(__FILE__)) | ||
|
||
before(:all) do | ||
prepare_test_app | ||
FileUtils.cd(destination_root) { run_generator } | ||
end | ||
|
||
after(:all) do | ||
cleanup_test_app | ||
end | ||
|
||
it 'adds tailored select to package.json' do | ||
assert_file 'package.json' do |content| | ||
expect(content).to include('"@rolemodel/tailored-select":') | ||
end | ||
end | ||
end |