Improve code in items, pages, links

This commit is contained in:
Christophe Vilayphiou 2012-02-17 14:54:11 +08:00
parent cf4a6c0b35
commit 0b71714d44
7 changed files with 37 additions and 63 deletions

View File

@ -16,21 +16,6 @@ class Admin::ItemsController < ApplicationController
end end
end end
#TODO
# Allow to move items down and up different parents
# def up
# @item = Item.find(params[:id])
# @item.move_higher
# redirect_to admin_items_url( :parent_name => @item.parent_name )
# end
#
# def down
# @item = Item.find(params[:id])
# @item.move_lower
# redirect_to admin_items_url( :parent_name => @item.parent_name )
# end
protected protected
def find_snippets def find_snippets

View File

@ -12,13 +12,11 @@ class Admin::LinksController < ApplicationController
def new def new
@item = Link.new @item = Link.new
@item.is_published = true @item.parent = Page.find(params[:parent_id]) rescue nil
@item.parent_id = Page.find(params[:parent_id]).id rescue nil
end end
def edit def edit
@item = Link.find(params[:id]) @item = Link.find(params[:id])
@i18n_variable = @item.i18n_variable
end end
def create def create
@ -34,7 +32,6 @@ class Admin::LinksController < ApplicationController
end end
else else
flash.now[:error] = t('admin.create_error_link') flash.now[:error] = t('admin.create_error_link')
@i18n_variable = @item.i18n_variable
render :action => "new" render :action => "new"
end end
end end
@ -52,7 +49,6 @@ class Admin::LinksController < ApplicationController
end end
else else
flash.now[:error] = t('admin.update_error_link') flash.now[:error] = t('admin.update_error_link')
@i18n_variable = @item.i18n_variable
render :action => "edit" render :action => "edit"
end end
end end

View File

@ -19,19 +19,17 @@ class Admin::PagesController < ApplicationController
def new def new
@item = Page.new @item = Page.new
@item.parent = Item.find(params[:parent_id]) rescue nil
@apps = ModuleApp.all @apps = ModuleApp.all
@item.is_published = true
@item.parent_id = @parent_item.id rescue nil
@designs = Design.all.entries @designs = Design.all.entries
@default_design = Design.first @design = Design.first
end end
def edit def edit
@item = Page.find(params[:id]) @item = Page.find(params[:id])
@apps = ModuleApp.all @apps = ModuleApp.all
@i18n_variable = @item.i18n_variable
@designs = Design.all.entries @designs = Design.all.entries
@design = @item.design @design = @item.design ? @item.design : @designs.first
@app_frontend_urls = @item.module_app.app_pages if @item.module_app @app_frontend_urls = @item.module_app.app_pages if @item.module_app
end end
@ -47,7 +45,9 @@ class Admin::PagesController < ApplicationController
end end
else else
flash.now[:error] = t('admin.create_error_page') flash.now[:error] = t('admin.create_error_page')
@i18n_variable = @item.i18n_variable @apps = ModuleApp.all
@designs = Design.all.entries
@design = Design.first
render :action => "new" render :action => "new"
end end
end end

View File

@ -13,13 +13,16 @@ class PagesController < ApplicationController
def show def show
#begin #begin
item = Item.first(:conditions => {:full_name => params[:page_name]}) @item = Item.first(:conditions => {:full_name => params[:page_name]})
case item._type if @item.is_published
when 'Page' case @item._type
@item = item when 'Page'
render_page(params[:id]) render_page(params[:id])
when 'Link' when 'Link'
redirect_to "http://#{item[:url]}" redirect_to "http://#{@item[:url]}"
end
else
render :file => "#{Rails.root}/public/404.html", :status => :not_found
end end
#rescue #rescue
# render :file => "#{Rails.root}/public/404.html", :status => :not_found # render :file => "#{Rails.root}/public/404.html", :status => :not_found

View File

@ -5,15 +5,13 @@ class Item
field :name, :index => true field :name, :index => true
field :full_name, :index => true field :full_name, :index => true
field :parent_id, :index => true
field :parent_name
field :position, :type => Integer field :position, :type => Integer
field :is_published, :type => Boolean, :default => true, :index => true field :is_published, :type => Boolean, :default => false, :index => true
validates_format_of :name, :with => /^[0-9a-zA-Z\-_]+$/ validates_format_of :name, :with => /^[0-9a-zA-Z\-_]+$/
validates :name, :exclusion => { :in => LIST[:forbidden_item_names] } validates :name, :exclusion => { :in => LIST[:forbidden_item_names] }
validates_uniqueness_of :name, :scope => :parent_id validates_uniqueness_of :name, :scope => :parent_id
validates_presence_of :name, :full_name, :position, :is_published validates_presence_of :name, :full_name, :position
belongs_to :parent, :class_name => "Item" belongs_to :parent, :class_name => "Item"
has_one :i18n_variable, :as => :language_value, :autosave => true, :dependent => :destroy has_one :i18n_variable, :as => :language_value, :autosave => true, :dependent => :destroy
@ -47,9 +45,6 @@ class Item
self.position = (max_page)? max_page + 1 : 1 self.position = (max_page)? max_page + 1 : 1
end end
# Set the parent value
self.parent_name = Item.first(:conditions => {:id => self.parent_id} ).name rescue nil
# Build the full_name from the ancestors array # Build the full_name from the ancestors array
full_node = self.ancestors.map{ |a| a.name }.push( self.name ) full_node = self.ancestors.map{ |a| a.name }.push( self.name )
# Remove root node if not root # Remove root node if not root

View File

@ -1,16 +1,18 @@
<%= f.error_messages %> <%= f.error_messages %>
<%= f.hidden_field :parent_id %> <%= f.hidden_field :parent, :value => @item.parent.id %>
<p> <p>
<%= f.label :name, "Name" %> <%= f.label :name, "Name" %>
<%= f.text_field :name, :class => 'text' %> <%= f.text_field :name, :class => 'text' %>
</p> </p>
<% @site_valid_locales.each do |locale| %> <%= f.fields_for :i18n_variable, (@item.new_record? ? @item.build_i18n_variable : @item.i18n_variable) do |f| %>
<p> <% @site_valid_locales.each do |locale| %>
<%= label_tag "link[title]", "#{t('admin.title')} #{locale}" %> <p>
<%= text_field_tag "link[i18n_variable][#{locale}]", (@i18n_variable[locale] if @i18n_variable), :class => 'text' %> <%= f.label :locale, "#{t('admin.title')} #{I18nVariable.from_locale(locale)}" %>
</p> <%= f.text_field locale %>
</p>
<% end %>
<% end %> <% end %>
<p> <p>

View File

@ -1,34 +1,27 @@
<%= f.error_messages %> <%= f.error_messages %>
<%= f.hidden_field :parent_id %> <%= f.hidden_field :parent, :value => @item.parent.id %>
<p> <p>
<%= f.label :name, t('admin.name') %> <%= f.label :name, t('admin.name') %>
<%= f.text_field :name, :class => 'text' %> <%= f.text_field :name, :class => 'text' %>
</p> </p>
<%= f.fields_for :i18n_variable, (@item.new_record? ? @item.build_i18n_variable : @item.i18n_variable) do |f| %>
<% @site_valid_locales.each do |locale| %> <% @site_valid_locales.each do |locale| %>
<p> <p>
<%= label_tag "page[title]", "#{t('admin.title')} #{locale}" %> <%= f.label :locale, "#{t('admin.title')} #{I18nVariable.from_locale(locale)}" %>
<%= text_field_tag "page[i18n_variable][#{locale}]", (@i18n_variable[locale] if @i18n_variable), :class => 'text' %> <%= f.text_field locale %>
</p> </p>
<% end %>
<% end %> <% end %>
<p> <p>
<%= t('admin.design_name') %> <%= t('admin.design_name') %>
<% if @design %> <%= f.collection_select :design, @designs, :id, :title, {:selected => @design.id}, {:rel => admin_pages_path} %>
<%= f.collection_select :design_id, @designs, :id, :title, {}, :rel => admin_pages_path %>
<% else %>
<%= f.select :design_id, @designs.collect { |d| [d.title, d.id] }, {:selected => @default_design.id}, {:rel => admin_pages_path} %>
<% end %>
</p> </p>
<p> <p>
<%= t('admin.theme') %> <%= t('admin.theme') %>
<% if @design %> <%= f.select :theme_id, @design.themes.collect { |t| [t.name.capitalize, t.id] }, :include_blank => true %>
<%= f.select :theme_id, @design.themes.collect { |t| [t.name.capitalize, t.id] }, :include_blank => true %>
<% else %>
<%= f.select :theme_id, @default_design.themes.collect { |t| [t.name.capitalize, t.id] }, :include_blank => true %>
<% end %>
</p> </p>
<p> <p>
<%= t('admin.module_app') %> <%= t('admin.module_app') %>