Skip to content

Commit

Permalink
Extend friendly_id to drupal_node model
Browse files Browse the repository at this point in the history
Add update method to update node path

Modify show action in controllers for finding object by slugs
  • Loading branch information
ananyo2012 committed Jul 6, 2016
1 parent 549a2f8 commit 5d29f8b
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 23 deletions.
9 changes: 9 additions & 0 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -158,4 +158,13 @@ def comments_node_and_path
end
end

def redirect_old_urls
# If an old id or a numeric id was used to find the record, then
# the request path will not match the notes path, and we should do
# a 301 redirect that uses the current friendly id.
if request.path != @node.path
return redirect_to @node.path, :status => :moved_permanently
end
end

end
5 changes: 4 additions & 1 deletion app/controllers/map_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ def index
end

def show
@node = DrupalNode.find_map_by_slug(params[:name]+'/'+params[:date])
@node = DrupalNode.find params[:name]

redirect_old_urls

@node.view
@title = @node.title
@tags = @node.tags
Expand Down
3 changes: 2 additions & 1 deletion app/controllers/notes_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,9 @@ def raw

def show
if params[:author] && params[:date]
@node = DrupalNode.where(path: "/notes/#{params[:author]}/#{params[:date]}/#{params[:id]}").first
@node = DrupalNode.find params[:id]
@node = @node || DrupalNode.where(path: "/report/#{params[:id]}").first
redirect_old_urls
else
@node = DrupalNode.find params[:id]
end
Expand Down
5 changes: 4 additions & 1 deletion app/controllers/questions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,11 @@ def index

def show
if params[:author] && params[:date]
@node = DrupalNode.where(path: "/notes/#{params[:author]}/#{params[:date]}/#{params[:id]}").first
@node = DrupalNode.find params[:id]
@node = @node || DrupalNode.where(path: "/report/#{params[:id]}").first
if request.path != @node.path(:question)
return redirect_to @node.path(:question), :status => :moved_permanently
end
else
@node = DrupalNode.find params[:id]
end
Expand Down
15 changes: 6 additions & 9 deletions app/controllers/wiki_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ def subdomain
end

def show
if params[:lang]
@node = DrupalNode.find_by_slug(params[:lang]+"/"+params[:id])
else
@node = DrupalNode.find_by_slug(params[:id])
@node = DrupalNode.find params[:id]

if request.path != @node.path && request.path != '/wiki/' + @node.nid.to_s
return redirect_to @node.path, :status => :moved_permanently
end

return if check_and_redirect_node(@node)
Expand Down Expand Up @@ -63,11 +63,7 @@ def raw
end

def edit
if params[:lang]
@node = DrupalNode.find_by_slug(params[:lang]+"/"+params[:id])
else
@node = DrupalNode.find_by_slug(params[:id])
end
@node = DrupalNode.find params[:id]
if ((Time.now.to_i - @node.latest.timestamp) < 5.minutes.to_i) && @node.latest.author.uid != current_user.uid
flash.now[:warning] = "Someone has clicked 'Edit' less than 5 minutes ago; be careful not to overwrite each others' edits!"
end
Expand Down Expand Up @@ -142,6 +138,7 @@ def update
i.vid = @revision.vid
i.save
end
@node.title = @revision.title
# save main image
if params[:main_image] && params[:main_image] != ""
begin
Expand Down
42 changes: 31 additions & 11 deletions app/models/drupal_node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,17 @@ def validate(record)
end

class DrupalNode < ActiveRecord::Base
attr_accessible :title, :uid, :status, :type, :vid, :cached_likes, :comment, :path
attr_accessible :title, :uid, :status, :type, :vid, :cached_likes, :comment, :path, :slug
self.table_name = 'node'
self.primary_key = 'nid'

extend FriendlyId
friendly_id :title, use: [:slugged, :history]

def should_generate_new_friendly_id?
slug.blank? || title_changed?
end

has_many :drupal_node_revision, :foreign_key => 'nid', :dependent => :destroy
# wasn't working to tie it to .vid, manually defining below
# has_one :drupal_main_image, :foreign_key => 'vid', :dependent => :destroy
Expand All @@ -41,7 +48,7 @@ class DrupalNode < ActiveRecord::Base

validates :title, :presence => :true
validates_with UniqueUrlValidator, :on => :create
validates :path, :uniqueness => { :scope => :nid, :message => "This title has already been taken" }
validates :path, :uniqueness => { :message => "This title has already been taken" }

# making drupal and rails database conventions play nice;
# 'changed' is a reserved word in rails
Expand All @@ -62,6 +69,7 @@ def self.inheritance_column
before_save :set_changed_and_created
after_create :setup
before_validation :set_path, on: :create
after_save :update_path

# can switch to a "question-style" path if specified
def path(type = :default)
Expand Down Expand Up @@ -92,6 +100,18 @@ def set_path
self.path = self.generate_path if self.path.blank? && !self.title.blank?
end

def update_path
new_path = if self.type == 'note'
username = DrupalUsers.find_by_uid(self.uid).name
"/notes/#{username}/#{Time.at(self.created).strftime("%m-%d-%Y")}/#{self.friendly_id}"
elsif self.type == 'page'
"/wiki/" + self.friendly_id
elsif self.type == 'map'
"/map/#{self.friendly_id}/#{Time.at(self.created).strftime("%m-%d-%Y")}"
end
self.update_column(:path, new_path)
end

def set_changed_and_created
self['changed'] = DateTime.now.to_i
end
Expand Down Expand Up @@ -399,9 +419,9 @@ def totalcount
# URL-related methods:

# is this used anymore? deprecate?
def slug
self.path.split('/').last
end
# def slug
# self.path.split('/').last
# end

def edit_path
if self.type == "page" || self.type == "tool" || self.type == "place"
Expand All @@ -412,17 +432,17 @@ def edit_path
path
end

def self.find_by_slug(title)
DrupalNode.where(path: ["/#{title}", "/tool/#{title}", "/wiki/#{title}", "/place/#{title}"]).first
end
# def self.find_by_slug(title)
# DrupalNode.where(path: ["/#{title}", "/tool/#{title}", "/wiki/#{title}", "/place/#{title}"]).first
# end

def self.find_root_by_slug(title)
DrupalNode.where(path: ["/#{title}"]).first
end

def self.find_map_by_slug(title)
DrupalNode.where(path: "/map/#{title}").first
end
# def self.find_map_by_slug(title)
# DrupalNode.where(path: "/map/#{title}").first
# end

def map
# This fires off a query that orders by vid DESC
Expand Down

0 comments on commit 5d29f8b

Please sign in to comment.