Skip to content

Commit

Permalink
Changed 'root' and 'orphan' definitions to 'home' and 'root'.
Browse files Browse the repository at this point in the history
  • Loading branch information
gaspard committed Jul 16, 2013
1 parent 68d440f commit 839b05f
Show file tree
Hide file tree
Showing 22 changed files with 165 additions and 163 deletions.
6 changes: 6 additions & 0 deletions History.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
== 1.2.6

* Major changes
* Support for multiple domains (site alias) for a single site.

== 1.2.5 2013-07-15

* Major changes
Expand All @@ -6,6 +11,7 @@
* WARN: Properties in list context do not resolve with first element anymore. "first" prefix needed.
* Enable VirtualClass on native classes !
* Added 'activity' brick which records visitor's last "seen_at" date. <== TODO: Document
* Support for site alias (other domain pointing to same data) "rake mkalias". <== TODO: Document

* Minor changes
* Fixed encode_params in Ajax requests.
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/nodes_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class NodesController < ApplicationController
layout :popup_layout, :only => [:edit, :import]

def index
if @node = secure(Node) { Node.find(current_site.root_id) }
if @node = secure(Node) { Node.find(current_site.home_id) }
respond_to do |format|
format.html { render_and_cache :mode => '+index' }
format.xml { render :xml => @node.to_xml }
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/versions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ def show
respond_to do |format|

format.html {
if @node.id == current_site.root_id
if @node.id == current_site.home_id
render_and_cache :cache => false, :mode => '+index'
insert_warning
else
Expand Down
58 changes: 0 additions & 58 deletions app/models/node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1280,64 +1280,6 @@ def project_zip
# Id to zip mapping for user_id. Used by zafu and forms.
def user_zip; self[:user_id]; end

# transform to another class
# def vclass=(new_class)
# if new_class.kind_of?(String)
# klass = Module.const_get(new_class)
# else
# klass = new_class
# end
# raise NameError if !klass.ancestors.include?(Node) || klass.version_class != self.class.content_class
#
#
#
# rescue NameError
# errors.add('klass', 'invalid')
# end

# transform an Node into another Object. This is a two step operation :
# 1. create a new object with the attributes from the old one
# 2. move old object out of the way (setting parent_id and section_id to -1)
# 3. try to save new object
# 4. delete old and set new object id to old
# THIS IS DANGEROUS !! NEEDS TESTING
# def change_to(klass)
# return nil if self[:id] == current_site[:root_id]
# # ==> Check for class specific information (file to remove, participations, tags, etc) ... should we leave these things and
# # not care ?
# # ==> When changing into something else : update version type and data !!!
# my_id = self[:id].to_i
# my_parent = self[:parent_id].to_i
# my_project = self[:section_id].to_i
# connection = self.class.connection
# # 1. create a new object with the attributes from the old one
# new_obj = secure!(klass) { klass.new(self.attributes) }
# # 2. move old object out of the way (setting parent_id and section_id to -1)
# self.class.connection.execute "UPDATE #{self.class.table_name} SET parent_id='0', section_id='0' WHERE id=#{my_id}"
# # 3. try to save new object
# if new_obj.save
# tmp_id = new_obj[:id]
# # 4. delete old and set new object id to old. Delete tmp Version.
# self.class.connection.execute "DELETE FROM #{self.class.table_name} WHERE id=#{my_id}"
# self.class.connection.execute "DELETE FROM #{Version.table_name} WHERE node_id=#{tmp_id}"
# self.class.connection.execute "UPDATE #{self.class.table_name} SET id='#{my_id}' WHERE id=#{tmp_id}"
# self.class.connection.execute "UPDATE #{self.class.table_name} SET section_id=id WHERE id=#{my_id}" if new_obj.kind_of?(Section)
# self.class.logger.info "[#{self[:id]}] #{self.class} --> #{klass}"
# if new_obj.kind_of?(Section)
# # update section_id for children
# sync_section(my_id)
# elsif self.kind_of?(Section)
# # update section_id for children
# sync_section(parent[:section_id])
# end
# secure ( klass ) { klass.find(my_id) }
# else
# # set object back
# self.class.connection.execute "UPDATE #{self.class.table_name} SET parent_id='#{my_parent}', section_id='#{my_project}' WHERE id=#{my_id}"
# self
# end
# end

# Find the discussion for the current context (v_status and v_lang). This automatically creates a new #Discussion if there is
# no closed or open discussion for the current lang and Node#can_auto_create_discussion? is true
def discussion
Expand Down
61 changes: 34 additions & 27 deletions app/models/site.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
The #Site model holds configuration information for a site:
+host+:: Unique host name. (teti.ch, zenadmin.org, dev.example.org, ...)
+orphan_id+:: Site seed node id. This is the only node in the site without a parent.
+root_id+:: This is the apparent root of the site.
+root_id+:: Site seed node id. This is the only node in the site without a parent.
+home_id+:: This is the apparent root of the site (home page).
+anon_id+:: Anonymous user id. This user is the 'public' user of the site. Even if +authorize+ is set to true, this user is needed to configure the defaults for all newly created users.
+public_group_id+:: Id of the 'public' group. Every user of the site (with 'anonymous user') belongs to this group.
+site_group_id+:: Id of the 'site' group. Every user except anonymous are part of this group. This group can be seen as the 'logged in users' group.
Expand Down Expand Up @@ -57,12 +57,12 @@ class Site < ActiveRecord::Base

include RubyLess
safe_method :host => String, :lang_list => [String], :default_lang => String, :master_host => String
safe_method :root => Proc.new {|h, r, s| {:method => 'root_node', :class => VirtualClass['Project'], :nil => true}}
safe_method :orphan => Proc.new {|h, r, s| {:method => 'orphan_node', :class => VirtualClass['Project'], :nil => true}}
safe_method :root => Proc.new {|h, r, s| {:method => 'root_node', :class => current_site.root_node.vclass, :nil => true}}
safe_method :home => Proc.new {|h, r, s| {:method => 'home_node', :class => current_site.home_node.vclass, :nil => true}}

validate :valid_site
validates_uniqueness_of :host
attr_accessible :name, :languages, :default_lang, :authentication, :http_auth, :ssl_on_auth, :auto_publish, :redit_time, :api_group_id, :root_zip
attr_accessible :name, :languages, :default_lang, :authentication, :http_auth, :ssl_on_auth, :auto_publish, :redit_time, :api_group_id, :home_zip
has_many :groups, :order => "name"
has_many :nodes
has_many :users
Expand Down Expand Up @@ -181,8 +181,8 @@ def create_for_host(host, su_password, opts={})

raise Exception.new("Could not publish root node for site [#{host}] (site#{site[:id]})\n#{root.errors.map{|k,v| "[#{k}] #{v}"}.join("\n")}") unless (root.v_status == Zena::Status::Pub || root.publish)

site.home_id = root[:id]
site.root_id = root[:id]
site.orphan_id = root[:id]

# Make sure safe definitions on Time/Array/String are available on prop_eval validation.
Zena::Use::ZafuSafeDefinitions
Expand Down Expand Up @@ -228,14 +228,21 @@ def master_sites
def find_by_host(host)
host = $1 if host =~ /^(.*)\.$/
if site = self.find(:first, :conditions => ['host = ?', host]) rescue nil
if id = site.master_id
# The loaded site is an alias, load master site.
master = self.find(:first, :conditions => ['id = ?', id])
master.alias = site
site = master
end
setup_master(site)
else
nil
end
end

def setup_master(site)
if id = site.master_id
# The loaded site is an alias, load master site.
master = self.find(:first, :conditions => ['id = ?', id])
master.alias = site
master
else
site
end
site
end

# List of attributes that can be configured in the admin form
Expand Down Expand Up @@ -293,9 +300,9 @@ def root_node
@root ||= secure(Node) { Node.find(root_id) } || Node.new(:title => host)
end

# Return the orphan node.
def orphan_node
@orphan ||= secure(Node) { Node.find(orphan_id) } || Node.new(:title => host)
# Return the home node.
def home_node
@home ||= secure(Node) { Node.find(home_id) } || Node.new(:title => host)
end

# Return the public group: the one in which every visitor belongs.
Expand Down Expand Up @@ -383,19 +390,19 @@ def authentication?
@alias && @alias[:authentication] || self[:authentication]
end

def root_id
@root_id ||= @alias && @alias[:root_id] || self[:root_id]
def home_id
@home_id ||= @alias && @alias[:home_id] || self[:home_id] || self[:root_id]
end

def root_zip
root_node.zip
def home_zip
home_node.zip
end

def root_zip=(zip)
def home_zip=(zip)
if id = secure(Node) { Node.translate_pseudo_id(zip) }
self[:root_id] = id
self[:home_id] = id
else
@root_zip_error = _('could not be found')
@home_zip_error = _('could not be found')
end
end

Expand All @@ -404,8 +411,8 @@ def create_alias(hostname)
ali = Site.new(self.attributes)
ali.host = hostname
ali.master_id = self.id
ali.orphan_id = self.orphan_id
ali.root_id = self.root_id
ali.home_id = self.home_id
ali.prop = self.prop
ali.save
ali
Expand Down Expand Up @@ -605,9 +612,9 @@ def valid_site
self[:default_lang] = nil
end

if @root_zip_error
errors.add('root_id', @root_zip_error)
@root_zip_error = nil
if @home_zip_error
errors.add('root_id', @home_zip_error)
@home_zip_error = nil
end
end

Expand Down
2 changes: 1 addition & 1 deletion app/views/sites/_form.erb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<% end %>
<table cellspacing='0' class='edit_site'>
<tr><td class='label'><%= _('host') %></td><td><%= @site[:host] %></td></tr>
<tr><td class='label'><%= _('root') %></td><td><%= text_field('site', :root_zip, :size=>15, :value => @site.root_zip) %></td></tr>
<tr><td class='label'><%= _('home') %></td><td><%= text_field('site', :home_zip, :size=>15, :value => @site.home_zip) %></td></tr>
<% Site.attributes_for_form(@site.is_alias?)[:text].each do |name| -%>
<tr><td class='label'><%= _(name) %></td><td><%= text_field('site', name, :size=>nil) %></td></tr>
<% end -%>
Expand Down
4 changes: 2 additions & 2 deletions app/views/sites/_li.erb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
:method => :get) %></td>
<td class="name"><%= li.host %></td>
<% if !li.master_id %>
<td class='root'><%= li.root_node.title.limit(10) %></td>
<td class='home'><%= li.home_node.title.limit(10) %></td>
<% [:languages, :default_lang].each do |sym| -%>
<td class="<%= sym %>"><%= li[sym] %></td>
<% end -%>
Expand All @@ -15,6 +15,6 @@
<td class='redit_time' ><%= li.redit_time %></td>
<td class='options'><%= Site.attributes_for_form(li.is_alias?)[:bool].reject{|sym| !li.send(sym)}.join(', ') %></td>
<% else %>
<td colspan='8' class='root'><%= li.root_node.title %></td>
<td colspan='8' class='root'><%= li.home_node.title %></td>
<% end -%>
</tr>
4 changes: 2 additions & 2 deletions app/views/zafu/default/Node.zafu
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@
</head>

<body do='void' name='body'>
<div id='top' do='root' do='link'/>
<div id='top' do='home' do='link'/>
<div id='header'>
<div id='login' do='login_link'/>
<div id='visitor'><r:visitor_link/> <r:if test='can_edit?' do='link' mode='admin' do='t'>btn_edit</r:if></div>
<div id='search' do='search_box' type='search'/>
<ol id='menu' do='root' do='pages'>
<ol id='menu' do='home' do='pages'>
<li do='each' on_if='is_ancestor?(main)' do='link'/>
</ol>
</div>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class RenameOrphanAndRootInSite < ActiveRecord::Migration
def self.up
rename_column :sites, :root_id, :home_id
rename_column :sites, :orphan_id, :root_id
end

def self.down
rename_column :sites, :root_id, :orphan_id
rename_column :sites, :home_id, :root_id
end
end
4 changes: 2 additions & 2 deletions db/init/base/skins/default/Node.zafu
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@
</head>

<body do='void' name='body'>
<div id='top' do='root' do='link'/>
<div id='top' do='home' do='link'/>
<div id='header'>
<div id='login' do='login_link'/>
<div id='visitor'><r:visitor_link/> <r:if test='can_edit?' do='link' mode='admin' do='t'>btn_edit</r:if></div>
<div id='search' do='search_box' type='search'/>
<ol id='menu' do='root' do='pages'>
<ol id='menu' do='home' do='pages'>
<li do='each' on_if='is_ancestor?(main)' do='link'/>
</ol>
</div>
Expand Down
2 changes: 1 addition & 1 deletion lib/zena/controller/test_case.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def setup
activate_authlogic
end

def login(fixture)
def login(fixture, site_name = nil)
super
if defined?(@controller)
@controller.class_eval do
Expand Down
2 changes: 1 addition & 1 deletion lib/zena/info.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module Zena
VERSION = '1.2.5'
VERSION = '1.2.6'
ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..', '..'))
end
2 changes: 1 addition & 1 deletion lib/zena/use/ancestry.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def title_join
TITLE_ML_JOIN = %Q{INNER JOIN idx_nodes_ml_strings AS id1 ON id1.node_id = nodes.id AND id1.key = 'title'}

# (slow). Find a node by it's path. This is used during node importation when stored as zml files or to resolve custom_base url until we have an "alias" table.
def find_by_path(path, parent_id = current_site.orphan_id, multilingual = false)
def find_by_path(path, parent_id = current_site.root_id, multilingual = false)
res = nil
path = path.split('/') unless path.kind_of?(Array)
last = path.size - 1
Expand Down
8 changes: 7 additions & 1 deletion lib/zena/use/query_node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -546,9 +546,15 @@ def context_relation(relation)
when 'root'
# Special pseudo-context
add_table(main_table)
set_main_class(VirtualClass['Project'])
set_main_class(current_site.root_node.vclass)
add_filter "#{table}.id = #{current_site.root_id}"
return true
when 'home'
# Special pseudo-context
add_table(main_table)
set_main_class(current_site.home_node.vclass)
add_filter "#{table}.id = #{current_site.home_id}"
return true
#when 'author', 'traductions', 'versions'
# # TODO: not implemented yet...
# return nil
Expand Down
12 changes: 9 additions & 3 deletions lib/zena/use/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,17 @@ module TestHelper
include Zena::Use::Upload::UploadedFile

# Set visitor for unit testing
def login(fixture)
def login(fixture, site_name = nil)
user = users(fixture)
setup_visitor(user, user.site)
if site_name
site = Site.setup_master(Site.find_by_name(site_name))
else
# Not an alias
site = user.site
end
setup_visitor(user, site)
user.ip = '10.0.0.44'
$_test_site = user.site.name
$_test_site = site.name
::I18n.locale = user.lang
end

Expand Down
14 changes: 13 additions & 1 deletion test/integration/query_node/relations.yml
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,16 @@ project_as_blog:
# Should scope by path
# This is the same as "project where class like Blog"
src: "Blog:project"
sql: "[%Q{SELECT nodes.* FROM nodes WHERE #{secure_scope('nodes')} AND nodes.kpath LIKE 'NPPB%' AND nodes.id = ? ORDER BY nodes.zip ASC}, @node.get_project_id]"
sql: "[%Q{SELECT nodes.* FROM nodes WHERE #{secure_scope('nodes')} AND nodes.kpath LIKE 'NPPB%' AND nodes.id = ? ORDER BY nodes.zip ASC}, @node.get_project_id]"

from_root:
context:
site: alias
src: nodes from root limit 3
res: 'people, projects list, Collections'

from_home:
context:
site: alias
src: nodes from home limit 3
res: 'bird, flower'
4 changes: 2 additions & 2 deletions test/integration/query_node_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ def yt_do_test(file, test)
params[k.to_sym] = v
end

$_test_site = params[:site] || 'zena'
login @context[:visitor].to_sym
$_test_site = @context[:site] || 'zena'
login(@context[:visitor].to_sym, $_test_site)

@context[:rubyless_helper] = self
defaults = (@context[:default] ||= {})
Expand Down

0 comments on commit 839b05f

Please sign in to comment.