Skip to content

Commit

Permalink
Split Resources and Contents code
Browse files Browse the repository at this point in the history
  • Loading branch information
atd committed Feb 10, 2009
1 parent e61f45a commit d858fcc
Show file tree
Hide file tree
Showing 20 changed files with 428 additions and 194 deletions.
2 changes: 1 addition & 1 deletion README.rdoc
Expand Up @@ -21,7 +21,7 @@ Documentation: http://cmsplugin.rubyforge.org/
You can add functionality to any of your ActiveRecord models:

=== Basic
Resource:: a model that have, at least, {CRUD}[http://en.wikipedia.org/wiki/Create,_read,_update_and_delete] operations and can be imported/exported in different formats.
ActiveRecord::Resource:: a model that have, at least, {CRUD}[http://en.wikipedia.org/wiki/Create,_read,_update_and_delete] operations and can be imported/exported in different formats.
ActiveRecord::Content:: a Resource that belongs to a Container.
ActiveRecord::Container:: a model that host Contents.
ActiveRecord::Agent:: a model with authentication, authorization and potencial to perform actions over Resources.
Expand Down
10 changes: 5 additions & 5 deletions app/controllers/categories_controller.rb
@@ -1,6 +1,6 @@
# Categories methods and filters for Category
class CategoriesController < ApplicationController
before_filter :needs_categories_domain, :only => [ :index, :new, :create ]
before_filter :categories_domain!, :only => [ :index, :new, :create ]

before_filter :get_category, :only => [ :show, :edit, :update, :destroy ]

Expand Down Expand Up @@ -103,9 +103,9 @@ def destroy

protected

# Sets @categories_domain, getting a CategoriesDomain from path (using get_resource_from_path) or current_site
def needs_categories_domain
@categories_domain = get_resource_from_path(:acts_as => :categories_domain) || current_site
# Sets @categories_domain, getting a CategoriesDomain from path (using record_from_path) or site
def categories_domain!
@categories_domain = record_from_path(:acts_as => :categories_domain) || site
end

# Find Category using params[:id]
Expand All @@ -118,7 +118,7 @@ def get_category

# Set @container and @agents variables from @categories_domain
#
# @categories_domain variable is set either in :needs_categories_domain or :get_category
# @categories_domain variable is set either in :categories_domain! or :get_category
def categories_domains_and_agents #:nodoc:
#TODO return unless @categories_domain && @categories_domain.acts_as_container?
@containers = Array(@categories_domain)
Expand Down
8 changes: 4 additions & 4 deletions app/controllers/invitations_controller.rb
Expand Up @@ -2,8 +2,8 @@ class InvitationsController < ApplicationController
# GET /invitations
# GET /invitations.xml
def index
@invitations = get_stage ?
get_stage.stage_invitations.column_sort(params[:order], params[:direction]) :
@invitations = stage ?
stage.stage_invitations.column_sort(params[:order], params[:direction]) :
Invitation.column_sort(params[:order], params[:direction])

respond_to do |format|
Expand Down Expand Up @@ -102,7 +102,7 @@ def destroy

private

def get_stage
@stage ||= get_resource_from_path(:acts_as => :stage)
def stage
@stage ||= record_from_path(:acts_as => :stage)
end
end
12 changes: 6 additions & 6 deletions app/controllers/performances_controller.rb
@@ -1,6 +1,6 @@
class PerformancesController < ApplicationController
before_filter :get_stage, :only => [ :index, :new, :create ]
before_filter :get_performance, :only => [ :edit, :update, :destroy ]
before_filter :stage, :only => [ :index, :new, :create ]
before_filter :performance, :only => [ :edit, :update, :destroy ]
before_filter :parse_polymorphic_agent, :only => [ :create, :update ]

def index
Expand All @@ -9,7 +9,7 @@ def index

def create
@performance = Performance.new(params[:performance])
@performance.stage = get_stage
@performance.stage = stage

if @performance.save
respond_to do |format|
Expand Down Expand Up @@ -62,11 +62,11 @@ def destroy

private

def get_stage
@stage ||= get_resource_from_path(:acts_as => :stage)
def stage
@stage ||= record_from_path(:acts_as => :stage)
end

def get_performance
def performance
@performance = Performance.find(params[:id])
@stage = @performance.stage
end
Expand Down
1 change: 1 addition & 0 deletions app/helpers/application_helper.rb
@@ -1,5 +1,6 @@
# Methods added to this helper will be available to all templates in the application.
module ApplicationHelper
include ResourcesHelper
include ContentsHelper
include MenuBoxHelper
include SortableHelper
Expand Down
2 changes: 1 addition & 1 deletion app/helpers/contents_helper.rb
Expand Up @@ -67,7 +67,7 @@ def contents_list(options = {})
end

# Show info about the Content
def content_info(content)
def content_info(content = nil)
content ||= @content

returning "" do |html|
Expand Down
11 changes: 11 additions & 0 deletions app/helpers/resources_helper.rb
@@ -0,0 +1,11 @@
module ResourcesHelper
def resource_info(resource = nil)
resource ||= @resource

returning "" do |html|
html << "<div id=\"content_info_top\" class=\"block_white_top\"#{ t('detail.other') } </div>"
html << "<div id=\"content_info_center\" class=\"block_white_center\">#{ render(:partial => "resources/info", :locals => { :resource => resource } ) if resource }</div>"
html << "<div id=\"content_info_bottom\" class=\"block_white_bottom\"></div>"
end
end
end
12 changes: 12 additions & 0 deletions app/views/resources/_info.html.erb
@@ -0,0 +1,12 @@
<b class="details_info"><%= t(:title) %></b>: <%= resource.title %><br />
<% if resource.respond_to?(:author) && resource.author -%>
<b class="details_info"><%= t(:author) %></b>: <%= link_to resource.author.name, resource.author %><br />
<% end -%>
<b class="details_info"><%= t(:created_at_date, :date => localize(resource.created_at)) %><br />
<b class="details_info"><%= t(:updated_at_date, :date => localize(resource.updated_at)) %><br />
<% if resource.respond_to?(:categories) && resource.categories.any? -%>
<b class="details_info"><%= t('category.other')%></b>: <%= resource.categories.map{ |c| link_to(sanitize(c.name), c) }.join(", ") %><br />
<% end -%>
<%= link_to(t(:show, :scope => resource.class.to_s.underscore), polymorphic_path(resource), {:class => "view action"}) %>
<%= link_to(t(:edit, :scope => resource.class.to_s.underscore), edit_polymorphic_path(resource),{:class => "edit action"}) %>
<%= link_to(t(:delete, :scope => resource.class.to_s.underscore), polymorphic_path(resource), {:class => "delete action", :confirm => t(:confirm_delete, :scope => resource.class.to_s.underscore), :method => :delete}) %>
30 changes: 15 additions & 15 deletions lib/action_controller/agents.rb
Expand Up @@ -3,7 +3,7 @@ module ActionController #:nodoc:
module Agents
class << self
def included(base) #:nodoc:
base.send :include, ActionController::Base unless base.ancestors.include?(ActionController::Base)
base.send :include, ActionController::Move unless base.ancestors.include?(ActionController::Move)
base.send :include, ActionController::Authentication unless base.ancestors.include?(ActionController::Authentication)
end
end
Expand All @@ -29,16 +29,16 @@ def show

# Render a form for creating a new Agent
def new
@agent = self.resource_class.new
instance_variable_set "@#{ self.resource_class.to_s.underscore }", @agent
@agent = model_class.new
instance_variable_set "@#{ model_class.to_s.underscore }", @agent
@title = authenticated? ?
t(:new, :scope => self.resource_class.to_s.underscore) :
t(:new, :scope => model_class.to_s.underscore) :
t(:join_to_site, :site => Site.current.name)
end

# Create new Agent instance
def create
@agent = self.resource_class.new(params[:agent])
@agent = model_class.new(params[:agent])

unless authenticated?
cookies.delete :auth_token unless authenticated?
Expand All @@ -48,15 +48,15 @@ def create
@agent.save!

if authenticated?
redirect_to polymorphic_path(self.resource_class.new)
redirect_to polymorphic_path(model_class.new)
flash[:info] = t(:created, :scope => @agent.class.to_s.underscore)
else
self.current_agent = @agent
redirect_to @agent
flash[:info] = t(:account_created)
end

if self.resource_class.agent_options[:activation]
if model_class.agent_options[:activation]
flash[:info] << '<br />'
flash[:info] << ( @agent.active? ?
t(:activation_email_sent, :scope => @agent.class.to_s.underscore) :
Expand All @@ -69,12 +69,12 @@ def create
def destroy
@agent.destroy
flash[:info] = t(:deleted, :scope => @agent.class.to_s.underscore)
redirect_to polymorphic_path(self.resource_class.new)
redirect_to polymorphic_path(model_class.new)
end

# Activate Agent from email
def activate
self.current_agent = params[:activation_code].blank? ? Anonymous.current : self.resource_class.find_by_activation_code(params[:activation_code])
self.current_agent = params[:activation_code].blank? ? Anonymous.current : model_class.find_by_activation_code(params[:activation_code])
if authenticated? && current_agent.respond_to?("active?") && !current_agent.active?
current_agent.activate
flash[:info] = t(:account_activated)
Expand All @@ -86,7 +86,7 @@ def activate

def forgot_password
if params[:email]
@agent = self.resource_class.find_by_email(params[:email])
@agent = model_class.find_by_email(params[:email])
unless @agent
flash[:error] = t(:could_not_find_anybody_with_that_email_address)
return
Expand All @@ -100,7 +100,7 @@ def forgot_password

# Resets Agent password via email
def reset_password
@agent = self.resource_class.find_by_reset_password_code(params[:reset_password_code])
@agent = model_class.find_by_reset_password_code(params[:reset_password_code])
raise unless @agent
return if params[:password].blank?

Expand All @@ -125,18 +125,18 @@ def reset_password
#
# Example GET /users/1 or GET /users/quentin
def get_agent
@agent = ( params[:id].match(/^\d+$/) ? self.resource_class.find(params[:id]) : self.resource_class.find_by_login(params[:id]) )
instance_variable_set "@#{ self.resource_class.to_s.underscore }", @agent
@agent = ( params[:id].match(/^\d+$/) ? model_class.find(params[:id]) : model_class.find_by_login(params[:id]) )
instance_variable_set "@#{ model_class.to_s.underscore }", @agent
end

# Filter for activation methods
def activation_required
redirect_back_or_default('/') unless self.resource_class.agent_options[:activation]
redirect_back_or_default('/') unless model_class.agent_options[:activation]
end

# Filter for methods that require login_and_password authentication, like reset_password
def login_and_pass_auth_required
redirect_back_or_default('/') unless self.resource_class.agent_options[:authentication].include?(:login_and_password)
redirect_back_or_default('/') unless model_class.agent_options[:authentication].include?(:login_and_password)
end

private
Expand Down
38 changes: 19 additions & 19 deletions lib/action_controller/contents.rb
Expand Up @@ -2,7 +2,7 @@ module ActionController #:nodoc:
# Controller methods and default filters for Agents Controllers
module Contents
def self.included(base) #:nodoc:
base.send :include, ActionController::Move unless base.ancestors.include?(ActionController::Move)
base.send :include, ActionController::Move unless base.ancestors.include?(ActionController::MoveResources)
base.send :include, ActionController::Authorization unless base.ancestors.include?(ActionController::Authorization)
end

Expand All @@ -17,10 +17,10 @@ def self.included(base) #:nodoc:
# GET /:container_type/:container_id/contents
def index(&block)
if current_container
@title ||= t('other_in_container', :container => current_container.name, :scope => self.resource_class.to_s.underscore)
@title ||= t('other_in_container', :container => current_container.name, :scope => model_class.to_s.underscore)
@agents = current_container.actors
else
@title ||= t(self.resource_class.to_s.underscore, :count => :other)
@title ||= t(model_class.to_s.underscore, :count => :other)
@agents = ActiveRecord::Agent.authentication_classes.map(&:all).flatten.sort{ |a, b| a.name <=> b.name }
end

Expand All @@ -29,8 +29,8 @@ def index(&block)
params[:order], params[:direction] = "entries.updated_at", "DESC"
end

@contents = self.resource_class.in_container(current_container).column_sort(params[:order], params[:direction]).paginate(:page => params[:page])
instance_variable_set "@#{ self.resource_class.to_s.tableize }", @contents
@contents = model_class.in_container(current_container).column_sort(params[:order], params[:direction]).paginate(:page => params[:page])
instance_variable_set "@#{ model_class.to_s.tableize }", @contents

@updated = @contents.any? ? @contents.first.entry.updated_at : Time.now.utc

Expand Down Expand Up @@ -61,7 +61,7 @@ def show
@content.entry = @content.parent.entry
end

instance_variable_set "@#{ self.resource_class.to_s.underscore }", @content
instance_variable_set "@#{ model_class.to_s.underscore }", @content

@title ||= @content.entry.title if @content.entry

Expand Down Expand Up @@ -95,18 +95,18 @@ def show
# GET /:container_type/:container_id/contents/new
# GET /contents/new
def new
@content = self.resource_class.new
@content = model_class.new
@content.entry = Entry.new(:content => @content)
instance_variable_set("@#{ self.resource_class.to_s.underscore }", @content)
@title ||= t(:new, :scope => self.resource_class.to_s.underscore)
instance_variable_set("@#{ model_class.to_s.underscore }", @content)
@title ||= t(:new, :scope => model_class.to_s.underscore)
end

# Render form for updating Content
#
# GET /contents/:id/edit
# GET /:container_type/:container_id/contents/:id/edit
def edit
@title ||= t(:editing, :scope => self.resource_class.to_s.underscore)
@title ||= t(:editing, :scope => model_class.to_s.underscore)
end

# Create new Content
Expand All @@ -121,7 +121,7 @@ def create
# every time a Content is posted.
# Idea: Should use SHA1 on one or some relevant Content field(s)
# and find_or_create_by_sha1
@content = instance_variable_set "@#{controller_name.singularize}", self.resource_class.new(params[self.resource_class.to_s.underscore.to_sym])
@content = instance_variable_set "@#{controller_name.singularize}", model_class.new(params[model_class.to_s.underscore.to_sym])

params[:entry] ||= {}
params[:entry][:agent] = current_agent
Expand All @@ -135,7 +135,7 @@ def create
flash[:valid] = t(:created, :scope => @content.class.to_s.underscore)
redirect_to [ current_container, @content ]
else
@title ||= t(:new, self.resource_class.to_s.underscore)
@title ||= t(:new, :scope => model_class.to_s.underscore)
render :action => 'new'
end
}
Expand Down Expand Up @@ -181,19 +181,19 @@ def update
xml_formats << @content.format unless @content.format == :html

format.any(*xml_formats) {
if @content.update_attributes(params[self.resource_class.to_s.underscore.to_sym])
if @content.update_attributes(params[model_class.to_s.underscore.to_sym])
head :ok
else
render :xml => @content.errors.to_xml, :status => :not_acceptable
end
}

format.html {
if @content.update_attributes(params[self.resource_class.to_s.underscore.to_sym])
if @content.update_attributes(params[model_class.to_s.underscore.to_sym])
flash[:valid] = t(:updated, :scope => @content.class.to_s.underscore)
redirect_to [ current_container, @content ].compact
else
@title ||= t(:editing, :scope => self.resource_class.to_s.underscore)
@title ||= t(:editing, :scope => model_class.to_s.underscore)
render :action => 'edit'
end
}
Expand All @@ -209,7 +209,7 @@ def destroy
@content.destroy

respond_to do |format|
format.html { redirect_to polymorphic_path([ current_container, self.resource_class.new ].compact) }
format.html { redirect_to polymorphic_path([ current_container, model_class.new ].compact) }
format.atom { head :ok }
# FIXME: Check AtomPub, RFC 5023
# format.send(mime_type) { head :ok }
Expand All @@ -223,7 +223,7 @@ def destroy
# Render Bad Request unless the controller name relates to a class that acts_as_content.
# If current_container exists, check its valid contents
def controller_name_is_valid_content
unless (current_container && current_container.container_options[:contents] || ActiveRecord::Content.symbols).include?(self.resource_class.collection)
unless (current_container && current_container.container_options[:contents] || ActiveRecord::Content.symbols).include?(model_class.collection)
render :text => "Doesn't support this Content type", :status => 400
end
end
Expand All @@ -243,8 +243,8 @@ def controller_name_is_valid_content
# This funcion uses ActiveRecord::Content#in_container named scope
#
def get_content
@content = self.resource_class.in_container(current_container).find params[:id]
instance_variable_set "@#{ self.resource_class.to_s.underscore }", @content
@content = model_class.in_container(current_container).find params[:id]
instance_variable_set "@#{ model_class.to_s.underscore }", @content
end
end
end
4 changes: 2 additions & 2 deletions lib/action_controller/logotypes.rb
Expand Up @@ -10,7 +10,7 @@ def show
@logotype = (
@logotypable ?
@logotypable.logotype :
self.resource_class.find(params[:id])
model_class.find(params[:id])
)

@logotype =
Expand All @@ -25,7 +25,7 @@ def show
private

def get_logotypable_from_path #:nodoc:
get_resource_from_path(:acts_as => :logotypable)
record_from_path(:acts_as => :logotypable)
end
end
end

0 comments on commit d858fcc

Please sign in to comment.