Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
rolemodel_rails (0.25.0)
rolemodel_rails (0.25.1)
rails (> 7.1)

GEM
Expand Down
2 changes: 1 addition & 1 deletion example_rails7/Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: ..
specs:
rolemodel_rails (0.25.0)
rolemodel_rails (0.25.1)
rails (> 7.1)

GEM
Expand Down
4 changes: 2 additions & 2 deletions example_rails8/Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: ..
specs:
rolemodel_rails (0.25.0)
rolemodel_rails (0.25.1)
rails (> 7.1)

GEM
Expand Down Expand Up @@ -521,7 +521,7 @@ CHECKSUMS
regexp_parser (2.11.3) sha256=ca13f381a173b7a93450e53459075c9b76a10433caadcb2f1180f2c741fc55a4
reline (0.6.3) sha256=1198b04973565b36ec0f11542ab3f5cfeeec34823f4e54cebde90968092b1835
rexml (3.4.4) sha256=19e0a2c3425dfbf2d4fc1189747bdb2f849b6c5e74180401b15734bc97b5d142
rolemodel_rails (0.25.0)
rolemodel_rails (0.25.1)
rubocop (1.84.1) sha256=14cc626f355141f5a2ef53c10a68d66b13bb30639b26370a76559096cc6bcc1a
rubocop-ast (1.49.0) sha256=49c3676d3123a0923d333e20c6c2dbaaae2d2287b475273fddee0c61da9f71fd
rubocop-performance (1.26.1) sha256=cd19b936ff196df85829d264b522fd4f98b6c89ad271fa52744a8c11b8f71834
Expand Down
1 change: 1 addition & 0 deletions lib/generators/rolemodel.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require 'rails'
require 'pathname'
require 'rails/generators'
require 'rails/generators/bundle_helper'
require_relative 'rolemodel/replace_content_helper'

Expand Down
8 changes: 4 additions & 4 deletions lib/generators/rolemodel/optics/icons/USAGE
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
Description:
runs the icons optics generator
Generates a ViewHelper Integrated With The Icon Provider of Your Choice

Example:
rails generate rolemodel:optics:icons
bin/rails generate rolemodel:optics:icons --feather

This will create:
app/helpers/icon_helper.rb
And then in a slim template/partial:
= button_to icon('delete'), @post, method: :delete
37 changes: 25 additions & 12 deletions lib/generators/rolemodel/optics/icons/icons_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,44 @@ module Rolemodel
module Optics
# Generates the icon helper and icon builders for the chosen icon library
class IconsGenerator < Rolemodel::BaseGenerator
source_root File.expand_path('templates', __dir__)

SUPPORTED_LIBRARIES = {
SUPPORTED_LIBRARIES = HashWithIndifferentAccess.new(
material: 'filled, size, weight, emphasis, additional_classes, color, hover_text',
phosphor: 'duotone, filled, size, weight, additional_classes, color, hover_text',
tabler: 'filled, size, additional_classes, color, hover_text',
feather: 'size, additional_classes, color, hover_text',
lucide: 'size, additional_classes, color, hover_text',
custom: 'filled, size, weight, emphasis, additional_classes, color, hover_text'
}.freeze
).freeze

source_root File.expand_path('templates', __dir__)
class_exclusive do
SUPPORTED_LIBRARIES.keys.each do |library|
class_option library, type: :boolean, lazy_default: false, desc: "Use #{library} icon library"
end
end

def remove_existing_icon_helper_and_builders
remove_dir 'app/icon_builders'
remove_file 'app/helpers/icon_helper.rb'
end

def add_view_helper
say 'generating icon helper', :green
@chosen_library = capture_user_selection
@supported_properties = SUPPORTED_LIBRARIES.fetch(@chosen_library)

@chosen_library = ask(
template 'app/icon_builders/icon_builder.rb'
template "app/icon_builders/#{@chosen_library}_icon_builder.rb"
template 'app/helpers/icon_helper.rb'
end

private

def capture_user_selection
options.except(*%i[skip_namespace skip_collision_check]).invert[true] || ask(
'What icon library would you like to add?',
default: SUPPORTED_LIBRARIES.keys.first.to_s,
limited_to: SUPPORTED_LIBRARIES.keys.map(&:to_s)
)

@supported_properties = SUPPORTED_LIBRARIES[@chosen_library.to_sym]

copy_file 'app/icon_builders/icon_builder.rb'
copy_file "app/icon_builders/#{@chosen_library}_icon_builder.rb"
template 'app/helpers/icon_helper.rb'
end
end
end
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
# frozen_string_literal: true

# Helper method that renders icons from different icon libraries.
# Helper method that renders icons from your icon library of choice.
#
# Usage:
# = icon('home', size: 'x-large', color: 'primary')
# = flash_icon('notice', size: 'x-large', color: 'primary')
# = icon('home', size: 'x-large', color: 'primary')
# = flash_icon('notice', size: 'x-large', color: 'primary')
#
# The chosen icon library is <%= @chosen_library %>. This can be
# changed by rerunning the generator with a different --icon-library option.
#
# Run `bin/rails g rolemodel:optics:icons --help` for an up-to-date list of available libraries.
#
# The default library is Material Icons, but can be changed here by pulling
# in a different builder from RoleModel Rails.
# Available libraries:
# - MaterialIconBuilder
# - PhosphorIconBuilder
# - TablerIconBuilder
# - FeatherIconBuilder
# - LucideIconBuilder
# - CustomIconBuilder
module IconHelper
# <%= @supported_properties %>
def icon(name, **)
Expand Down
2 changes: 1 addition & 1 deletion lib/rolemodel_rails/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module RolemodelRails
VERSION = '0.25.0'
VERSION = '0.25.1'
end
44 changes: 37 additions & 7 deletions spec/generators/rolemodel/optics/icons_generator_spec.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,42 @@
RSpec.describe Rolemodel::Optics::IconsGenerator, type: :generator do
before do
respond_to_prompt with: 'phosphor' # choose an icon library
run_generator_against_test_app
context 'default icon library' do
before do
respond_to_prompt with: 'material' # choose an icon library
run_generator_against_test_app
end

it 'adds the correct helper and builders' do
assert_file 'app/helpers/icon_helper.rb'
assert_file 'app/icon_builders/icon_builder.rb'
assert_file 'app/icon_builders/material_icon_builder.rb'
end
end

context 'selecting an alternate icon library via command line option' do
before { run_generator_against_test_app(['--phosphor']) }

it 'adds the correct helper and builders' do
assert_file 'app/helpers/icon_helper.rb'
assert_file 'app/icon_builders/icon_builder.rb'
assert_file 'app/icon_builders/phosphor_icon_builder.rb'
end
end

it 'adds the correct helper and builders' do
assert_file 'app/helpers/icon_helper.rb'
assert_file 'app/icon_builders/icon_builder.rb'
assert_file 'app/icon_builders/phosphor_icon_builder.rb'
context 're-running the generator' do
it 'removes existing helper and builders before adding new ones' do
assert_no_file 'app/helpers/icon_helper.rb'

run_generator_against_test_app(['--tabler'])
assert_file 'app/helpers/icon_helper.rb'
assert_file 'app/icon_builders/tabler_icon_builder.rb'

respond_to_prompt with: 'feather' # choose an icon library
run_generator_against_test_app
assert_file 'app/helpers/icon_helper.rb'
assert_file 'app/icon_builders/feather_icon_builder.rb'

assert_no_file 'app/icon_builders/tabler_icon_builder.rb'
assert_no_file 'app/icon_builders/material_icon_builder.rb'
end
end
end