mirror of
https://github.com/internetee/registry.git
synced 2025-08-11 20:19:34 +02:00
refacto controllers
This commit is contained in:
parent
c183ff103c
commit
81e2c3aa29
2 changed files with 43 additions and 26 deletions
|
@ -2,6 +2,8 @@ module Admin
|
||||||
class DomainVersionsController < BaseController
|
class DomainVersionsController < BaseController
|
||||||
load_and_authorize_resource class: Version::DomainVersion
|
load_and_authorize_resource class: Version::DomainVersion
|
||||||
|
|
||||||
|
PER_PAGE = 7
|
||||||
|
|
||||||
def index
|
def index
|
||||||
params[:q] ||= {}
|
params[:q] ||= {}
|
||||||
|
|
||||||
|
@ -55,25 +57,18 @@ module Admin
|
||||||
end
|
end
|
||||||
|
|
||||||
def show
|
def show
|
||||||
per_page = 7
|
|
||||||
if params[:current]
|
if params[:current]
|
||||||
@domain = Domain.find(params[:domain_id] || params[:id])
|
@domain = Domain.find(params[:domain_id] || params[:id])
|
||||||
@version = nil
|
|
||||||
else
|
else
|
||||||
@version = Version::DomainVersion.find(params[:id])
|
@version = Version::DomainVersion.find(params[:id])
|
||||||
@domain = Domain.find(@version.item_id)
|
@domain = Domain.find(@version.item_id)
|
||||||
end
|
end
|
||||||
|
|
||||||
@versions = Version::DomainVersion.where(item_id: @domain.id).order(created_at: :desc, id: :desc)
|
@versions = Version::DomainVersion.where(item_id: @domain.id).order(created_at: :desc, id: :desc)
|
||||||
@versions_map = @versions.all.map(&:id)
|
@versions_map = @versions.all.map(&:id)
|
||||||
|
|
||||||
if params[:page].blank?
|
get_page if params[:page].blank?
|
||||||
counter = @version ? (@versions_map.index(@version.id) + 1) : 1
|
@versions = @versions.page(params[:page]).per(PER_PAGE)
|
||||||
page = counter / per_page
|
|
||||||
page += 1 if (counter % per_page) != 0
|
|
||||||
params[:page] = page
|
|
||||||
end
|
|
||||||
|
|
||||||
@versions = @versions.page(params[:page]).per(per_page)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def search
|
def search
|
||||||
|
@ -86,6 +81,13 @@ module Admin
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
|
def get_page
|
||||||
|
counter = @version ? (@versions_map.index(@version.id) + 1) : 1
|
||||||
|
page = counter / PER_PAGE
|
||||||
|
page += 1 if (counter % PER_PAGE) != 0
|
||||||
|
params[:page] = page
|
||||||
|
end
|
||||||
|
|
||||||
def fix_date_params
|
def fix_date_params
|
||||||
params_copy = params[:q].deep_dup
|
params_copy = params[:q].deep_dup
|
||||||
created_at = params_copy['created_at_lteq']
|
created_at = params_copy['created_at_lteq']
|
||||||
|
|
|
@ -52,24 +52,12 @@ module Admin
|
||||||
end
|
end
|
||||||
|
|
||||||
def versions
|
def versions
|
||||||
@domain = Domain.where(id: params[:domain_id]).includes({ versions: :item }).first
|
@domain = get_domain_with_versions
|
||||||
@versions = @domain.versions
|
@versions = @domain.versions
|
||||||
@old_versions = Kaminari.paginate_array(@versions.not_creates.reverse)
|
@old_versions = paginate_versions(@versions.not_creates.reverse)
|
||||||
.page(params[:page])
|
|
||||||
.per(DEFAULT_VERSIONS_PER_PAGE)
|
|
||||||
|
|
||||||
@post_update_domains = []
|
post_update_domains = generate_collection_of_post_update_domains(@old_versions.to_a)
|
||||||
old_versions_arr = @old_versions.to_a
|
@post_update_domains = sort_post_update_domains(post_update_domains)
|
||||||
old_versions_arr.each_with_index do |version, idx|
|
|
||||||
next_version = old_versions_arr[idx - 1] # reverse order!
|
|
||||||
if next_version
|
|
||||||
@post_update_domains << (next_version.reify || @domain)
|
|
||||||
else
|
|
||||||
@post_update_domains << @domain
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
@post_update_domains.sort_by! { |d| -d.updated_at.to_i }
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def download
|
def download
|
||||||
|
@ -84,6 +72,33 @@ module Admin
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
|
def get_domain_with_versions
|
||||||
|
Domain.where(id: params[:domain_id]).includes({ versions: :item }).first
|
||||||
|
end
|
||||||
|
|
||||||
|
def paginate_versions(versions)
|
||||||
|
Kaminari.paginate_array(versions).page(params[:page]).per(DEFAULT_VERSIONS_PER_PAGE)
|
||||||
|
end
|
||||||
|
|
||||||
|
def generate_collection_of_post_update_domains(old_versions_arr)
|
||||||
|
post_update_domains = []
|
||||||
|
|
||||||
|
old_versions_arr.each_with_index do |version, idx|
|
||||||
|
next_version = old_versions_arr[idx - 1] # reverse order!
|
||||||
|
if next_version
|
||||||
|
post_update_domains << (next_version.reify || @domain)
|
||||||
|
else
|
||||||
|
post_update_domains << @domain
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
post_update_domains
|
||||||
|
end
|
||||||
|
|
||||||
|
def sort_post_update_domains(post_update_domains)
|
||||||
|
post_update_domains.sort_by! { |d| -d.updated_at.to_i }
|
||||||
|
end
|
||||||
|
|
||||||
def set_domain
|
def set_domain
|
||||||
@domain = Domain.find(params[:id])
|
@domain = Domain.find(params[:id])
|
||||||
end
|
end
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue