Skip to content

Commit

Permalink
Fixes resolve/refinerycms-search#16
Browse files Browse the repository at this point in the history
This works by allowing refinery.url_for(Refinery.route_for_model(result.class, :admin => false)) to be used in place of refinery.url_for(result)
  • Loading branch information
parndt committed May 14, 2012
1 parent b013343 commit 59970c6
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 49 deletions.
2 changes: 1 addition & 1 deletion core/app/views/refinery/admin/_form_actions.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
cancel_title = t('.cancel_lose_changes')
end
cancel_button_id ||= "cancel_button"
cancel_url ||= (((back = url_for(:back)).include?('javascript') or action_name =~ /^(create|update)$/) ? refinery.send(Refinery.route_for_model(f.object.class, true)) : back)
cancel_url ||= (((back = url_for(:back)).include?('javascript') or action_name =~ /^(create|update)$/) ? refinery.send(Refinery.route_for_model(f.object.class, :plural => true)) : back)
end

continue_editing = defined?(continue_editing) ? continue_editing : (f.object.present? and f.object.persisted?)
Expand Down
38 changes: 27 additions & 11 deletions core/lib/refinery/core.rb
Original file line number Diff line number Diff line change
Expand Up @@ -137,23 +137,39 @@ def version
Refinery::Version.to_s
end

# Returns string version of url helper path. We need this to temporary support namespaces
# Returns string version of url helper path. We need this to temporarily support namespaces
# like Refinery::Image and Refinery::Blog::Post
#
# Example:
# Refinery.route_for_model("Refinery::Image") => "admin_image_path"
# Refinery.route_for_model(Refinery::Image, true) => "admin_images_path"
# Refinery.route_for_model(Refinery::Image) => "admin_image_path"
# Refinery.route_for_model(Refinery::Image, {:plural => true}) => "admin_images_path"
# Refinery.route_for_model(Refinery::Blog::Post) => "blog_admin_post_path"
# Refinery.route_for_model(Refinery::Blog::Post, true) => "blog_admin_posts_path"
def route_for_model(klass, plural = false)
parts = klass.to_s.underscore.split('/').delete_if { |p| p.blank? }
# Refinery.route_for_model(Refinery::Blog::Post, {:plural => true}) => "blog_admin_posts_path"
# Refinery.route_for_model(Refinery::Blog::Post, {:admin => false}) => "blog_post_path"
def route_for_model(klass, options = {})
if [TrueClass, FalseClass].include? options.class
options = {:plural => options}
Refinery.deprecate "[Refinery.route_for_model] 'plural' argument",
:when => '2.1',
:replacement => '{:plural => false}'
end

options = {:plural => false, :admin => true}.merge options

klass = klass.constantize if klass.respond_to?(:constantize)
active_name = ActiveModel::Name.new(klass, (Refinery if klass.parents.include?(Refinery)))

if options[:admin]
# Most of the time this gets rid of 'refinery'
parts = active_name.underscore.split('/').reject{|name| active_name.singular_route_key.exclude?(name)}
resource_name = parts.pop
resource_name = options[:plural] ? resource_name.pluralize : resource_name.singularize

resource_name = plural ? parts[-1].pluralize : parts[-1]
[parts.join("_"), "admin", resource_name, "path"].reject(&:blank?).join "_"
else
path = options[:plural] ? active_name.route_key : active_name.singular_route_key

if parts.size == 2
"admin_#{resource_name}_path"
elsif parts.size > 2
[parts[1..-2].join("_"), "admin", resource_name, "path"].join("_")
[path, 'path'].join '_'
end
end

Expand Down
2 changes: 1 addition & 1 deletion core/lib/refinery/crud.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def self.default_options(model_name)
:order => ('position ASC' if this_class.table_exists? && this_class.column_names.include?('position')),
:paging => true,
:per_page => false,
:redirect_to_url => "refinery.#{Refinery.route_for_model(class_name.constantize, true)}",
:redirect_to_url => "refinery.#{Refinery.route_for_model(class_name.constantize, :plural => true)}",
:searchable => true,
:search_conditions => '',
:sortable => true,
Expand Down
44 changes: 26 additions & 18 deletions core/spec/lib/refinery/activity_spec.rb
Original file line number Diff line number Diff line change
@@ -1,38 +1,46 @@
require "spec_helper"
require 'spec_helper'

describe Refinery::Activity do
before { module X; module Y; class Z; end; end; end }

let(:activity) { Refinery::Activity.new(:class_name => "X::Y::Z") }
let(:activity) { Refinery::Activity.new(:class_name => 'X::Y::Z') }

describe "#base_class_name" do
it "should return the base class name, less module nesting" do
activity.base_class_name.should == "Z"
describe '#base_class_name' do
it 'returns the base class name, less module nesting' do
activity.base_class_name.should == 'Z'
end
end

describe "#klass" do
it "returns class constant" do
describe '#klass' do
it 'returns class constant' do
activity.klass.should == X::Y::Z
end
end

describe "#url_prefix" do
it "returns edit_ by default" do
activity.url_prefix.should == "edit_"
describe '#url_prefix' do
it 'returns edit_ by default' do
activity.url_prefix.should == 'edit_'
end

it "returns user specified prefix" do
activity.url_prefix = "testy"
activity.url_prefix.should == "testy_"
activity.url_prefix = "testy_"
activity.url_prefix.should == "testy_"
it 'returns user specified prefix' do
activity.url_prefix = 'testy'
activity.url_prefix.should == 'testy_'
activity.url_prefix = 'testy_'
activity.url_prefix.should == 'testy_'
end
end

describe "#url" do
it "should return the url" do
activity.url.should == "refinery.edit_y_admin_z_path"
describe '#url' do
it 'returns the url' do
activity.url.should == 'refinery.edit_x_y_admin_z_path'
end
end

describe '#url with Refinery namespace' do
before { module Refinery; module Y; class Z; end; end; end }
let(:activity) { Refinery::Activity.new(:class_name => 'Refinery::Y::Z') }
it 'returns the url' do
activity.url.should == 'refinery.edit_y_admin_z_path'
end
end
end
59 changes: 41 additions & 18 deletions core/spec/lib/refinery/core_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -137,39 +137,62 @@
end

describe ".route_for_model" do
context "when passed Refinery::Dummy" do
context 'with Refinery::Dummy' do
module Refinery::Dummy
end

it "returns admin_dummy_path" do
Refinery.route_for_model("Refinery::Dummy").should == "admin_dummy_path"
Refinery.route_for_model(Refinery::Dummy).should == "admin_dummy_path"
end
end

context "when passed Refinery::Dummy and true" do
it "returns admin_dummies_path" do
Refinery.route_for_model("Refinery::Dummy", true).should == "admin_dummies_path"
context ":plural => true" do
it "returns admin_dummies_path" do
Refinery.route_for_model(Refinery::Dummy, :plural => true).should == "admin_dummies_path"
end
end
end

context "when passed Refinery::DummyName" do
context 'with Refinery::DummyName' do
module Refinery::DummyName
end

it "returns admin_dummy_name_path" do
Refinery.route_for_model("Refinery::DummyName").should == "admin_dummy_name_path"
Refinery.route_for_model(Refinery::DummyName).should == "admin_dummy_name_path"
end
end

context "when passed Refinery::DummyName and true" do
it "returns admin_dummy_names_path" do
Refinery.route_for_model("Refinery::DummyName", true).should == "admin_dummy_names_path"
context ":plural => true" do
it "returns admin_dummy_names_path" do
Refinery.route_for_model(Refinery::DummyName, :plural => true).should == "admin_dummy_names_path"
end
end
end

context "when passed Refinery::Dummy::Name" do
context 'with Refinery::Dummy::Name' do
module Refinery::Dummy
module Name
end
end

it "returns dummy_admin_name_path" do
Refinery.route_for_model("Refinery::Dummy::Name").should == "dummy_admin_name_path"
Refinery.route_for_model(Refinery::Dummy::Name).should == "dummy_admin_name_path"
end

context ":plural => true" do
it "returns dummy_admin_names_path" do
Refinery.route_for_model(Refinery::Dummy::Name, :plural => true).should == "dummy_admin_names_path"
end
end

context ":admin => false" do
it "returns dummy_name_path" do
Refinery.route_for_model(Refinery::Dummy::Name, :admin => false).should == 'dummy_name_path'
end
end
end

context "when passed Refinery::Dummy::Name and true" do
it "returns dummy_admin_names_path" do
Refinery.route_for_model("Refinery::Dummy::Name", true).should == "dummy_admin_names_path"
context ":admin => false, :plural => true" do
it "returns dummy_names_path" do
Refinery.route_for_model(Refinery::Dummy::Name, :admin => false, :plural => true).should == 'dummy_names_path'
end
end
end
end
Expand Down

0 comments on commit 59970c6

Please sign in to comment.