Skip to content

Commit

Permalink
renamed association columns on MenuLink from refinery_resource to jus…
Browse files Browse the repository at this point in the history
…t resource
  • Loading branch information
jokklan committed Mar 26, 2013
1 parent 2a0a9f6 commit 5bc85b4
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 32 deletions.
6 changes: 3 additions & 3 deletions app/controllers/refinery/menus/admin/menu_links_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ def create
respond_to do |format|
format.js do
@menu_links = []
if params[:refinery_resource_ids]
params[:refinery_resource_ids].each do |id|
@menu_links << MenuLink.create({refinery_resource_id: id}.merge(params[:menu_link]))
if params[:resource_ids]
params[:resource_ids].each do |id|
@menu_links << MenuLink.create({resource_id: id}.merge(params[:menu_link]))
end
else
@menu_links << MenuLink.create(params[:menu_link])
Expand Down
12 changes: 6 additions & 6 deletions app/models/refinery/menus/menu_link.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ module Menus
class MenuLink < Refinery::Core::BaseModel
self.table_name = "refinery_menus_links"

attr_accessible :parent_id, :refinery_page_id, :refinery_menu_id, :refinery_resource_id, :refinery_resource_type,
attr_accessible :parent_id, :refinery_page_id, :refinery_menu_id, :resource, :resource_id, :resource_type,
:title_attribute, :custom_url, :label, :menu, :id_attribute, :class_attribute

belongs_to :menu, :class_name => '::Refinery::Menus::Menu', :foreign_key => :refinery_menu_id
belongs_to :resource, :foreign_key => :refinery_resource_id, :polymorphic => true
belongs_to :resource, :polymorphic => true

# Docs for acts_as_nested_set https://github.com/collectiveidea/awesome_nested_set
# rather than :delete_all we want :destroy
Expand Down Expand Up @@ -59,24 +59,24 @@ def resource_config
end

def resource_type
refinery_resource_type || "Custom link"
super || "Custom link"
end

def type_name
resource_type.titleize
end

def custom_link?
refinery_resource_id.nil? || refinery_resource_type.nil?
resource_id.nil? || resource_type.nil?
end

def resource_link?
refinery_resource_id.present? && refinery_resource_type.present?
resource_id.present? && resource_type.present?
end

def resource
return nil if custom_link?
resource_klass.find(refinery_resource_id)
resource_klass.find(resource_id)
end

def resource_title
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
<%= content_tag :div, class: "pp-add-box resource-pp-add-box" do %>
<%= form_for [refinery, :menus, :admin, @menu.links.build(refinery_resource_type: resource_type)], remote: true do |f| %>
<%= form_for [refinery, :menus, :admin, @menu.links.build(resource_type: resource_type)], remote: true do |f| %>
<h3><%= resource_type.to_s.titleize %></h3>
<div class='pp-selection-box'>
<ul class='checkboxes'>
<%= f.hidden_field :refinery_menu_id %>
<%= f.hidden_field :refinery_resource_type %>
<%= f.hidden_field :resource_type %>

<% resources = Refinery::Menus::MenuLink.find_all_of_type(resource_type) %>
<% resources.each_with_index do |object, index| %>
<li>
<%= check_box_tag "refinery_resource_ids[]", object.id %>
<%= label_tag "refinery_resource_ids[]", object.send(Refinery::Menus::MenuLink.resource_config(resource_type)[:title_attr]) %>
<%= check_box_tag "resource_ids[]", object.id %>
<%= label_tag "resource_ids[]", object.send(Refinery::Menus::MenuLink.resource_config(resource_type)[:title_attr]) %>
</li>
<% end %>
</ul>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class RenameRefineryResourceToResourceForRefineryMenusMenuLinks < ActiveRecord::Migration
def change
rename_column :refinery_menus_links, :refinery_resource_id, :resource_id
rename_column :refinery_menus_links, :refinery_resource_type, :resource_type
end
end
4 changes: 2 additions & 2 deletions spec/factories/factory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
label "label"

factory :menu_link_with_resource do
refinery_resource_id 1
refinery_resource_type 'refinery_resource'
resource_id 1
resource_type 'refinery_resource'
end
end
end
34 changes: 17 additions & 17 deletions spec/models/refinery/menus/menu_link_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ module Menus
end

it "should set label to resource title if resource link" do
@menu_link = FactoryGirl.build(:menu_link, label: nil, refinery_resource_id: 1, refinery_resource_type: "refinery_resource")
@menu_link = FactoryGirl.build(:menu_link, label: nil, resource_id: 1, resource_type: "refinery_resource")
@resource = double(:send => "Some Resource", :title => "Some Resource")
@menu_link.stub(:resource).and_return(@resource)
@menu_link.stub(:resource_config).and_return({:title_attr => "title"})
Expand All @@ -173,7 +173,7 @@ module Menus
describe "#resource_klass" do
it "should call class method resource_klass" do
Refinery::Menus::MenuLink.should_receive(:resource_klass).with("refinery_resource")
@menu_link = FactoryGirl.build(:menu_link, refinery_resource_type: "refinery_resource")
@menu_link = FactoryGirl.build(:menu_link, resource_type: "refinery_resource")

@menu_link.resource_klass
end
Expand All @@ -182,15 +182,15 @@ module Menus
describe "#resource_config" do
it "should call class method resource_config" do
Refinery::Menus::MenuLink.should_receive(:resource_config).with("refinery_resource")
@menu_link = FactoryGirl.build(:menu_link, refinery_resource_type: "refinery_resource")
@menu_link = FactoryGirl.build(:menu_link, resource_type: "refinery_resource")

@menu_link.resource_config
end
end

describe "#resource_type" do
it "should return refinery_resource_type if exists" do
@menu_link = FactoryGirl.build(:menu_link, refinery_resource_type: "refinery_resource")
it "should return resource_type if exists" do
@menu_link = FactoryGirl.build(:menu_link, resource_type: "refinery_resource")
@menu_link.resource_type.should == "refinery_resource"
end

Expand All @@ -210,35 +210,35 @@ module Menus
end

describe "#custom_link?" do
it "should return true if refinery_resource_id is nil" do
@menu_link = FactoryGirl.build(:menu_link, refinery_resource_id: nil)
it "should return true if resource_id is nil" do
@menu_link = FactoryGirl.build(:menu_link, resource_id: nil)
@menu_link.custom_link?.should be_true
end

it "should return true if refinery_resource_type is nil" do
@menu_link = FactoryGirl.build(:menu_link, refinery_resource_type: nil)
it "should return true if resource_type is nil" do
@menu_link = FactoryGirl.build(:menu_link, resource_type: nil)
@menu_link.custom_link?.should be_true
end

it "should return false if refinery_resource_id and refinery_resource_type is present" do
@menu_link = FactoryGirl.build(:menu_link, refinery_resource_type: "refinery_resource", refinery_resource_id: 1)
it "should return false if resource_id and resource_type is present" do
@menu_link = FactoryGirl.build(:menu_link, resource_type: "refinery_resource", resource_id: 1)
@menu_link.custom_link?.should be_false
end
end

describe "#resource_link?" do
it "should return false if refinery_resource_id is nil" do
@menu_link = FactoryGirl.build(:menu_link, refinery_resource_id: nil)
it "should return false if resource_id is nil" do
@menu_link = FactoryGirl.build(:menu_link, resource_id: nil)
@menu_link.resource_link?.should be_false
end

it "should return false if refinery_resource_type is nil" do
@menu_link = FactoryGirl.build(:menu_link, refinery_resource_type: nil)
it "should return false if resource_type is nil" do
@menu_link = FactoryGirl.build(:menu_link, resource_type: nil)
@menu_link.resource_link?.should be_false
end

it "should return true if refinery_resource_id and refinery_resource_type is present" do
@menu_link = FactoryGirl.build(:menu_link, refinery_resource_type: "refinery_resource", refinery_resource_id: 1)
it "should return true if resource_id and resource_type is present" do
@menu_link = FactoryGirl.build(:menu_link, resource_type: "refinery_resource", resource_id: 1)
@menu_link.resource_link?.should be_true
end
end
Expand Down

0 comments on commit 5bc85b4

Please sign in to comment.