Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cleanup customization interfaces #282

Merged
merged 3 commits into from
Nov 2, 2023
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
39 changes: 26 additions & 13 deletions app/models/spree/admin/actions/action.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,26 @@ module Spree
module Admin
module Actions
class Action
attr_reader :icon_name, :key, :classes, :text, :method, :id, :target, :data
STYLE_CLASSES = {
::Spree::Admin::Actions::ActionStyle::PRIMARY => 'btn-success',
::Spree::Admin::Actions::ActionStyle::SECONDARY => 'btn-secondary',
::Spree::Admin::Actions::ActionStyle::LIGHT => 'btn-light'
}

def initialize(config)
@icon_name = config[:icon_name]
@key = config[:key]
@url = config[:url]
@classes = config[:classes]
@availability_checks = config[:availability_checks]
@text = config[:text]
@method = config[:method]
@id = config[:id]
@translation_options = config[:translation_options]
@target = config[:target]
@data = config[:data]
attr_reader :key, :label_translation_key, :icon_key, :method, :id, :target, :data_attributes

def initialize(key, label_translation_key, url, icon_key, style, availability_checks, additional_classes, method, id, target, data_attributes) # rubocop:disable Metrics/ParameterLists
@key = key
@label_translation_key = label_translation_key
@url = url
@icon_key = icon_key
@style = style
@availability_checks = availability_checks
@additional_classes = additional_classes
@method = method
@id = id
@target = target
@data_attributes = data_attributes
end

def available?(current_ability, resource = nil)
Expand All @@ -29,6 +35,13 @@ def available?(current_ability, resource = nil)
def url(resource = nil)
@url.is_a?(Proc) ? @url.call(resource) : @url
end

def classes
[
STYLE_CLASSES[@style],
@additional_classes
].compact.join(' ')
end
end
end
end
Expand Down
89 changes: 60 additions & 29 deletions app/models/spree/admin/actions/action_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,43 +4,74 @@ module Actions
class ActionBuilder
include ::Spree::Admin::PermissionChecks

def initialize(config)
@icon_name = config[:icon_name]
@key = config[:key]
@url = config[:url]
@classes = config[:classes]
def initialize(key, url)
@key = key
@label_translation_key = key
@url = url
@icon_key = nil
@style = ::Spree::Admin::Actions::ActionStyle::LIGHT
@availability_checks = []
@method = config[:method]
@id = config[:id]
@translation_options = config[:translation_options]
@target = config[:target]
@data = config[:data]
@classes = ''
@method = nil
@id = nil
@target = nil
@data_attributes = {}
end

def build
Action.new(build_config)
def with_label_translation_key(key)
@label_translation_key = key
self
end

def with_icon_key(icon_key)
@icon_key = icon_key
self
end

private
def with_classes(classes)
@classes = classes
self
end

def build_config
{
icon_name: @icon_name,
key: @key,
url: @url,
classes: @classes,
availability_checks: @availability_checks,
text: text(@key, @translation_options),
method: @method,
id: @id,
translation_options: @translation_options,
target: @target,
data: @data
}
def with_style(style)
@style = style
self
end

def text(text, options)
options.present? ? ::Spree.t(text, options) : ::Spree.t(text)
def with_method(method)
@method = method
self
end

def with_id(id)
@id = id
self
end

def with_target(target)
@target = target
self
end

def with_data_attributes(data_attributes)
@data_attributes = data_attributes
self
end

def build
Action.new(
@key,
@label_translation_key,
@url,
@icon_key,
@style,
@availability_checks,
@classes,
@method,
@id,
@target,
@data_attributes
)
end
end
end
Expand Down
11 changes: 11 additions & 0 deletions app/models/spree/admin/actions/action_style.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module Spree
module Admin
module Actions
module ActionStyle
PRIMARY = 'primary'
SECONDARY = 'secondary'
LIGHT = 'light'
end
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,14 @@ def build

def add_new_adjustment_action(root)
action =
ActionBuilder.new(new_adjustment_config).
ActionBuilder.new('new_adjustment', ->(resource) { new_admin_order_adjustment_path(resource) }).
with_icon_key('add.svg').
with_style(::Spree::Admin::Actions::ActionStyle::PRIMARY).
with_create_ability_check(::Spree::Adjustment).
build

root.add(action)
end

def new_adjustment_config
{
icon_name: 'add.svg',
key: :new_adjustment,
url: ->(resource) { new_admin_order_adjustment_path(resource) },
classes: 'btn-success'
}
end
end
end
end
Expand Down
15 changes: 4 additions & 11 deletions app/models/spree/admin/actions/images_default_actions_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,15 @@ def build

def add_new_image_action(root)
action =
ActionBuilder.new(new_image_config).
ActionBuilder.new('new_image', ->(resource) { new_admin_product_image_path(resource) }).
with_icon_key('add.svg').
with_style(::Spree::Admin::Actions::ActionStyle::PRIMARY).
with_id('new_image_link').
with_create_ability_check(::Spree::Image).
build

root.add(action)
end

def new_image_config
{
icon_name: 'add.svg',
key: :new_image,
url: ->(resource) { new_admin_product_image_path(resource) },
classes: 'btn-success',
id: 'new_image_link'
}
end
end
end
end
Expand Down
94 changes: 24 additions & 70 deletions app/models/spree/admin/actions/order_default_actions_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,102 +18,65 @@ def build

def add_approve_action(root)
action =
ActionBuilder.new(approve_config).
ActionBuilder.new('approve', ->(resource) { approve_admin_order_path(resource) }).
with_icon_key('approve.svg').
with_label_translation_key('admin.order.events.approve').
with_method(:put).
with_data_attributes({ confirm: Spree.t(:order_sure_want_to, event: :approve) }).
with_state_change_check('approve').
with_fire_ability_check.
build

root.add(action)
end

def approve_config
{
icon_name: 'approve.svg',
key: :approve,
url: ->(resource) { approve_admin_order_path(resource) },
classes: 'btn-light',
method: :put,
translation_options: {
scope: 'admin.order.events'
},
data: { confirm: Spree.t(:order_sure_want_to, event: :approve) }
}
end

def add_cancel_action(root)
action =
ActionBuilder.new(cancel_config).
ActionBuilder.new('cancel', ->(resource) { cancel_admin_order_path(resource) }).
with_icon_key('cancel.svg').
with_label_translation_key('admin.order.events.cancel').
with_method(:put).
with_data_attributes({ confirm: Spree.t(:order_sure_want_to, event: :cancel) }).
with_state_change_check('cancel').
with_fire_ability_check.
build

root.add(action)
end

def cancel_config
{
icon_name: 'cancel.svg',
key: :cancel,
url: ->(resource) { cancel_admin_order_path(resource) },
classes: 'btn-light',
method: :put,
translation_options: {
scope: 'admin.order.events'
},
data: { confirm: Spree.t(:order_sure_want_to, event: :cancel) }
}
end

def add_resume_action(root)
action =
ActionBuilder.new(resume_config).
ActionBuilder.new('resume', ->(resource) { resume_admin_order_path(resource) }).
with_icon_key('resume.svg').
with_label_translation_key('admin.order.events.resume').
with_method(:put).
with_data_attributes({ confirm: Spree.t(:order_sure_want_to, event: :resume) }).
with_state_change_check('resume').
with_fire_ability_check.
build

root.add(action)
end

def resume_config
{
icon_name: 'resume.svg',
key: :resume,
url: ->(resource) { resume_admin_order_path(resource) },
classes: 'btn-light',
method: :put,
translation_options: {
scope: 'admin.order.events'
},
data: { confirm: Spree.t(:order_sure_want_to, event: :resume) }
}
end

def add_resend_action(root)
action =
ActionBuilder.new(resend_config).
ActionBuilder.new('resend', ->(resource) { resend_admin_order_path(resource) }).
with_icon_key('envelope.svg').
with_label_translation_key('admin.order.events.resend').
with_method(:post).
with_style(::Spree::Admin::Actions::ActionStyle::SECONDARY).
with_resend_ability_check.
build

root.add(action)
end

def resend_config
{
icon_name: 'envelope.svg',
key: :resend,
url: ->(resource) { resend_admin_order_path(resource) },
classes: 'btn-secondary',
method: :post,
translation_options: {
scope: 'admin.order.events',
default: ::Spree.t(:resend)
}
}
end

def add_reset_download_links_action(root)
action =
ActionBuilder.new(reset_download_links_config).
ActionBuilder.new('reset_download_links', ->(resource) { reset_digitals_admin_order_path(resource) }).
with_icon_key('hdd.svg').
with_label_translation_key('admin.digitals.reset_download_links').
with_method(:put).
with_availability_check(
lambda do |ability, resource|
ability.can?(:update, resource) && resource.some_digital?
Expand All @@ -123,15 +86,6 @@ def add_reset_download_links_action(root)

root.add(action)
end

def reset_download_links_config
{
icon_name: 'hdd.svg',
key: 'admin.digitals.reset_download_links',
url: ->(resource) { reset_digitals_admin_order_path(resource) },
method: :put
}
end
end
end
end
Expand Down
15 changes: 4 additions & 11 deletions app/models/spree/admin/actions/orders_default_actions_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,15 @@ def build

def add_new_order_action(root)
action =
ActionBuilder.new(new_order_config).
ActionBuilder.new('new_order', new_admin_order_path).
with_icon_key('add.svg').
with_id('admin_new_order').
with_style(::Spree::Admin::Actions::ActionStyle::PRIMARY).
with_create_ability_check(::Spree::Order).
build

root.add(action)
end

def new_order_config
{
icon_name: 'add.svg',
key: :new_order,
url: new_admin_order_path,
classes: 'btn-success',
id: 'admin_new_order'
}
end
end
end
end
Expand Down
Loading
Loading