diff --git a/.gitignore b/.gitignore index 6c81ff6dd..7ad054897 100644 --- a/.gitignore +++ b/.gitignore @@ -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 diff --git a/Gemfile b/Gemfile index 65928fcd4..751fb004e 100644 --- a/Gemfile +++ b/Gemfile @@ -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' diff --git a/Gemfile.lock b/Gemfile.lock index 0a783187d..4d69d67ca 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -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) @@ -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) @@ -113,5 +118,6 @@ DEPENDENCIES ckeditor! mongrel mysql2 (= 0.2.11) + paperclip (~> 2.3.12) rails (= 3.0.9) redgreen (~> 1.2.2) diff --git a/README.rdoc b/README.rdoc index 33e3848af..d9891d3e6 100644 --- a/README.rdoc +++ b/README.rdoc @@ -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: diff --git a/app/controllers/ckeditor/attachment_files_controller.rb b/app/controllers/ckeditor/attachment_files_controller.rb index 9f2ff0703..e91160471 100644 --- a/app/controllers/ckeditor/attachment_files_controller.rb +++ b/app/controllers/ckeditor/attachment_files_controller.rb @@ -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 diff --git a/app/controllers/ckeditor/base_controller.rb b/app/controllers/ckeditor/base_controller.rb index c35948ad7..dcce168eb 100644 --- a/app/controllers/ckeditor/base_controller.rb +++ b/app/controllers/ckeditor/base_controller.rb @@ -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"" diff --git a/app/controllers/ckeditor/pictures_controller.rb b/app/controllers/ckeditor/pictures_controller.rb index 079bfb274..1e6e45d23 100644 --- a/app/controllers/ckeditor/pictures_controller.rb +++ b/app/controllers/ckeditor/pictures_controller.rb @@ -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 diff --git a/lib/ckeditor.rb b/lib/ckeditor.rb index f7b22c237..6575541f0 100644 --- a/lib/ckeditor.rb +++ b/lib/ckeditor.rb @@ -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 diff --git a/lib/ckeditor/engine.rb b/lib/ckeditor/engine.rb index 61b08ab69..0890206cd 100644 --- a/lib/ckeditor/engine.rb +++ b/lib/ckeditor/engine.rb @@ -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 diff --git a/lib/ckeditor/helpers/controllers.rb b/lib/ckeditor/helpers/controllers.rb new file mode 100644 index 000000000..23f2400d1 --- /dev/null +++ b/lib/ckeditor/helpers/controllers.rb @@ -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 diff --git a/lib/generators/ckeditor/templates/models/active_record/attachment_file.rb b/lib/generators/ckeditor/templates/models/active_record/attachment_file.rb index ef3e2f5ae..d76dbcd95 100644 --- a/lib/generators/ckeditor/templates/models/active_record/attachment_file.rb +++ b/lib/generators/ckeditor/templates/models/active_record/attachment_file.rb @@ -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 diff --git a/lib/generators/ckeditor/templates/models/active_record/picture.rb b/lib/generators/ckeditor/templates/models/active_record/picture.rb index db365adab..4192217ce 100644 --- a/lib/generators/ckeditor/templates/models/active_record/picture.rb +++ b/lib/generators/ckeditor/templates/models/active_record/picture.rb @@ -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) diff --git a/test/controllers/pictures_controller_test.rb b/test/controllers/pictures_controller_test.rb new file mode 100644 index 000000000..8bbc2b0b1 --- /dev/null +++ b/test/controllers/pictures_controller_test.rb @@ -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 diff --git a/test/dummy/app/models/ckeditor/asset.rb b/test/dummy/app/models/ckeditor/asset.rb new file mode 100644 index 000000000..9c33c05a0 --- /dev/null +++ b/test/dummy/app/models/ckeditor/asset.rb @@ -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 diff --git a/test/dummy/app/models/ckeditor/attachment_file.rb b/test/dummy/app/models/ckeditor/attachment_file.rb new file mode 100644 index 000000000..d76dbcd95 --- /dev/null +++ b/test/dummy/app/models/ckeditor/attachment_file.rb @@ -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 diff --git a/test/dummy/app/models/ckeditor/picture.rb b/test/dummy/app/models/ckeditor/picture.rb new file mode 100644 index 000000000..4192217ce --- /dev/null +++ b/test/dummy/app/models/ckeditor/picture.rb @@ -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 diff --git a/test/dummy/config/application.rb b/test/dummy/config/application.rb index e8a687b27..7100761ac 100644 --- a/test/dummy/config/application.rb +++ b/test/dummy/config/application.rb @@ -8,6 +8,7 @@ Bundler.require require "ckeditor" +require "paperclip" module Dummy class Application < Rails::Application @@ -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. diff --git a/test/dummy/config/initializers/ckeditor.rb b/test/dummy/config/initializers/ckeditor.rb new file mode 100644 index 000000000..808f412c5 --- /dev/null +++ b/test/dummy/config/initializers/ckeditor.rb @@ -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 diff --git a/test/dummy/db/migrate/20110705195648_create_ckeditor_assets.rb b/test/dummy/db/migrate/20110705195648_create_ckeditor_assets.rb new file mode 100644 index 000000000..9ad4c15a2 --- /dev/null +++ b/test/dummy/db/migrate/20110705195648_create_ckeditor_assets.rb @@ -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 diff --git a/test/dummy/db/schema.rb b/test/dummy/db/schema.rb index 4b098eb53..4a1b88c53 100644 --- a/test/dummy/db/schema.rb +++ b/test/dummy/db/schema.rb @@ -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" diff --git a/test/dummy/test/fixtures/files/rails.png b/test/dummy/test/fixtures/files/rails.png new file mode 100644 index 000000000..d5edc04e6 Binary files /dev/null and b/test/dummy/test/fixtures/files/rails.png differ