From 424535c1aecc9ced51b21ca5186f0a797d883cea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ra=C3=BAl=20Fuentes?= Date: Tue, 14 Aug 2018 14:20:09 +0200 Subject: [PATCH] Add models needed to include images on ckeditor4 --- app/models/ckeditor/asset.rb | 4 +++ app/models/ckeditor/attachment_file.rb | 13 +++++++++ app/models/ckeditor/picture.rb | 14 ++++++++++ .../20180813141443_create_ckeditor_assets.rb | 23 +++++++++++++++ db/schema.rb | 28 ++++++++++++++----- 5 files changed, 75 insertions(+), 7 deletions(-) create mode 100644 app/models/ckeditor/asset.rb create mode 100644 app/models/ckeditor/attachment_file.rb create mode 100644 app/models/ckeditor/picture.rb create mode 100644 db/migrate/20180813141443_create_ckeditor_assets.rb diff --git a/app/models/ckeditor/asset.rb b/app/models/ckeditor/asset.rb new file mode 100644 index 000000000..cf636ed19 --- /dev/null +++ b/app/models/ckeditor/asset.rb @@ -0,0 +1,4 @@ +class Ckeditor::Asset < ActiveRecord::Base + include Ckeditor::Orm::ActiveRecord::AssetBase + include Ckeditor::Backend::Paperclip +end diff --git a/app/models/ckeditor/attachment_file.rb b/app/models/ckeditor/attachment_file.rb new file mode 100644 index 000000000..8d0c2eec7 --- /dev/null +++ b/app/models/ckeditor/attachment_file.rb @@ -0,0 +1,13 @@ +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_presence :data + validates_attachment_size :data, less_than: 100.megabytes + do_not_validate_attachment_file_type :data + + def url_thumb + @url_thumb ||= Ckeditor::Utils.filethumb(filename) + end +end diff --git a/app/models/ckeditor/picture.rb b/app/models/ckeditor/picture.rb new file mode 100644 index 000000000..445c2bbd9 --- /dev/null +++ b/app/models/ckeditor/picture.rb @@ -0,0 +1,14 @@ +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_presence :data + validates_attachment_size :data, less_than: 2.megabytes + validates_attachment_content_type :data, content_type: /\Aimage/ + + def url_content + url(:content) + end +end diff --git a/db/migrate/20180813141443_create_ckeditor_assets.rb b/db/migrate/20180813141443_create_ckeditor_assets.rb new file mode 100644 index 000000000..df3df2862 --- /dev/null +++ b/db/migrate/20180813141443_create_ckeditor_assets.rb @@ -0,0 +1,23 @@ +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.string :data_fingerprint + t.string :type, limit: 30 + + # Uncomment it to save images dimensions, if your need it + t.integer :width + t.integer :height + + t.timestamps null: false + end + + add_index :ckeditor_assets, :type + end + + def self.down + drop_table :ckeditor_assets + end +end diff --git a/db/schema.rb b/db/schema.rb index 9c568058e..e03b22ac7 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20180727140800) do +ActiveRecord::Schema.define(version: 20180813141443) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -294,6 +294,20 @@ ActiveRecord::Schema.define(version: 20180727140800) do t.datetime "updated_at", null: false end + create_table "ckeditor_assets", force: :cascade do |t| + t.string "data_file_name", null: false + t.string "data_content_type" + t.integer "data_file_size" + t.string "data_fingerprint" + t.string "type", limit: 30 + t.integer "width" + t.integer "height" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + end + + add_index "ckeditor_assets", ["type"], name: "index_ckeditor_assets_on_type", using: :btree + create_table "comments", force: :cascade do |t| t.integer "commentable_id" t.string "commentable_type" @@ -1291,6 +1305,12 @@ ActiveRecord::Schema.define(version: 20180727140800) do add_index "votes", ["votable_id", "votable_type", "vote_scope"], name: "index_votes_on_votable_id_and_votable_type_and_vote_scope", using: :btree add_index "votes", ["voter_id", "voter_type", "vote_scope"], name: "index_votes_on_voter_id_and_voter_type_and_vote_scope", using: :btree + create_table "web_sections", force: :cascade do |t| + t.text "name" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + end + create_table "widget_cards", force: :cascade do |t| t.string "title" t.text "description" @@ -1309,12 +1329,6 @@ ActiveRecord::Schema.define(version: 20180727140800) do t.datetime "updated_at", null: false end - create_table "web_sections", force: :cascade do |t| - t.text "name" - t.datetime "created_at", null: false - t.datetime "updated_at", null: false - end - add_foreign_key "administrators", "users" add_foreign_key "annotations", "legacy_legislations" add_foreign_key "annotations", "users"