Skip to content

Commit

Permalink
Merge pull request #15 from realstorypro/stimulus-upgrades
Browse files Browse the repository at this point in the history
Stimulus upgrades
  • Loading branch information
Leonid Medovyy authored Apr 14, 2023
2 parents b036dfe + 47d45b3 commit fa56a4c
Show file tree
Hide file tree
Showing 9 changed files with 76 additions and 19 deletions.
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:
rapid_ui (1.1.1)
rapid_ui (1.2.0)
view_component

GEM
Expand Down
4 changes: 2 additions & 2 deletions lib/install/rapid_ui.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
RapidUi.configure do |config|
config.ui_file = Rails.root.join('config/ui.yml')
config.ui_file = Rails.root.join("config/ui.yml")
end

RapidUi.boot
RapidUi.boot
6 changes: 4 additions & 2 deletions lib/install/wrapper_component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,17 @@ def tag_arguments
arguments[:type] = @component.type
arguments[:rows] = @component.rows
arguments[:placeholder] = @component.placeholder
arguments[:step] = @component.step
arguments[:disabled] = @component.disabled
arguments
end

# retreives the default name from a hash
# retrieves the default name from a hash
def default_name(default)
default[0].to_sym
end

# retreives the default value from a hash
# retrieves the default value from a hash
def default_value(default)
default[1]
end
Expand Down
73 changes: 64 additions & 9 deletions lib/rapid_ui/component.rb
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
module Ui
# responsible for the out building of the component
class Component
attr_accessor :action, :action_attr, :alt, :css_class, :data, :id, :img, :method, :name_attr, :placeholder, :rows, :style, :tag, :text, :title, :type, :url, :value, :rel
attr_accessor :action_attr, :alt, :css_class, :data, :id, :img, :method, :name_attr, :placeholder, :params,
:rows, :style, :tag, :text, :title, :type, :target, :url, :value, :rel, :step, :disabled

def initialize(settings)
@settings = settings
set_defaults
build_component
transpose_settings %w[action_attr alt data id img method name_attr placeholder rows style tag text title type url value rel]
transpose_settings %w[action_attr alt data id img method name_attr placeholder rows style tag text title type url
value rel step disabled]
end

# sets component defaults
def set_defaults
@data = nil
@css_class = ''
@css_class = ""
end

# builds out the component
Expand All @@ -24,26 +26,27 @@ def build_component
build_responsiveness
build_name
build_default_class
apply_stimulus_shortcuts
build_stimulus_props
end

# removes the ui class if the switch is set to false
def build_ui
add_class 'ui' unless (@settings.key?(:ui) && @settings[:ui] == false)
add_class "ui" unless @settings.key?(:ui) && @settings[:ui] == false
end


# adds dynamic class to the component is the switch is set to true
def build_dynamic
add_class 'dynamic' if (@settings.key?(:dynamic) && @settings[:dynamic])
add_class "dynamic" if @settings.key?(:dynamic) && @settings[:dynamic]
end

# builds out css class for the component
def build_class
add_class @settings[:class] if @settings.key?(:class)
end

# builds out the reponsive css classes
# builds out the responsive css classes
# used by semantic-ui responsive classes
def build_responsiveness
add_class build_only if @settings.key?(:only)
add_class build_size if @settings.key?(:size)
Expand All @@ -54,7 +57,7 @@ def build_responsiveness
end
end

# builds out the name for the compnent
# builds out the name for the component
def build_name
return unless @settings.key?(:name)

Expand All @@ -67,11 +70,21 @@ def build_default_class
add_class @settings[:css_class] if @settings.key?(:css_class)
end

def apply_stimulus_shortcuts
@settings[:controller] = @settings[:c] if @settings.key?(:c)
@settings[:action] = @settings[:a] if @settings.key?(:a)
@settings[:target] = @settings[:t] if @settings.key?(:t)
@settings[:params] = @settings[:p] if @settings.key?(:p)
@settings[:values] = @settings[:v] if @settings.key?(:v)
end

# builds out stimulus.js shortcuts
def build_stimulus_props
add_data :controller, @settings[:controller] if @settings.key?(:controller)
add_data :target, @settings[:target] if @settings.key?(:target)
add_data :action, @settings[:action] if @settings.key?(:action)
add_target_data @settings[:target] if @settings.key?(:target)
add_params_data @settings[:params] if @settings.key?(:params)
add_values_data @settings[:values] if @settings.key?(:values)
end

private
Expand Down Expand Up @@ -129,5 +142,47 @@ def add_data(name, value)

@settings[:data][name] = value
end

# goes through the target object and populates the data variables
def add_target_data(target_obj)
target_obj.keys.each do |target|
controller = target.to_s
target= target_obj[target]

data_string = "#{controller}-target"
add_data(data_string, target)
end
end


# goes through the params object and populate the data variables
def add_params_data(params_obj)
params_obj.keys.each do |params_controller|
controller = params_controller.to_s

params = params_obj[params_controller]
params.keys.each do |param|
param_value = params[param]
data_string = "#{controller}-#{param}-param"

add_data(data_string, param_value)
end
end
end

# goes through the values object and populate the data variables
def add_values_data(params_obj)
params_obj.keys.each do |params_controller|
controller = params_controller.to_s

params = params_obj[params_controller]
params.keys.each do |param|
param_value = params[param]
data_string = "#{controller}-#{param}-value"

add_data(data_string, param_value)
end
end
end
end
end
2 changes: 1 addition & 1 deletion lib/rapid_ui/display.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class Display < ActionView::Base
include ViewComponent

def initialize
RapidUi.configuration.ui_hash['ui'].each do |ui_element|
RapidUi.configuration.ui_hash["ui"].each do |ui_element|
define_singleton_method ui_element[0].to_s do |args = {}|
# for convenience we allow people to only pass a class string
# it is the same as setting a class
Expand Down
2 changes: 1 addition & 1 deletion lib/rapid_ui/helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ def ux
Ui::Display.instance
end
end
end
end
2 changes: 1 addition & 1 deletion lib/rapid_ui/utilities.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def initialize
end

def defaults(component:)
@config['ui'][component.to_s]
@config["ui"][component.to_s]
end
end
end
2 changes: 1 addition & 1 deletion lib/rapid_ui/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module RapidUi
VERSION = "1.1.1"
VERSION = "1.2.0"
end
2 changes: 1 addition & 1 deletion lib/tasks/rapid_ui.rake
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@ namespace :rapid_ui do
puts "----------------------------------------"
puts "Rapid Ui installed."
end
end
end

0 comments on commit fa56a4c

Please sign in to comment.