Commit ad64e0bf by Mykhailo Makohin

add sort to projects

parent 9bbf49c4
...@@ -36,6 +36,8 @@ gem 'mini_magick' ...@@ -36,6 +36,8 @@ gem 'mini_magick'
gem 'video_info', '~> 2.7' gem 'video_info', '~> 2.7'
gem 'acts_as_list' gem 'acts_as_list'
gem 'rails_sortable' gem 'rails_sortable'
gem 'aasm'
gem 'ransack'
group :development, :test do group :development, :test do
......
GEM GEM
remote: https://rubygems.org/ remote: https://rubygems.org/
specs: specs:
aasm (4.12.3)
concurrent-ruby (~> 1.0)
actioncable (5.0.7.2) actioncable (5.0.7.2)
actionpack (= 5.0.7.2) actionpack (= 5.0.7.2)
nio4r (>= 1.2, < 3.0) nio4r (>= 1.2, < 3.0)
...@@ -319,6 +321,7 @@ PLATFORMS ...@@ -319,6 +321,7 @@ PLATFORMS
ruby ruby
DEPENDENCIES DEPENDENCIES
aasm
activeadmin activeadmin
acts_as_list acts_as_list
awesome_print awesome_print
...@@ -347,6 +350,7 @@ DEPENDENCIES ...@@ -347,6 +350,7 @@ DEPENDENCIES
puma (~> 3.0) puma (~> 3.0)
rails (~> 5.0.7, >= 5.0.7.2) rails (~> 5.0.7, >= 5.0.7.2)
rails_sortable rails_sortable
ransack
remotipart remotipart
sass-rails (~> 5.0) sass-rails (~> 5.0)
simple_form simple_form
......
...@@ -52,7 +52,7 @@ ActiveAdmin.register Project do ...@@ -52,7 +52,7 @@ ActiveAdmin.register Project do
:title_uk, :title_en, :heading_uk, :heading_en, :slug, :short_description_uk, :title_uk, :title_en, :heading_uk, :heading_en, :slug, :short_description_uk,
:short_description_en, :description_uk,:description_en, :site, :short_description_en, :description_uk,:description_en, :site,
:link_to_facebook, :required_amount, :related_links_uk, :related_links_en, :link_to_facebook, :required_amount, :related_links_uk, :related_links_en,
:footer_photo, :remove_footer_photo, :footer_photo, :remove_footer_photo, :aasm_state,
project_photos_attributes: [:id, :project_id, :photo, :title_uk, project_photos_attributes: [:id, :project_id, :photo, :title_uk,
:title_en, :_destroy], :title_en, :_destroy],
project_galeries_attributes: [:id, :project_id, :photo, :name_uk, project_galeries_attributes: [:id, :project_id, :photo, :name_uk,
...@@ -72,6 +72,7 @@ ActiveAdmin.register Project do ...@@ -72,6 +72,7 @@ ActiveAdmin.register Project do
form html: { multipart: true } do |f| form html: { multipart: true } do |f|
f.inputs do f.inputs do
f.input :aasm_state, as: :select, collection: [:approved, :display], label: 'State'
f.input :photo, label: "#{t "active_admin.attributes.project.photo"}", f.input :photo, label: "#{t "active_admin.attributes.project.photo"}",
hint: f.object.photo.present? ? image_tag(f.object.photo.url(:thumb_small)) : content_tag(:span, "no file yet") hint: f.object.photo.present? ? image_tag(f.object.photo.url(:thumb_small)) : content_tag(:span, "no file yet")
f.input :photo_preview, label: "#{t "active_admin.attributes.project.photo_preview"}", f.input :photo_preview, label: "#{t "active_admin.attributes.project.photo_preview"}",
...@@ -210,6 +211,9 @@ ActiveAdmin.register Project do ...@@ -210,6 +211,9 @@ ActiveAdmin.register Project do
show do show do
attributes_table do attributes_table do
row :id row :id
row "State" do |p|
p.aasm_state
end
row I18n.t "active_admin.attributes.project.photo" do |p| row I18n.t "active_admin.attributes.project.photo" do |p|
p.photo? ? image_tag(p.photo_url(:thumb_small)) : image_tag("default_img.png") p.photo? ? image_tag(p.photo_url(:thumb_small)) : image_tag("default_img.png")
end end
......
class HomeController < ApplicationController class HomeController < ApplicationController
STATUSES = %i[in_progress implemented]
def index def index
@projects = Project.all status_order = params[:q]&.key?(:s) ? params[:q][:s] : 'status asc'
@q = Project.where(status: STATUSES).ransack(params[:q])
@projects = @q.result.order_by_status(status_order)
end end
end end
class Project < ApplicationRecord class Project < ApplicationRecord
enum status: [:in_progress, :implemented]
STATUS_ASC_ORDERS = [1, 0].freeze
STATUS_DESC_ORDERS = STATUS_ASC_ORDERS.reverse.freeze
STATUSES_ORDER_MAP = { "status asc" => STATUS_ASC_ORDERS, "status desc" => STATUS_DESC_ORDERS}
scope :order_by_status, -> (status_order) {
order_by = ['CASE']
STATUSES_ORDER_MAP[status_order].each_with_index do |status, index|
order_by << "WHEN status=#{status} THEN #{index}"
end
order_by << 'END'
order(order_by.join(' '))
}
def self.ransortable_attributes(auth_object = nil)
ransackable_attributes(auth_object) + %w(
order_by_status
)
end
include AASM
aasm do
state :registered, initial: true
state :approved, :display
event :approved do
transitions from: [:registered, :display], to: :approved
end
event :display do
transitions from: [:registered, :approved], to: :display
end
end
translates :title, :individual_type, :heading, :related_links, translates :title, :individual_type, :heading, :related_links,
:short_description, :description :short_description, :description
globalize_accessors :locales => [:en, :uk], :attributes => [:individual_type, :title, globalize_accessors :locales => [:en, :uk], :attributes => [:individual_type, :title,
...@@ -13,7 +48,7 @@ class Project < ApplicationRecord ...@@ -13,7 +48,7 @@ class Project < ApplicationRecord
mount_uploader :footer_photo, AvatarUploader mount_uploader :footer_photo, AvatarUploader
enum types: [:program, :project] enum types: [:program, :project]
enum status: [:in_progress, :implemented] # enum status: [:in_progress, :implemented]
has_many :project_partials, -> { order(position: :asc) }, dependent: :destroy has_many :project_partials, -> { order(position: :asc) }, dependent: :destroy
has_many :project_galeries, inverse_of: :project, dependent: :destroy has_many :project_galeries, inverse_of: :project, dependent: :destroy
has_many :project_qoutes, inverse_of: :project, dependent: :destroy has_many :project_qoutes, inverse_of: :project, dependent: :destroy
...@@ -34,4 +69,10 @@ class Project < ApplicationRecord ...@@ -34,4 +69,10 @@ class Project < ApplicationRecord
:heading_uk, :heading_en, :site, :link_to_facebook, :related_links_uk, :heading_uk, :heading_en, :site, :link_to_facebook, :related_links_uk,
:related_links_en, maximum: 200 :related_links_en, maximum: 200
validates_format_of :slug, :with => /\A[a-z0-9]+\z/i validates_format_of :slug, :with => /\A[a-z0-9]+\z/i
def self.ransortable_attributes(auth_object = nil)
ransackable_attributes(auth_object) + %w(
order_by_status
)
end
end end
...@@ -4,7 +4,7 @@ class AvatarUploader < CarrierWave::Uploader::Base ...@@ -4,7 +4,7 @@ class AvatarUploader < CarrierWave::Uploader::Base
process :resize_to_fit => [1280, 1024] process :resize_to_fit => [1280, 1024]
version :thumb_small do version :thumb_small do
process resize_to_fill: [320,200] process resize_to_fill: [300,152]
end end
version :thumb do version :thumb do
......
...@@ -2,7 +2,8 @@ ...@@ -2,7 +2,8 @@
.container .container
.text_container .text_container
= raw @project.description = raw @project.description
- unless @project.photo_before.blank?
#slider.beer-slider{"data-beer-label" => "before"} #slider.beer-slider{"data-beer-label" => "before"}
= image_tag(@project.photo_before.url) = image_tag(@project.photo_before_url)
.beer-reveal{"data-beer-label" => "after"} .beer-reveal{"data-beer-label" => "after"}
= image_tag(@project.photo_after.url) = image_tag(@project.photo_after.url)
...@@ -24,6 +24,7 @@ uk: ...@@ -24,6 +24,7 @@ uk:
projects: 'проекти' projects: 'проекти'
all: 'Всі' all: 'Всі'
in_progress: 'В розробці' in_progress: 'В розробці'
implemented: 'Реалізовано'
project: 'Проект' project: 'Проект'
program: 'Програма' program: 'Програма'
more: 'більше' more: 'більше'
......
class AddColumnToProjects < ActiveRecord::Migration[5.0]
def change
add_column :projects, :aasm_state, :string
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: 20191016070020) do ActiveRecord::Schema.define(version: 20191024074653) 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"
...@@ -200,6 +200,7 @@ ActiveRecord::Schema.define(version: 20191016070020) do ...@@ -200,6 +200,7 @@ ActiveRecord::Schema.define(version: 20191016070020) do
t.string "footer_photo" t.string "footer_photo"
t.datetime "created_at", null: false t.datetime "created_at", null: false
t.datetime "updated_at", null: false t.datetime "updated_at", null: false
t.string "aasm_state"
end end
create_table "users", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8" do |t| create_table "users", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8" do |t|
......
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