Skip to content

Commit

Permalink
refactor: badge field rename secondary to neutral
Browse files Browse the repository at this point in the history
  • Loading branch information
Paul-Bob committed Aug 31, 2023
1 parent 8e56110 commit 57d3da8
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 29 deletions.
26 changes: 1 addition & 25 deletions app/components/avo/fields/common/badge_viewer_component.html.erb
Original file line number Diff line number Diff line change
@@ -1,25 +1 @@
<%
backgrounds = {
info: 'bg-blue-500',
success: 'bg-green-500',
danger: 'bg-red-500',
warning: 'bg-yellow-500',
secondary: 'bg-gray-500',
}

label = @value
background = :info

@options.invert.each do |values, type|
if [values].flatten.map { |value| value }.include? @value
label = @value
background = type.to_sym
next
end
end

classes = 'whitespace-nowrap rounded-md uppercase px-2 py-1 text-xs font-bold block text-center truncate '

classes += "#{backgrounds[background]} text-white" if backgrounds[background].present?
%>
<span class="<%= classes %>" style="max-width: 120px;"><%= label %></span>
<span class="<%= classes %>" style="max-width: 120px;"><%= @value %></span>
24 changes: 24 additions & 0 deletions app/components/avo/fields/common/badge_viewer_component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,29 @@ class Avo::Fields::Common::BadgeViewerComponent < ViewComponent::Base
def initialize(value:, options:)
@value = value
@options = options
@backgrounds = {
info: "bg-blue-500",
success: "bg-green-500",
danger: "bg-red-500",
warning: "bg-yellow-500",
neutral: "bg-gray-500"
}
end

def classes
background = :info

@options.invert.each do |values, type|
if [values].flatten.map { |value| value }.include? @value
background = type.to_sym
next
end
end

classes = "whitespace-nowrap rounded-md uppercase px-2 py-1 text-xs font-bold block text-center truncate "

classes += "#{@backgrounds[background]} text-white" if @backgrounds[background].present?

classes
end
end
2 changes: 1 addition & 1 deletion lib/avo/fields/badge_field.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ def initialize(id, **args, &block)

hide_on [:edit, :new]

default_options = {info: :info, success: :success, danger: :danger, warning: :warning, secondary: :secondary}
default_options = {info: :info, success: :success, danger: :danger, warning: :warning, neutral: :neutral}
@options = args[:options].present? ? default_options.merge(args[:options]) : default_options
end
end
Expand Down
2 changes: 1 addition & 1 deletion spec/dummy/app/avo/resources/project_resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class ProjectResource < Avo::BaseResource
placeholder: "Choose the stage",
display_value: true,
include_blank: false
field :stage, as: :badge, options: {info: ["Discovery", "Idea"], success: "Done", warning: "On hold", danger: "Cancelled", secondary: "Drafting"}
field :stage, as: :badge, options: {info: ["Discovery", "Idea"], success: "Done", warning: "On hold", danger: "Cancelled", neutral: "Drafting"}
field :country,
as: :country,
index_text_align: :left,
Expand Down
4 changes: 2 additions & 2 deletions spec/features/avo/badge_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
}
end

describe "with secondary value" do
describe "with neutral value" do
let(:stage) { "Drafting" }
let!(:project) { create :project, users_required: 15, stage: stage }

Expand Down Expand Up @@ -135,7 +135,7 @@
}
end

describe "with secondary value" do
describe "with neutral value" do
let(:stage) { "Drafting" }
let!(:project) { create :project, users_required: 15, stage: stage }

Expand Down

0 comments on commit 57d3da8

Please sign in to comment.