Skip to content

Commit

Permalink
fix: set_locale param not setting the locale properly (#2855)
Browse files Browse the repository at this point in the history
* fix: `set_locale` param not setting the locale properly

* fix: set_locale distinct from the default locale

* fix specs
  • Loading branch information
adrianthedev authored Jun 14, 2024
1 parent 8db96f0 commit fa7d182
Show file tree
Hide file tree
Showing 8 changed files with 26 additions and 31 deletions.
12 changes: 6 additions & 6 deletions app/components/avo/divider_component.html.erb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<div class="relative flex justify-center items-center w-full !border-b-2 border-gray-200 border-solid">
<div class="absolute inset-auto z-20 text-xs font-semibold uppercase leading-none text-gray-500 bg-white px-2">
<%if label.present?%>
<%=label%>
<%end%>
</div>
<div class="relative flex justify-center items-center w-full !border-b-2 border-gray-200 border-solid" data-component-name="<%= self.class.to_s.underscore %>">
<% if label.present? %>
<div class="absolute inset-auto z-20 text-xs font-semibold uppercase leading-none text-gray-500 bg-white px-2 border py-1 rounded">
<%= label %>
</div>
<% end %>
</div>
25 changes: 8 additions & 17 deletions app/controllers/avo/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ class ApplicationController < ::ActionController::Base
protect_from_forgery with: :exception
around_action :set_avo_locale
around_action :set_force_locale, if: -> { params[:force_locale].present? }
before_action :set_default_locale, if: -> { params[:set_locale].present? }
before_action :init_app
before_action :set_active_storage_current_host
before_action :set_resource_name
Expand Down Expand Up @@ -52,10 +51,6 @@ def turbo_frame_request?
super
end

def hello
puts "Nobody tested me :("
end

private

# Get the pluralized resource name for this request
Expand Down Expand Up @@ -279,23 +274,21 @@ def model_param_key
@resource.form_scope
end

# Sets the locale set in avo.rb initializer
# Sets the locale set in avo.rb initializer or if to something that the user set using the `?set_locale=pt-BR` param
def set_avo_locale(&action)
locale = Avo.configuration.locale || I18n.default_locale
I18n.with_locale(locale, &action)
end
locale = Avo.configuration.default_locale

# Enable the user to change the default locale with the `?set_locale=pt-BR` param
def set_default_locale
locale = params[:set_locale] || I18n.default_locale
if params[:set_locale].present?
locale = params[:set_locale]
Avo.configuration.locale = locale
end

I18n.default_locale = locale
I18n.with_locale(locale, &action)
end

# Temporary set the locale and reverting at the end of the request.
def set_force_locale(&action)
locale = params[:force_locale] || I18n.default_locale
I18n.with_locale(locale, &action)
I18n.with_locale(params[:force_locale], &action)
end

def set_sidebar_open
Expand Down Expand Up @@ -327,8 +320,6 @@ def set_stylesheet_assets_path
end
end

private

def choose_layout
if turbo_frame_request?
"avo/blank"
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/avo/base_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -558,7 +558,7 @@ def is_associated_record?

# Set pagy locale from params or from avo configuration, if both nil locale = "en"
def set_pagy_locale
@pagy_locale = locale.to_s || Avo.configuration.locale || "en"
@pagy_locale = locale.to_s || Avo.configuration.default_locale || "en"
end

def safe_call(method)
Expand Down
4 changes: 4 additions & 0 deletions lib/avo/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,10 @@ def default_turbo
def pagination
Avo::ExecutionContext.new(target: @pagination).handle
end

def default_locale
@locale || I18n.default_locale
end
end

def self.configuration
Expand Down
2 changes: 1 addition & 1 deletion spec/dummy/app/avo/resources/project.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class Avo::Resources::Project < Avo::BaseResource
query.ransack(id_eq: params[:q], name_cont: params[:q], country_cont: params[:q], m: "or").result(distinct: false)
}
}
self.includes = [:users, :files_attachments]
self.includes = [files_attachments: :blob, users: [:comments, :teams, post: [comments: :user]]]
self.index_query = -> {
query.unscoped
}
Expand Down
1 change: 1 addition & 0 deletions spec/dummy/config/initializers/avo.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
params: request.params
}
end
config.locale = :en
# config.raise_error_on_missing_policy = true
# config.authorization_client = "Avo::Services::AuthorizationClients::ExtraPunditClient"

Expand Down
2 changes: 1 addition & 1 deletion spec/features/action_icon_and_divider_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
expect(page).to have_css("path[stroke-linecap='round'][stroke-linejoin='round'][d*='M3.055 11']")
expect(page).to have_css("path[stroke-linecap='round'][stroke-linejoin='round'][d*='M10.5 19.5']")

expect(page).to have_css(".relative.col-span-full.border-t.border-gray-300.border-solid")
expect(page).to have_css("[data-component-name='avo/divider_component']")
end
end
end
9 changes: 4 additions & 5 deletions spec/features/avo/divider_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,14 @@

describe "Divider in actions" do
it "renders divider without label" do
dividers = page.all(".relative.col-span-full.border-t")
dividers = page.all("[data-component-name='avo/divider_component']")
second_divider = dividers[1]
expect(second_divider).to have_css(".absolute", text: "")
expect(second_divider).not_to have_selector(".absolute.inset-auto.rounded")
end

it "renders divider with label" do
expect(page).to have_css ".relative.col-span-full.border-t"
dividers = page.all(".relative.col-span-full.border-t")
expect(dividers.first.find(".absolute").text.strip).to eq "Other actions"
dividers = page.all("[data-component-name='avo/divider_component']")
expect(dividers.first.find(".absolute.inset-auto.rounded").text.strip).to eq "Other actions"
expect(page).to have_content "Other actions"
end
end
Expand Down

0 comments on commit fa7d182

Please sign in to comment.