Skip to content

Commit

Permalink
Controller helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
galetahub committed Jul 8, 2011
1 parent 089d874 commit 6cfe947
Show file tree
Hide file tree
Showing 21 changed files with 212 additions and 6 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
log/*.log
pkg/
test/dummy/db/*.sqlite3
test/dummy/db/schema.rb
test/dummy/log/*.log
test/dummy/public/ckeditor_assets/
test/dummy/tmp/
test/tmp
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ platforms :ruby do
group :development, :test do
gem "capybara", ">= 0.4.0"
gem "redgreen", "~> 1.2.2"
gem "paperclip", "~> 2.3.12"

# To use debugger (ruby-debug for Ruby 1.8.7+, ruby-debug19 for Ruby 1.9.2+)
# gem 'ruby-debug'
Expand Down
6 changes: 6 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ GEM
cgi_multipart_eof_fix (2.5.0)
childprocess (0.1.9)
ffi (~> 1.0.6)
cocaine (0.1.0)
daemons (1.1.4)
erubis (2.6.6)
abstract (>= 1.0.0)
Expand All @@ -69,6 +70,10 @@ GEM
mysql2 (0.2.11)
nokogiri (1.4.6)
orm_adapter (0.0.5)
paperclip (2.3.12)
activerecord (>= 2.3.0)
activesupport (>= 2.3.2)
cocaine (>= 0.0.2)
polyglot (0.3.1)
rack (1.2.3)
rack-mount (0.6.14)
Expand Down Expand Up @@ -113,5 +118,6 @@ DEPENDENCIES
ckeditor!
mongrel
mysql2 (= 0.2.11)
paperclip (~> 2.3.12)
rails (= 3.0.9)
redgreen (~> 1.2.2)
26 changes: 26 additions & 0 deletions README.rdoc
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,32 @@ Jquery sample:

<%= form.ckeditor :content, :label => false, :input_html => { :toolbar => 'Full' } %>

=== Default scope

For example, you need split assets collection for each user.

class ApplicationController < ActionController::Base

protected

def ckeditor_filebrowser_scope(options = {})
super { :assetable_id => current_user.id, :assetable_type => 'User' }.merge(options)
end
end

=== Before create asset callback

class ApplicationController < ActionController::Base

protected

def ckeditor_before_create_asset(asset)
asset.assetable = current_user if respond_to?(:current_user)
return can?(:create, asset)
end
end


== I18n

en:
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/ckeditor/attachment_files_controller.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
class Ckeditor::AttachmentFilesController < Ckeditor::BaseController

def index
@attachments = Ckeditor.attachment_file_model.find_all(:order => [:id, :desc])
@attachments = Ckeditor.attachment_file_model.find_all(ckeditor_attachment_files_scope)
respond_with(@attachments)
end

Expand Down
4 changes: 3 additions & 1 deletion app/controllers/ckeditor/base_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ def respond_with_asset(asset)
file = params[:CKEditor].blank? ? params[:qqfile] : params[:upload]
asset.data = Ckeditor::Http.normalize_param(file, request)

if asset.save
callback = ckeditor_before_create_asset(asset)

if callback && asset.save
body = params[:CKEditor].blank? ? asset.to_json(:only=>[:id, :type]) : %Q"<script type='text/javascript'>
window.parent.CKEDITOR.tools.callFunction(#{params[:CKEditorFuncNum]}, '#{Ckeditor::Utils.escape_single_quotes(asset.url_content)}');
</script>"
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/ckeditor/pictures_controller.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
class Ckeditor::PicturesController < Ckeditor::BaseController

def index
@pictures = Ckeditor.picture_model.find_all(:order => [:id, :desc])
@pictures = Ckeditor.picture_model.find_all(ckeditor_pictures_scope)
respond_with(@pictures)
end

Expand Down
1 change: 1 addition & 0 deletions lib/ckeditor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ module Helpers
autoload :ViewHelper, 'ckeditor/helpers/view_helper'
autoload :FormHelper, 'ckeditor/helpers/form_helper'
autoload :FormBuilder, 'ckeditor/helpers/form_builder'
autoload :Controllers, 'ckeditor/helpers/controllers'
end

module Hooks
Expand Down
6 changes: 5 additions & 1 deletion lib/ckeditor/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ module Ckeditor
class Engine < ::Rails::Engine
config.action_view.javascript_expansions[:ckeditor] = "ckeditor/ckeditor"

initializer "ckeditor.view_helpers" do
initializer "ckeditor.helpers" do
ActiveSupport.on_load(:action_controller) do
ActionController::Base.send :include, Ckeditor::Helpers::Controllers
end

ActiveSupport.on_load :action_view do
ActionView::Base.send :include, Ckeditor::Helpers::ViewHelper
ActionView::Base.send :include, Ckeditor::Helpers::FormHelper
Expand Down
30 changes: 30 additions & 0 deletions lib/ckeditor/helpers/controllers.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
module Ckeditor
module Helpers
module Controllers
extend ActiveSupport::Concern

protected

def ckeditor_before_create_asset(asset)
asset.assetable = current_user if respond_to?(:current_user)
return true
end

def ckeditor_authenticate(asset)
# TODO:
end

def ckeditor_pictures_scope(options = {})
ckeditor_filebrowser_scope(options)
end

def ckeditor_attachment_files_scope(options = {})
ckeditor_filebrowser_scope(options)
end

def ckeditor_filebrowser_scope(options = {})
{ :order => [:id, :desc] }.merge(options)
end
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ class Ckeditor::AttachmentFile < Ckeditor::Asset
:path => ":rails_root/public/ckeditor_assets/attachments/:id/:filename"

validates_attachment_size :data, :less_than => 100.megabytes
validates_attachment_presence :data

def url_thumb
@url_thumb ||= begin
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ class Ckeditor::Picture < Ckeditor::Asset
:styles => { :content => '800>', :thumb => '118x100#' }

validates_attachment_size :data, :less_than => 2.megabytes
validates_attachment_presence :data

def url_content
url(:content)
Expand Down
38 changes: 38 additions & 0 deletions test/controllers/pictures_controller_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
require 'test_helper'

class PicturesControllerTest < ActionController::TestCase
tests Ckeditor::PicturesController

def setup
@image = fixture_file_upload('files/rails.png', 'image/png')
end

test "index action" do
get :index

assert_equal 200, @response.status
assert_template "ckeditor/pictures/index"
end

test "create action via filebrowser" do
assert_difference 'Ckeditor::Picture.count' do
post :create, :qqfile => @image
end

assert_equal 200, @response.status
end

test "create action via CKEditor upload form" do
assert_difference 'Ckeditor::Picture.count' do
post :create, :upload => @image, :CKEditor => 'ckeditor_field'
end

assert_equal 200, @response.status
end

test "invalid params for create action" do
assert_no_difference 'Ckeditor::Picture.count' do
post :create, :qqfile => nil
end
end
end
7 changes: 7 additions & 0 deletions test/dummy/app/models/ckeditor/asset.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require 'mime/types'

class Ckeditor::Asset < ActiveRecord::Base
include Ckeditor::Orm::ActiveRecord::AssetBase

attr_accessible :data, :assetable_type, :assetable_id, :assetable
end
15 changes: 15 additions & 0 deletions test/dummy/app/models/ckeditor/attachment_file.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class Ckeditor::AttachmentFile < Ckeditor::Asset
has_attached_file :data,
:url => "/ckeditor_assets/attachments/:id/:filename",
:path => ":rails_root/public/ckeditor_assets/attachments/:id/:filename"

validates_attachment_size :data, :less_than => 100.megabytes
validates_attachment_presence :data

def url_thumb
@url_thumb ||= begin
extname = File.extname(filename).gsub(/^\./, '')
"/javascripts/ckeditor/filebrowser/images/thumbs/#{extname}.gif"
end
end
end
13 changes: 13 additions & 0 deletions test/dummy/app/models/ckeditor/picture.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class Ckeditor::Picture < Ckeditor::Asset
has_attached_file :data,
:url => "/ckeditor_assets/pictures/:id/:style_:basename.:extension",
:path => ":rails_root/public/ckeditor_assets/pictures/:id/:style_:basename.:extension",
:styles => { :content => '800>', :thumb => '118x100#' }

validates_attachment_size :data, :less_than => 2.megabytes
validates_attachment_presence :data

def url_content
url(:content)
end
end
3 changes: 2 additions & 1 deletion test/dummy/config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

Bundler.require
require "ckeditor"
require "paperclip"

module Dummy
class Application < Rails::Application
Expand All @@ -16,7 +17,7 @@ class Application < Rails::Application
# -- all .rb files in that directory are automatically loaded.

# Custom directories with classes and modules you want to be autoloadable.
# config.autoload_paths += %W(#{config.root}/extras)
config.autoload_paths += %W(#{config.root}/app/models/ckeditor)

# Only load the plugins named here, in the order given (default is alphabetical).
# :all can be used as a placeholder for all plugins not explicitly named.
Expand Down
18 changes: 18 additions & 0 deletions test/dummy/config/initializers/ckeditor.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Use this hook to configure ckeditor
if Object.const_defined?("Ckeditor")
Ckeditor.setup do |config|
# ==> ORM configuration
# Load and configure the ORM. Supports :active_record (default), :mongo_mapper and
# :mongoid (bson_ext recommended) by default. Other ORMs may be
# available as additional gems.
require 'ckeditor/orm/active_record'

# Allowed image file types for upload.
# Set to nil or [] (empty array) for all file types
# config.image_file_types = ["jpg", "jpeg", "png", "gif", "tiff"]

# Allowed attachment file types for upload.
# Set to nil or [] (empty array) for all file types
# config.attachment_file_types = ["doc", "docx", "rar", "zip", "xls", "swf"]
end
end
26 changes: 26 additions & 0 deletions test/dummy/db/migrate/20110705195648_create_ckeditor_assets.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
class CreateCkeditorAssets < ActiveRecord::Migration
def self.up
create_table :ckeditor_assets do |t|
t.string :data_file_name, :null => false
t.string :data_content_type
t.integer :data_file_size

t.integer :assetable_id
t.string :assetable_type, :limit => 30
t.string :type, :limit => 30

# Uncomment it to save images dimensions, if your need it
# t.integer :width
# t.integer :height

t.timestamps
end

add_index "ckeditor_assets", ["assetable_type", "type", "assetable_id"], :name => "idx_ckeditor_assetable_type"
add_index "ckeditor_assets", ["assetable_type", "assetable_id"], :name => "idx_ckeditor_assetable"
end

def self.down
drop_table :ckeditor_assets
end
end
16 changes: 15 additions & 1 deletion test/dummy/db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,21 @@
#
# It's strongly recommended to check this file into your version control system.

ActiveRecord::Schema.define(:version => 20110623120047) do
ActiveRecord::Schema.define(:version => 20110705195648) do

create_table "ckeditor_assets", :force => true do |t|
t.string "data_file_name", :null => false
t.string "data_content_type"
t.integer "data_file_size"
t.integer "assetable_id"
t.string "assetable_type", :limit => 30
t.string "type", :limit => 30
t.datetime "created_at"
t.datetime "updated_at"
end

add_index "ckeditor_assets", ["assetable_type", "assetable_id"], :name => "idx_ckeditor_assetable"
add_index "ckeditor_assets", ["assetable_type", "type", "assetable_id"], :name => "idx_ckeditor_assetable_type"

create_table "posts", :force => true do |t|
t.string "title"
Expand Down
Binary file added test/dummy/test/fixtures/files/rails.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 6cfe947

Please sign in to comment.