Last active
March 16, 2017 06:39
-
-
Save dcuadraq/5e635532f45524add9d4abb51a5bc946 to your computer and use it in GitHub Desktop.
Add globalize to States on Solidus (2.1)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# db/migrate/20170315153444_create_spree_state_translations.rb | |
class CreateSpreeStateTranslations < ActiveRecord::Migration[5.0] | |
reversible do |dir| | |
dir.up do | |
Spree::State.create_translation_table! name: :string, abbr: :string | |
end | |
dir.down do | |
Spree::State.drop_translation_table! | |
end | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# app/views/spree/admin/states/edit.html.erb | |
<%= render 'spree/admin/shared/areas_tabs' %> | |
<% admin_breadcrumb(Spree.t(:settings)) %> | |
<% admin_breadcrumb(Spree.t('admin.tab.areas')) %> | |
<% admin_breadcrumb(link_to plural_resource_name(Spree::State), spree.admin_states_path) %> | |
<% admin_breadcrumb(@state.name) %> | |
<% content_for :page_actions do %> | |
<% end %> | |
<%= render :partial => 'spree/shared/error_messages', :locals => { :target => @state } %> | |
<%= form_for [:admin, @country, @state] do |f| %> | |
<fieldset class="no-border-top"> | |
<%= render :partial => 'form', :locals => { :f => f } %> | |
<%= render :partial => 'spree/admin/shared/edit_resource_links' %> | |
</fieldset> | |
<% end %> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# config/routes.rb | |
resources :states do | |
member do | |
get :translation | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# app/models/spree/state_decorator.rb | |
Spree::State.class_eval do | |
translates :name, :abbr | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# app/controllers/spree/admin/states_controller_decorator.rb | |
Spree::Admin::StatesController.class_eval do | |
skip_before_action :load_resource, only: [:edit, :update] | |
def index | |
@states = Spree::State.where(country: @country) | |
respond_with(@collection) do |format| | |
format.html | |
format.js { render partial: 'state_list' } | |
end | |
end | |
def edit | |
@country = Spree::Country.find(params[:country_id]) | |
@state = Spree::State.find(params[:id]) | |
respond_with(@object) do |format| | |
format.html { render layout: !request.xhr? } | |
if request.xhr? | |
format.js { render layout: false } | |
end | |
end | |
end | |
def update | |
@state = Spree::State.find(params[:id]) | |
@country = @state.country | |
if @state.update_attributes(permitted_resource_params) | |
respond_with(@state) do |format| | |
format.html do | |
flash[:success] = flash_message_for(@state, :successfully_updated) | |
redirect_to location_after_save | |
end | |
format.js { render layout: false } | |
end | |
else | |
respond_with(@state) do |format| | |
format.html do | |
flash.now[:error] = @state.errors.full_messages.join(", ") | |
render_after_update_error | |
end | |
format.js { render layout: false } | |
end | |
end | |
end | |
def translation | |
@state = Spree::State.find(params[:id]) | |
end | |
def translate | |
state = Spree::State.find(params[:id]) | |
state.update update_state_attribute | |
redirect_to spree.admin_country_state_properties_path(params[:country_id], state) | |
end | |
private | |
def update_state_attribute | |
params.require(:state).permit(permitted_params) | |
end | |
def permitted_params | |
[translations_attributes: [:id, :locale, :name, :abbr]] | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment