Commit 2cd55b54 by Hoang Phuc Do

Add admin action for product and category

parent 4b77b135
...@@ -46,6 +46,8 @@ gem 'devise', '~> 4.3' ...@@ -46,6 +46,8 @@ gem 'devise', '~> 4.3'
gem 'rubocop', '~> 0.49.1' gem 'rubocop', '~> 0.49.1'
# The administration framework for Ruby on Rails. # The administration framework for Ruby on Rails.
gem 'activeadmin', '~> 1.0' gem 'activeadmin', '~> 1.0'
# CKEditor is a WYSIWYG editor to be used inside web pages
gem 'ckeditor', '~> 4.2', '>= 4.2.3'
# Use Capistrano for deployment # Use Capistrano for deployment
# gem 'capistrano-rails', group: :development # gem 'capistrano-rails', group: :development
......
...@@ -83,6 +83,12 @@ GEM ...@@ -83,6 +83,12 @@ GEM
mime-types (>= 1.16) mime-types (>= 1.16)
childprocess (0.7.0) childprocess (0.7.0)
ffi (~> 1.0, >= 1.0.11) ffi (~> 1.0, >= 1.0.11)
ckeditor (4.2.3)
cocaine
orm_adapter (~> 0.5.0)
climate_control (0.2.0)
cocaine (0.5.8)
climate_control (>= 0.0.3, < 1.0)
coffee-rails (4.2.2) coffee-rails (4.2.2)
coffee-script (>= 2.2.0) coffee-script (>= 2.2.0)
railties (>= 4.0.0) railties (>= 4.0.0)
...@@ -274,6 +280,7 @@ DEPENDENCIES ...@@ -274,6 +280,7 @@ DEPENDENCIES
byebug byebug
capybara (~> 2.13) capybara (~> 2.13)
carrierwave (~> 1.0) carrierwave (~> 1.0)
ckeditor (~> 4.2, >= 4.2.3)
coffee-rails (~> 4.2) coffee-rails (~> 4.2)
devise (~> 4.3) devise (~> 4.3)
faker (~> 1.6, >= 1.6.3) faker (~> 1.6, >= 1.6.3)
......
ActiveAdmin.register Category do
menu priority: 3
filter :title
filter :description
end
ActiveAdmin.register Product do
menu priority: 2
permit_params :title, :description, :sku, :price, :image_url, :remove_image_url, :category_id, :user_id
index do
selectable_column
column("ID", sortable: :id) { |product| link_to "##{product.id}", admin_product_path(product) }
column("Product title", sortable: :title) { |product| link_to "#{product.title}", admin_product_path(product) }
column("Price", sortable: :price) { |product| number_to_currency(product.price) }
column("Category", sortable: :category_id) { |product| auto_link product.category }
end
filter :title
filter :user, collection: User.pluck(:email, :id), label: 'User email'
show do
attributes_table do
row :title
row :sku
row(:description) { |product| raw(product.description) }
row(:price) { |product| number_to_currency(product.price) }
row :category
row :user
row :created_at
row :updated_at
end
end
sidebar "Product thumbnail", only: :show, if: proc{ product.image_url.present? } do
image_tag(get_product_thumbnail(product, 170, 204), style: 'width: 100%;')
end
form do |f|
f.semantic_errors
f.inputs 'Product Details' do
f.input :title
f.input :sku
f.input :description, as: :ckeditor
f.input :price
f.input :category
f.input :user, collection: User.pluck(:email, :id)
end
f.inputs 'Product image', multipart: true do
f.input :image_url, as: :file, hint: f.object.image_url.present? ? image_tag(f.object.image_url.url)
: content_tag(:span, 'Upload your product image')
f.input :image_url_cache, as: :hidden
f.input :remove_image_url, as: :boolean, label: 'Remove this image?' unless f.object.image_url.blank?
end
f.actions
end
end
...@@ -11,6 +11,16 @@ ActiveAdmin.register User do ...@@ -11,6 +11,16 @@ ActiveAdmin.register User do
actions actions
end end
show do
attributes_table do
row :email
row :super_admin
row :current_sign_in_at
row :sign_in_count
row :created_at
end
end
filter :email filter :email
filter :current_sign_in_at filter :current_sign_in_at
filter :sign_in_count filter :sign_in_count
...@@ -21,6 +31,7 @@ ActiveAdmin.register User do ...@@ -21,6 +31,7 @@ ActiveAdmin.register User do
f.input :email f.input :email
f.input :password f.input :password
f.input :password_confirmation f.input :password_confirmation
f.input :super_admin, label: 'Is super admin?'
end end
f.actions f.actions
end end
......
#= require active_admin/base #= require active_admin/base
#= require ckeditor/init
#= require admin/custom
$(document).ready(function(){
if ( $('.ckeditor.input').find('label').length ) {
$('.ckeditor.input').find('label').css('float','none');
}
});
...@@ -15,3 +15,7 @@ ...@@ -15,3 +15,7 @@
// For example, to change the default status-tag color: // For example, to change the default status-tag color:
// //
// .status_tag { background: #6090DB; } // .status_tag { background: #6090DB; }
.cke_editor_product_description {
margin-left: 20%!important;
}
\ No newline at end of file
...@@ -4,7 +4,7 @@ class ApplicationController < ActionController::Base ...@@ -4,7 +4,7 @@ class ApplicationController < ActionController::Base
def authenticate_active_admin_user! def authenticate_active_admin_user!
authenticate_user! authenticate_user!
unless current_user.super_admin? unless current_user.super_admin?
flash[:error] = "Unauthorized access!" flash[:error] = 'Unauthorized access!'
redirect_to root_path redirect_to root_path
end end
end end
......
class Ckeditor::Asset < ActiveRecord::Base
include Ckeditor::Orm::ActiveRecord::AssetBase
delegate :url, :current_path, :content_type, to: :data
validates :data, presence: true
end
class Ckeditor::AttachmentFile < Ckeditor::Asset
mount_uploader :data, CkeditorAttachmentFileUploader, mount_on: :data_file_name
def url_thumb
@url_thumb ||= Ckeditor::Utils.filethumb(filename)
end
end
class Ckeditor::Picture < Ckeditor::Asset
mount_uploader :data, CkeditorPictureUploader, mount_on: :data_file_name
def url_content
url(:content)
end
end
class Product < ApplicationRecord class Product < ApplicationRecord
attr_accessor :image_url_cache
belongs_to :category belongs_to :category
belongs_to :user belongs_to :user
mount_uploader :image_url, PictureUploader mount_uploader :image_url, PictureUploader
......
# encoding: utf-8
require 'carrierwave'
class CkeditorAttachmentFileUploader < CarrierWave::Uploader::Base
include Ckeditor::Backend::CarrierWave
# Include RMagick or ImageScience support:
# include CarrierWave::RMagick
# include CarrierWave::MiniMagick
# include CarrierWave::ImageScience
# Choose what kind of storage to use for this uploader:
storage :file
# Override the directory where uploaded files will be stored.
# This is a sensible default for uploaders that are meant to be mounted:
def store_dir
"uploads/ckeditor/attachments/#{model.id}"
end
# Provide a default URL as a default if there hasn't been a file uploaded:
# def default_url
# "/images/fallback/" + [version_name, "default.png"].compact.join('_')
# end
# Process files as they are uploaded:
# process :scale => [200, 300]
#
# def scale(width, height)
# # do something
# end
# Add a white list of extensions which are allowed to be uploaded.
# For images you might use something like this:
def extension_white_list
Ckeditor.attachment_file_types
end
end
# encoding: utf-8
class CkeditorPictureUploader < CarrierWave::Uploader::Base
include Ckeditor::Backend::CarrierWave
# Include RMagick or ImageScience support:
# include CarrierWave::RMagick
include CarrierWave::MiniMagick
# include CarrierWave::ImageScience
# Choose what kind of storage to use for this uploader:
storage :file
# Override the directory where uploaded files will be stored.
# This is a sensible default for uploaders that are meant to be mounted:
def store_dir
"uploads/ckeditor/pictures/#{model.id}"
end
# Provide a default URL as a default if there hasn't been a file uploaded:
# def default_url
# "/images/fallback/" + [version_name, "default.png"].compact.join('_')
# end
# Process files as they are uploaded:
# process scale: [200, 300]
#
# def scale(width, height)
# # do something
# end
process :extract_dimensions
# Create different versions of your uploaded files:
version :thumb do
process resize_to_fill: [118, 100]
end
version :content do
process resize_to_limit: [800, 800]
end
# Add a white list of extensions which are allowed to be uploaded.
# For images you might use something like this:
def extension_white_list
Ckeditor.image_file_types
end
end
# Use this hook to configure 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
# By default: %w(jpg jpeg png gif tiff)
# config.image_file_types = %w(jpg jpeg png gif tiff)
# Allowed flash file types for upload.
# Set to nil or [] (empty array) for all file types
# By default: %w(jpg jpeg png gif tiff)
# config.flash_file_types = %w(swf)
# Allowed attachment file types for upload.
# Set to nil or [] (empty array) for all file types
# By default: %w(doc docx xls odt ods pdf rar zip tar tar.gz swf)
# config.attachment_file_types = %w(doc docx xls odt ods pdf rar zip tar tar.gz swf)
# Setup authorization to be run as a before filter
# By default: there is no authorization.
# config.authorize_with :cancan
# Override parent controller CKEditor inherits from
# By default: 'ApplicationController'
# config.parent_controller = 'MyController'
# Asset model classes
# config.picture_model { Ckeditor::Picture }
# config.attachment_file_model { Ckeditor::AttachmentFile }
# Paginate assets
# By default: 24
# config.default_per_page = 24
# Customize ckeditor assets path
# By default: nil
# config.asset_path = 'http://www.example.com/assets/ckeditor/'
# To reduce the asset precompilation time, you can limit plugins and/or languages to those you need:
# By default: nil (no limit)
# config.assets_languages = ['en', 'uk']
# config.assets_plugins = ['image', 'smiley']
# CKEditor CDN
# More info here http://cdn.ckeditor.com/
# By default: nil (CDN disabled)
# config.cdn_url = '//cdn.ckeditor.com/4.6.2/standard/ckeditor.js'
# JS config url
# Used when CKEditor CDN enabled
# By default: "ckeditor/config.js"
# config.js_config_url = 'ckeditor/config.js'
end
...@@ -5,4 +5,5 @@ Rails.application.routes.draw do ...@@ -5,4 +5,5 @@ Rails.application.routes.draw do
resources :products resources :products
devise_for :users devise_for :users
ActiveAdmin.routes(self) ActiveAdmin.routes(self)
mount Ckeditor::Engine => '/ckeditor'
end end
class CreateCkeditorAssets < ActiveRecord::Migration[5.1]
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 :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
...@@ -10,7 +10,7 @@ ...@@ -10,7 +10,7 @@
# #
# It's strongly recommended that you check this file into your version control system. # It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 20170609074124) do ActiveRecord::Schema.define(version: 20170609152744) do
create_table "active_admin_comments", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8" do |t| create_table "active_admin_comments", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8" do |t|
t.string "namespace" t.string "namespace"
...@@ -31,6 +31,18 @@ ActiveRecord::Schema.define(version: 20170609074124) do ...@@ -31,6 +31,18 @@ ActiveRecord::Schema.define(version: 20170609074124) do
t.text "description" t.text "description"
end end
create_table "ckeditor_assets", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8" do |t|
t.string "data_file_name", null: false
t.string "data_content_type"
t.integer "data_file_size"
t.string "type", limit: 30
t.integer "width"
t.integer "height"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["type"], name: "index_ckeditor_assets_on_type"
end
create_table "products", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8" do |t| create_table "products", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8" do |t|
t.string "title" t.string "title"
t.text "description" t.text "description"
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment