2010-06-10 13:30:32 +00:00
|
|
|
module Admin
|
|
|
|
class ApiContentsController < ActionController::Base
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-06-10 13:30:32 +00:00
|
|
|
include Locomotive::Routing::SiteDispatcher
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-06-10 13:30:32 +00:00
|
|
|
before_filter :require_site
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-06-10 13:30:32 +00:00
|
|
|
before_filter :set_content_type
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2011-08-18 14:06:34 +00:00
|
|
|
before_filter :block_content_type_with_disabled_api
|
|
|
|
|
|
|
|
before_filter :sanitize_content_params, :only => :create
|
|
|
|
|
2010-06-10 13:30:32 +00:00
|
|
|
def create
|
|
|
|
@content = @content_type.contents.build(params[:content])
|
|
|
|
|
|
|
|
respond_to do |format|
|
|
|
|
if @content.save
|
|
|
|
format.json { render :json => { :content => @content } }
|
2010-10-28 23:36:45 +00:00
|
|
|
format.html do
|
|
|
|
flash[@content_type.slug.singularize] = @content.aliased_attributes
|
|
|
|
redirect_to params[:success_callback]
|
|
|
|
end
|
2010-06-10 13:30:32 +00:00
|
|
|
else
|
|
|
|
format.json { render :json => { :content => @content, :errors => @content.errors } }
|
2010-10-28 23:36:45 +00:00
|
|
|
format.html do
|
|
|
|
flash[@content_type.slug.singularize] = @content.aliased_attributes
|
2010-12-16 23:42:38 +00:00
|
|
|
flash['errors'] = @content.errors_to_hash
|
2010-10-28 23:36:45 +00:00
|
|
|
redirect_to params[:error_callback]
|
|
|
|
end
|
2010-06-10 13:30:32 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-06-10 13:30:32 +00:00
|
|
|
protected
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-06-10 13:30:32 +00:00
|
|
|
def set_content_type
|
2010-07-23 20:09:54 +00:00
|
|
|
@content_type = current_site.content_types.where(:slug => params[:slug]).first
|
2011-08-18 14:06:34 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def block_content_type_with_disabled_api
|
|
|
|
unless @content_type.api_enabled?
|
|
|
|
respond_to do |format|
|
|
|
|
format.json { render :json => { :error => 'Api not enabled' }, :status => :forbidden }
|
|
|
|
format.html { render :text => 'Api not enabled', :status => :forbidden }
|
|
|
|
end
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def sanitize_content_params
|
|
|
|
(params[:content] || {}).each do |key, value|
|
|
|
|
params[:content][key] = Sanitize.clean(value, Sanitize::Config::BASIC)
|
|
|
|
end
|
2010-06-10 13:30:32 +00:00
|
|
|
end
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-06-10 13:30:32 +00:00
|
|
|
end
|
2010-07-23 20:09:54 +00:00
|
|
|
end
|