Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 20 additions & 3 deletions app/controllers/refinery/blog/admin/categories_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,27 @@ module Admin
class CategoriesController < ::Refinery::AdminController

crudify :'refinery/blog/category',
:order => 'title ASC'

private
:order => 'lft ASC, title ASC',
:include => [ :children],
:paging => !Refinery::Blog.category_orderable

def children
@category = find_category
render :layout => false
end

protected

def after_update_positions
find_all_categories
render :partial => '/refinery/blog/admin/categories/sortable_list' and return
end

def find_category
@category = Category.find_by_path_or_id!(params[:path], params[:id])
end
alias_method :category, :find_category

def category_params
params.require(:category).permit(:title)
end
Expand Down
2 changes: 1 addition & 1 deletion app/models/refinery/blog/categorization.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module Refinery
module Blog
class Categorization < ActiveRecord::Base
class Categorization < Core::BaseModel

self.table_name = 'refinery_blog_categories_blog_posts'
belongs_to :blog_post, :class_name => 'Refinery::Blog::Post', :foreign_key => :blog_post_id
Expand Down
74 changes: 71 additions & 3 deletions app/models/refinery/blog/category.rb
Original file line number Diff line number Diff line change
@@ -1,19 +1,51 @@
# Encoding: utf-8

require 'refinery/core/base_model'


module Refinery
module Blog
class Category < ActiveRecord::Base
class Category < Core::BaseModel
extend FriendlyId

translates :title, :slug

friendly_id :title, :use => [:slugged, :globalize]
acts_as_nested_set :dependent => :destroy

has_many :categorizations, :dependent => :destroy, :foreign_key => :blog_category_id
has_many :posts, :through => :categorizations, :source => :blog_post

validates :title, :presence => true, :uniqueness => true

def self.translated
with_translations(::Globalize.locale)


class << self
def translated
with_translations(::Globalize.locale)
end



def find_by_path_or_id(path, id)
if path.present?
if path.friendly_id?
self.friendly.find_by_path(path)
else
self.friendly.find(path)
end
elsif id.present?
self.friendly.find(id)
end

end
def find_by_path_or_id!(path, id)
c = find_by_path_or_id(path, id)
raise ActiveRecord::RecordNotFound unless c
c
end


end

def post_count
Expand All @@ -23,6 +55,42 @@ def post_count
# how many items to show per page
self.per_page = Refinery::Blog.posts_per_page


def nested_url
globalized_slug = Globalize.with_locale(slug_locale) { to_param.to_s }
if ::Refinery::Blog.scope_slug_by_parent
[parent.try(:nested_url), globalized_slug].compact.flatten
else
[globalized_slug]
end
end

# Returns an array with all ancestors to_param, allow with its own
# Ex: with a China category and a Hong Kong underneath,
# ::Refinery::Blog::Category.find('hongkong').nested_url would return:
#
# ['china', 'hongkong']
#
alias_method :uncached_nested_url, :nested_url

# Returns the string version of nested_url, i.e., the path that should be
# generated by the router
def nested_path
['', nested_url].join('/')
end


def slug_locale
return Globalize.locale if translation_for(Globalize.locale).try(:slug).present?

if translations.empty? || translation_for(Refinery::I18n.default_frontend_locale).present?
Refinery::I18n.default_frontend_locale
else
translations.first.locale
end
end


end
end
end
14 changes: 14 additions & 0 deletions app/views/refinery/blog/admin/_submenu.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@

<ul class='collapsible_menu'>
<li class='not_a_link'>

<%= link_to t('.categories.title'), '#',
:class => 'folder_icon' %>
</li>
Expand All @@ -62,6 +63,19 @@
<%= link_to t('.categories.new'), refinery.new_blog_admin_category_path,
:class => 'folder_add_icon' %>
</li>

<% if Refinery::Blog.category_orderable && @categories.present? && @categories.many? && params[:controller] == 'refinery/blog/admin/categories' %>
<li>
<%= link_to t('.categories.reorder_categories'), refinery.blog_admin_categories_path,
:id => "reorder_action",
:class => "reorder_icon" %>

<%= link_to t('.categories.reorder_categories_done'), refinery.blog_admin_categories_path,
:id => "reorder_action_done",
:style => "display: none;",
:class => "reorder_icon" %>
</li>
<%end %>
</ul>

<ul class='collapsible_menu'>
Expand Down
56 changes: 33 additions & 23 deletions app/views/refinery/blog/admin/categories/_category.html.erb
Original file line number Diff line number Diff line change
@@ -1,26 +1,36 @@
<li class='clearfix record <%= cycle("on", "on-hover") %>' id="<%= dom_id(category) -%>">
<span class='title'>
<%= category.title.presence || category.translations.detect {|t| t.title.present?}.title %>
<span class="preview">
<% category.translations.each do |translation| %>
<% if translation.title.present? %>
<%= link_to refinery_icon_tag("flags/#{translation.locale}.png", :size => '16x11'),
refinery.edit_blog_admin_category_path(category, :switch_locale => translation.locale),
:class => 'locale' %>
<% end %>
<% end %>
<div class='clearfix'>
<% if category.children.present? %>
<span class="icon toggle <%= 'expanded' if Refinery::Blog.auto_expand_admin_tree %>" title="<%= t('expand_collapse', :scope => 'refinery.admin.blog.categories') %>"></span>
<% else %>
<span class="icon"></span>
<% end %>
<span class='title'>
<%= category.title.presence || category.translations.detect {|t| t.title.present?}.title %>
<span class="preview">
<% category.translations.each do |translation| %>
<% if translation.title.present? %>
<%= link_to refinery_icon_tag("flags/#{translation.locale}.png", :size => '16x11'),
refinery.edit_blog_admin_category_path(category, :switch_locale => translation.locale),
:class => 'locale' %>
<% end %>
<% end %>
</span>
</span>
</span>
<span class='actions'>
<%= link_to refinery_icon_tag("application_edit.png"),
refinery.edit_blog_admin_category_path(category),
:title => t('.edit') %>
<%= link_to refinery_icon_tag("delete.png"), refinery.blog_admin_category_path(category),
:class => "cancel confirm-delete",
:title => t('.delete'),
:method => :delete,
:data => {
:confirm => t('message', :scope => 'refinery.admin.delete', :title => category.title)
} %>
</span>
<span class='actions'>
<%= link_to refinery_icon_tag("application_edit.png"),
refinery.edit_blog_admin_category_path(category),
:title => t('.edit') %>
<%= link_to refinery_icon_tag("delete.png"), refinery.blog_admin_category_path(category),
:class => "cancel confirm-delete",
:title => t('.delete'),
:method => :delete,
:data => {
:confirm => t('message', :scope => 'refinery.admin.delete', :title => category.title)
} %>
</span>
</div>
<ul class='nested' data-ajax-content="<%= refinery.blog_admin_children_categories_path(category.nested_url) %>">
<%= render(:partial => 'category', :collection => category.children) if Refinery::Blog.auto_expand_admin_tree %>
</ul>
</li>
19 changes: 19 additions & 0 deletions app/views/refinery/blog/admin/categories/index.html.erb
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
<%= render '/refinery/blog/admin/submenu' %>
<%= render 'refinery/admin/search_header', :url => request.path %>
<% unless Refinery::Blog.category_orderable %>
<div id='records'>
<% if @categories.any? %>

<%= will_paginate @categories %>

<%= render "sortable_list" %>
Expand All @@ -19,3 +21,20 @@
<% end %>
<% end %>
</div>
<% else %>
<section id='records' class='tree'>
<%= render 'refinery/admin/search_header', :url => refinery.admin_pages_path %>
<% if @categories.any? %>
<%= render 'sortable_list' %>
<% else %>
<p>
<% if searching? %>
<%= t('no_results', :scope => 'refinery.admin.search') %>
<% else %>
<strong><%= t('.no_items_yet', :create => t('new', :scope => 'refinery.blog.admin.submenu.categories')) %></strong>
<% end %>
</p>
<% end %>
</section>
<%= render '/refinery/admin/make_sortable', options: {tree: true, replaceContentsAfterUpdate: true} if @categories.many? -%>
<% end %>
2 changes: 2 additions & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@ en:
shared:
categories:
title: Categories
reorder_categories: Reorder categories
reorder_categories_done: Finished reordering categories
rss_feed:
title: RSS Feed
subscribe: Subscribe
Expand Down
2 changes: 2 additions & 0 deletions config/locales/zh-CN.yml
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ zh-CN:
title: 分类
manage: 管理
new: 创建新的类别
reorder_categories: 重新编排分类顺序
reorder_categories_done: 完成分类顺序编排
comments:
title: 评论
title_with_count: '评论 (%{new_count} new)'
Expand Down
2 changes: 2 additions & 0 deletions config/locales/zh-TW.yml
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ zh-TW:
title: 分類
manage: 管理
new: 創建新的類別
reorder_categories: 重新編排分類順序
reorder_categories_done: 完成頁面分類編排
comments:
title: 評論
title_with_count: '評論 (%{new_count} new)'
Expand Down
9 changes: 6 additions & 3 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,12 @@
get :tags
end
end

resources :categories


get 'categories/*path/children', to: 'categories#children', as: 'children_categories'
resources :categories do
post :update_positions, on: :collection
end

resources :comments do
collection do
get :approved
Expand Down
4 changes: 3 additions & 1 deletion db/migrate/20110803223524_acts_as_taggable_on_migration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ def up
t.references :taggable, :polymorphic => true
t.references :tagger, :polymorphic => true

t.string :context
# Limit is created to prevent MySQL error on index
# length for MyISAM table type: http://bit.ly/vgW2Ql
t.string :context, limit: 128

t.datetime :created_at
end
Expand Down
9 changes: 9 additions & 0 deletions db/migrate/20141110151636_add_missing_taggable_index.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class AddMissingTaggableIndex < ActiveRecord::Migration
def self.up
add_index :taggings, [:taggable_id, :taggable_type, :context]
end

def self.down
remove_index :taggings, [:taggable_id, :taggable_type, :context]
end
end
20 changes: 20 additions & 0 deletions db/migrate/20141113153703_add_sorting_fields_to_categories.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
class AddSortingFieldsToCategories < ActiveRecord::Migration

def change
add_column :refinery_blog_categories, :position, :integer

add_column :refinery_blog_categories, :parent_id, :integer
add_column :refinery_blog_categories, :lft, :integer
add_column :refinery_blog_categories, :rgt, :integer
add_column :refinery_blog_categories, :depth, :integer


add_index :refinery_blog_categories, :position
add_index :refinery_blog_categories, :parent_id
add_index :refinery_blog_categories, :lft
add_index :refinery_blog_categories, :rgt
add_index :refinery_blog_categories, :depth

end

end
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,9 @@ Refinery::Blog.configure do |config|

# If you're grafting onto an existing app, change this to your User class
# Refinery::Blog.user_class = <%= Refinery::Blog.user_class.to_s.inspect %>

# If categories is not sortable, make it false;
# config.category_orderable = <%= Refinery::Blog.category_orderable.inspect %>

# config.scope_slug_by_parent = <%= Refinery::Blog.scope_slug_by_parent.inspect %>
end
7 changes: 5 additions & 2 deletions lib/refinery/blog/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,18 @@ module Blog
include ActiveSupport::Configurable

config_accessor :validate_source_url, :comments_per_page, :posts_per_page,
:post_teaser_length, :share_this_key, :page_url
:post_teaser_length, :share_this_key, :page_url, :category_orderable, :scope_slug_by_parent, :auto_expand_admin_tree

self.validate_source_url = false
self.comments_per_page = 10
self.posts_per_page = 10
self.post_teaser_length = 250
self.share_this_key = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
self.page_url = "/blog"

self.category_orderable = true
self.scope_slug_by_parent = true
self.auto_expand_admin_tree = true

# Refinery::User isn't available when this line gets hit, so we use static methods instead
@@user_class_name = nil
class << self
Expand Down
Loading