Skip to content

Commit

Permalink
support separators in action links
Browse files Browse the repository at this point in the history
  • Loading branch information
scambra committed Dec 10, 2024
1 parent 0a53b84 commit 2385048
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 11 deletions.
7 changes: 7 additions & 0 deletions app/assets/stylesheets/active_scaffold_colors.scss
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,9 @@ color: $column_color;
color: $header_color;
}

.active-scaffold div.actions a.separator {
border-color: $action_group_border_color;
}
.active-scaffold-header div.actions a.disabled {
color: $actions_disabled_color;
}
Expand Down Expand Up @@ -191,6 +194,10 @@ color: $column_empty_color;
border-color: $column_actions_border_color;
}

.active-scaffold tr.record td.actions table td.separator {
border-color: $column_actions_border_color;
}

.active-scaffold tr.record td.actions a.disabled {
color: $actions_disabled_color;
}
Expand Down
21 changes: 21 additions & 0 deletions app/assets/stylesheets/active_scaffold_layout.css
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,12 @@ top: 14px;
top: -3px;
}

.active-scaffold div.actions a.separator {
border-right: 1px solid;
padding-right: 0;
padding-left: 0;
}

.active-scaffold div.actions a.disabled {
opacity: 0.5;
}
Expand Down Expand Up @@ -251,6 +257,11 @@ text-align: right;
padding: 0 2px;
}

.active-scaffold tr.record td.actions table td.separator {
border-right: solid 1px;
padding: 0;
}

.active-scaffold tr.record td.actions a,
.active-scaffold tr.record td.actions div {
font-weight: bold;
Expand Down Expand Up @@ -304,6 +315,16 @@ width: auto;
text-align: left;
}

.active-scaffold .actions .action_group ul li.separator {
border-top-width: 1px;
border-top-style: solid;
height: 0;
}

.active-scaffold .actions .action_group ul li.separator + li {
border-top: none;
}

.active-scaffold .actions .action_group ul li div {
margin: 0;
padding: 5px 5px 5px 25px;
Expand Down
13 changes: 13 additions & 0 deletions lib/active_scaffold/data_structures/action_link_separator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class ActionLinkSeparator
def initialize(weight)
@weight = weight
end

attr_reader :weight

def ==(other)
other == :separator
end

def name_to_cache; end # :nodoc:
end
14 changes: 8 additions & 6 deletions lib/active_scaffold/data_structures/action_links.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ def add(action, options = {})
end
alias << add

def add_separator(weight = 0)
raise "Call add_separator on a group" if name == :root
add_to_set ActionLinkSeparator.new(weight)
end

def add_to_set(link)
@set << link
end
Expand All @@ -51,6 +56,7 @@ def add_to_group(link, group_name = nil)
def [](val)
links = []
@set.each do |item|
next if item == :separator
if item.is_a?(ActiveScaffold::DataStructures::ActionLinks)
collected = item[val]
links << collected unless collected.nil?
Expand All @@ -64,6 +70,7 @@ def [](val)
def find_duplicate(link)
links = []
@set.each do |item|
next if item == :separator
if item.is_a?(ActiveScaffold::DataStructures::ActionLinks)
collected = item.find_duplicate(link)
links << collected unless collected.nil?
Expand All @@ -76,6 +83,7 @@ def find_duplicate(link)

def delete(val)
each(include_set: true) do |link, set|
next if link == :separator
if link.action.to_s == val.to_s
set.delete link
break
Expand Down Expand Up @@ -110,12 +118,6 @@ def each(options = {}, &block)
end
end

def collect_by_type(type = nil)
links = []
subgroup(type).each(type) { |link| links << link }
links
end

def collect
@set
end
Expand Down
27 changes: 22 additions & 5 deletions lib/active_scaffold/helpers/action_link_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,33 +35,50 @@ def display_action_links(action_links, record, options, &block)
options[:level] ||= 0
options[:first_action] = true
output = ActiveSupport::SafeBuffer.new
prev_link = separator = nil

action_links.each(reverse: options.delete(:reverse), groups: true) do |link|
if link == :separator
separator = true if prev_link
next
end
content = nil
if link.is_a? ActiveScaffold::DataStructures::ActionLinks
unless link.empty?
options[:level] += 1
content = display_action_links(link, record, options, &block)
options[:level] -= 1
if content.present?
output << display_action_link(link, content, record, options)
content = display_action_link(link, content, record, options)
options[:first_action] = false
end
end
elsif !skip_action_link?(link, *Array(options[:for]))
authorized, reason = action_link_authorized?(link, *Array(options[:for]))
next if !authorized && options[:skip_unauthorized]

output << display_action_link(link, nil, record, options.merge(authorized: authorized, not_authorized_reason: reason))
content = display_action_link(link, nil, record, options.merge(authorized: authorized, not_authorized_reason: reason))
options[:first_action] = false
end
if content.present?
prev_link = true
output << display_action_link_separator(options) if separator
output << content
separator = false
end
end
output
end

def display_action_link_separator(options)
tag = options[:level_0_tag] || :a if options[:level].zero?
content_tag(tag || :li, '&nbsp;'.html_safe, class: 'separator')
end

def display_action_link(link, content, record, options)
if content
html_classes = hover_via_click? ? 'hover_click ' : ''
if (options[:level]).zero?
if options[:level].zero?
html_classes << 'action_group'
group_tag = :div
else
Expand All @@ -73,9 +90,9 @@ def display_action_link(link, content, record, options)
end
else
content = render_action_link(link, record, options)
content = content_tag(:li, content, class: ('top' if options[:first_action])) unless (options[:level]).zero?
content = content_tag(:li, content, class: ('top' if options[:first_action])) unless options[:level].zero?
end
content = content_tag(options[:level_0_tag], content, options[:options_level_0_tag]) if (options[:level]).zero? && options[:level_0_tag]
content = content_tag(options[:level_0_tag], content, options[:options_level_0_tag]) if options[:level].zero? && options[:level_0_tag]
content
end

Expand Down

0 comments on commit 2385048

Please sign in to comment.