Skip to content

Commit

Permalink
New feature translate slug
Browse files Browse the repository at this point in the history
  • Loading branch information
Brice Sanchez authored and bricesanchez committed Jul 23, 2015
1 parent 83f7202 commit 74c1b88
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 32 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ gemspec
gem 'refinerycms', git: 'https://github.com/refinery/refinerycms', branch: 'master'
gem 'refinerycms-acts-as-indexed', git: 'https://github.com/refinery/refinerycms-acts-as-indexed', branch: 'master'
gem 'refinerycms-i18n', git: 'https://github.com/refinery/refinerycms-i18n', branch: 'master'
gem 'friendly_id-globalize', github: 'norman/friendly_id-globalize', branch: 'master', ref: '5fedb92ff554f35a3351cdedc2eaafadbf16fd0d'

group :development, :test do
gem 'refinerycms-testing', git: 'https://github.com/refinery/refinerycms', branch: 'master'
Expand Down
26 changes: 13 additions & 13 deletions app/controllers/refinery/portfolio/admin/galleries_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class GalleriesController < ::Refinery::AdminController

crudify :'refinery/portfolio/gallery',
:order => 'lft ASC',
:include => [:children],
:include => [:children, :translations],
:paging => false

def new
Expand All @@ -18,22 +18,22 @@ def children
end

protected

def find_parent_gallery
if params[:parent_id].present?
::Refinery::Portfolio::Gallery.find(params[:parent_id])
def find_parent_gallery
if params[:parent_id].present?
::Refinery::Portfolio::Gallery.find(params[:parent_id])
end
end
end

def find_gallery
if params[:id].present?
@gallery = ::Refinery::Portfolio::Gallery.friendly.find(params[:id])
def find_gallery
if params[:id].present?
@gallery = ::Refinery::Portfolio::Gallery.friendly.find(params[:id])
end
end
end

def gallery_params
params.require(:gallery).permit(:title, :body, :parent_id, images: [])
end
private
def gallery_params
params.require(:gallery).permit(:title, :body, :parent_id, images: [])
end

end
end
Expand Down
16 changes: 8 additions & 8 deletions app/controllers/refinery/portfolio/admin/items_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@ def new
@item = Item.new(gallery: find_gallery)
end

private

def find_gallery
@gallery = Gallery.friendly.find(params[:gallery_id]) if params[:gallery_id]
end
protected
def find_gallery
@gallery = Gallery.friendly.find(params[:gallery_id]) if params[:gallery_id]
end

def item_params
params.require(:item).permit(:title, :caption, :image_id, :gallery_id)
end
private
def item_params
params.require(:item).permit(:title, :caption, :image_id, :gallery_id)
end

end
end
Expand Down
19 changes: 15 additions & 4 deletions app/models/refinery/portfolio/gallery.rb
Original file line number Diff line number Diff line change
@@ -1,21 +1,32 @@
module Refinery
module Portfolio
class Gallery < Refinery::Core::BaseModel
translates :title, :body, :slug
extend FriendlyId

acts_as_indexed :fields => [:title, :body]
acts_as_nested_set :dependent => :destroy

extend FriendlyId
friendly_id :title, :use => [:slugged]
translates :title, :body
friendly_id :friendly_id_source, use: :globalize

has_many :items, :dependent => :destroy
has_many :items, :dependent => :destroy

alias_attribute :description, :body

validates :title, presence: true, uniqueness: true

after_save :bulk_update_associated_items

# If title changes tell friendly_id to regenerate slug when
# saving record
def should_generate_new_friendly_id?
title_changed?
end

def friendly_id_source
title
end

def cover_image
items.sort_by(&:position).first if items.present?
end
Expand Down
7 changes: 3 additions & 4 deletions app/views/refinery/portfolio/admin/galleries/_form.html.erb
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
<%= form_for [refinery, :portfolio_admin, @gallery] do |f| -%>
<%= render :partial => "/refinery/admin/error_messages", :locals => {
:object => @gallery,
:include_object_name => true
} %>
<%= render "/refinery/admin/error_messages",
:object => f.object,
:include_object_name => true %>

<%= render '/refinery/admin/locale_picker', :current_locale => Globalize.locale %>

Expand Down
6 changes: 3 additions & 3 deletions app/views/refinery/portfolio/admin/items/_form.html.erb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<%= form_for [refinery, :portfolio_admin, @gallery, @item] do |f| -%>
<%= render :partial => "/refinery/admin/error_messages", :locals => {
:object => @item,
:include_object_name => true } %>
<%= render "/refinery/admin/error_messages",
:object => f.object,
:include_object_name => true %>

<%= render '/refinery/admin/locale_picker', :current_locale => Globalize.locale %>

Expand Down
9 changes: 9 additions & 0 deletions db/migrate/7_translate_slug_to_gallery.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class TranslateSlugToGallery < ActiveRecord::Migration
def up
Refinery::Portfolio::Gallery.add_translation_fields! slug: :string
end

def down
remove_column :refinery_portfolio_gallery_translations, :slug
end
end
3 changes: 3 additions & 0 deletions refinerycms-portfolio.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,7 @@ Gem::Specification.new do |s|

s.add_dependency 'refinerycms-core', '~> 3.0.0'
s.add_dependency 'refinerycms-acts-as-indexed', '~> 2.0.0'
s.add_dependency 'globalize', ['>= 4.0.0', '< 5.2']
s.add_dependency 'friendly_id', '~> 5.1.0'
s.add_dependency 'friendly_id-globalize', '>= 1.0.0.alpha2'
end

0 comments on commit 74c1b88

Please sign in to comment.