Skip to content

Commit

Permalink
Removing 'node_name' and changed fullpath/basepath from name based pa…
Browse files Browse the repository at this point in the history
…ths to zip based paths. Default sort is using position + title. All tests pass.
  • Loading branch information
anna-bucher committed Oct 2, 2010
1 parent 1c986f1 commit 5a3fb74
Show file tree
Hide file tree
Showing 144 changed files with 2,380 additions and 1,985 deletions.
1 change: 1 addition & 0 deletions TODO_ZENA_1_0
Expand Up @@ -7,3 +7,4 @@ TODO
3. Rename virtual_classes_controller to roles_controller
4. kill node_name

10. secure XML API (get cookie: do not send login each time)
15 changes: 9 additions & 6 deletions app/controllers/nodes_controller.rb
Expand Up @@ -227,7 +227,7 @@ def edit

respond_to do |format|
format.html do
@title_for_layout = @node.rootpath
@title_for_layout = @node.title
end

format.js do
Expand Down Expand Up @@ -366,7 +366,7 @@ def update
if @node.errors.empty?
render :xml => @node.to_xml, :status => :ok, :location => node_url(@node)
else
render :xml => @page.errors, :status => :unprocessable_entity
render :xml => @node.errors, :status => :unprocessable_entity
end
end # xml
end
Expand All @@ -389,6 +389,9 @@ def attribute
node_id = secure!(Node) { Node.translate_pseudo_id(id_query, :id, @node)}
@node = secure!(Node) { Node.find(node_id) }
elsif name_query = params[:name]
# ????
raise Exception.new "Attribute by name should be fixed or removed"

if name_query =~ /^(.*)\.[a-z]{2,3}$/
name_query = $1
end
Expand All @@ -400,7 +403,7 @@ def attribute
conditions << "#{kpath}%"
end

conditions[0] << "node_name LIKE ?"
conditions[0] << "title LIKE ?" # ???
conditions << "#{name_query}%"

conditions[0] = conditions[0].join(' AND ')
Expand Down Expand Up @@ -484,7 +487,7 @@ def clear_order
# archive ---> fullpath
def find_node
if path = params[:path]
if path.last =~ /\A(([a-zA-Z]+)([0-9]+)|([a-zA-Z0-9\-\*]+))(_[a-zA-Z]+|)(\..+|)\Z/
if path.last =~ Zena::Use::Urls::ALLOWED_REGEXP
zip = $3
name = $4
params[:mode] = $5 == '' ? nil : $5[1..-1]
Expand All @@ -499,7 +502,7 @@ def find_node
if name =~ /^\d+$/
@node = secure!(Node) { Node.find_by_zip(name) }
elsif name
basepath = (path[0..-2] + [name]).join('/')
basepath = (path[0..-2] + [name]).map! {|p| String.from_url_name(p) }.join('/')
@node = secure!(Node) { Node.find_by_path(basepath) }
else
@node = secure!(Node) { Node.find_by_zip(zip) }
Expand All @@ -516,7 +519,7 @@ def find_node
@link = Link.find_through(@node, params[:link_id])
end

@title_for_layout = @node.rootpath if @node
@title_for_layout = @node.title if @node
end

def set_format(format)
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/versions_controller.rb
Expand Up @@ -63,7 +63,7 @@ def edit
end
render :action=>'update'
else
@title_for_layout = @node.rootpath
@title_for_layout = @node.title
if @node.kind_of?(TextDocument)
if params['parse_assets']
@node.text = @node.parse_assets(@node.text, self, 'text')
Expand Down
67 changes: 36 additions & 31 deletions app/models/cache.rb
Expand Up @@ -4,44 +4,49 @@ class Cache < ActiveRecord::Base
cattr_accessor :perform_caching
before_save :set_site_id

# The code is here too bad to be kept.
# TODO: Rewrite
class << self

def with(visitor_id, visitor_groups, kpath, *context)
return yield unless perform_caching
if cached = self.find(:first, :conditions => ["visitor_id = ? AND site_id = ? AND context = ?", visitor_id, visitor.site.id, context.join('.').hash.abs])
cached[:content]
else
content = yield
self.create(:visitor_id=>visitor_id, :visitor_groups=>".#{visitor_groups.join('.')}.", :kpath=>kpath,
:context=>context.join('.').hash.abs, :content=>content )
content
end
yield

# return yield unless perform_caching
# if cached = self.find(:first, :conditions => ["visitor_id = ? AND site_id = ? AND context = ?", visitor_id, visitor.site.id, context.join('.').hash.abs])
# cached[:content]
# else
# content = yield
# self.create(:visitor_id=>visitor_id, :visitor_groups=>".#{visitor_groups.join('.')}.", :kpath=>kpath,
# :context=>context.join('.').hash.abs, :content=>content )
# content
# end
end

# We can provide a kpath selector for sweeping. If the kpath is in the cached scope, the cache is removed.
def sweep(hash)
if kpath = hash[:kpath]
klasses = []
kpath.split(//).each_index { |i| klasses << kpath[0..i] }
kpath_selector = " AND kpath IN (#{klasses.map{|k| connection.quote(k)}.join(',')})"
else
kpath_selector = ""
end
if hash[:visitor_id]
self.connection.execute "DELETE FROM #{self.table_name} WHERE visitor_id = '#{hash[:visitor_id]}'" + kpath_selector
end
if hash[:visitor_groups]
hash[:visitor_groups].each do |g|
self.connection.execute "DELETE FROM #{self.table_name} WHERE visitor_groups LIKE '%.#{g}.%'" + kpath_selector
end
end
if hash[:context]
context = [hash[:context]].flatten.join('.').hash.abs
self.connection.execute "DELETE FROM #{self.table_name} WHERE context = '#{context}'" + kpath_selector
end
if hash[:older_than]
self.connection.execute "DELETE FROM #{self.table_name} WHERE updated_at < '#{hash[:older_than]}'" + kpath_selector
end
return
# if kpath = hash[:kpath]
# klasses = []
# kpath.split(//).each_index { |i| klasses << kpath[0..i] }
# kpath_selector = " AND kpath IN (#{klasses.map{|k| connection.quote(k)}.join(',')})"
# else
# kpath_selector = ""
# end
# if hash[:visitor_id]
# self.connection.execute "DELETE FROM #{self.table_name} WHERE visitor_id = '#{hash[:visitor_id]}'" + kpath_selector
# end
# if hash[:visitor_groups]
# hash[:visitor_groups].each do |g|
# self.connection.execute "DELETE FROM #{self.table_name} WHERE visitor_groups LIKE '%.#{g}.%'" + kpath_selector
# end
# end
# if hash[:context]
# context = [hash[:context]].flatten.join('.').hash.abs
# self.connection.execute "DELETE FROM #{self.table_name} WHERE context = '#{context}'" + kpath_selector
# end
# if hash[:older_than]
# self.connection.execute "DELETE FROM #{self.table_name} WHERE updated_at < '#{hash[:older_than]}'" + kpath_selector
# end
end
end

Expand Down
35 changes: 10 additions & 25 deletions app/models/document.rb
Expand Up @@ -46,9 +46,10 @@ class Document < Node
safe_property :size, :content_type, :ext
safe_method :filename => String, :file => File, :filepath => String

validate :valid_file
validate :valid_content_type
after_save :clear_new_file
validate :make_unique_title
validate :valid_file
validate :valid_content_type
after_save :clear_new_file

class << self

Expand All @@ -71,8 +72,6 @@ def new(attrs = {})
content_type = file.content_type
elsif ct = attrs['content_type']
content_type = ct
elsif attrs['node_name'] =~ /^.*\.(\w+)$/ && types = Zena::EXT_TO_TYPE[$1.downcase]
content_type = types[0]
elsif attrs['title'] =~ /^.*\.(\w+)$/ && types = Zena::EXT_TO_TYPE[$1.downcase]
content_type = types[0]
end
Expand Down Expand Up @@ -144,6 +143,7 @@ def image?
end

# Get the document's public filename using the name and the file extension.
# FIXME: shouldn't we use title here ?
def filename
version.attachment.filename
end
Expand All @@ -153,11 +153,6 @@ def filepath(format=nil)
version.attachment.filepath(format)
end

# Get the node's rootpath with the file's extention.
def rootpath
super + ".#{prop['ext']}"
end

protected
def set_defaults
set_defaults_from_file
Expand All @@ -168,10 +163,6 @@ def set_defaults
self.title = $1
end

if node_name.to_s =~ /\A(.*)\.#{self.ext}$/i
self.node_name = $1
end

super

set_attachment_filename
Expand Down Expand Up @@ -224,20 +215,14 @@ def set_defaults_from_file
return unless @new_file
self.content_type = @new_file.content_type unless prop.content_type_changed?

if base = node_name || title || @new_file.original_filename
if base =~ /(.*)\.(\w+)$/
self.node_name = $1 if new_record?
else
self.node_name = base if new_record?
end
if base = @new_file.original_filename
self.title = base if title.blank?
end
end

# Make sure node_name is unique. This should be run after sync_node_name, this is why we
# hack around the name and use super.
def sync_node_name
super
get_unique_node_name_in_scope('ND%')
# Make sure title is unique. This should be run after prop_eval.
def make_unique_title
get_unique_title_in_scope('ND')
end

def get_extension
Expand Down

0 comments on commit 5a3fb74

Please sign in to comment.