#82: added Contents controller, logical model, views

This commit is contained in:
Vokhmin Alexey V 2013-07-05 20:08:09 +04:00
parent 95beeae221
commit d4c6c5edbc
8 changed files with 150 additions and 10 deletions

View File

@ -1,5 +1,6 @@
# -*- encoding : utf-8 -*- # -*- encoding : utf-8 -*-
class Api::V1::BaseController < ApplicationController class Api::V1::BaseController < ApplicationController
include PaginateHelper
#respond_to :json #respond_to :json
helper_method :member_path helper_method :member_path
@ -72,15 +73,6 @@ class Api::V1::BaseController < ApplicationController
end end
end end
def paginate_params
per_page = params[:per_page].to_i
per_page = 20 if per_page < 1
per_page = 100 if per_page >100
page = params[:page].to_i
page = nil if page == 0
{:page => page, :per_page => per_page}
end
def render_json_response(subject, message, status = 200) def render_json_response(subject, message, status = 200)
id = status != 200 ? nil : subject.id id = status != 200 ? nil : subject.id

View File

@ -0,0 +1,16 @@
class Platforms::ContentsController < Platforms::BaseController
include PaginateHelper
before_filter :authenticate_user!
skip_before_filter :authenticate_user! if APP_CONFIG['anonymous_access']
load_and_authorize_resource :platform
def index
@path = '/' << params[:path].to_s
@term = params[:term]
@contents = PlatformContent.find_by_platform(@platform, @path, @term)
.paginate(paginate_params)
end
end

View File

@ -0,0 +1,13 @@
# -*- encoding : utf-8 -*-
module PaginateHelper
def paginate_params
per_page = params[:per_page].to_i
per_page = 20 if per_page < 1
per_page = 100 if per_page >100
page = params[:page].to_i
page = nil if page == 0
{:page => page, :per_page => per_page}
end
end

View File

@ -0,0 +1,82 @@
class PlatformContent
# ------------------
# *** ATTRIBUTES ***
# ------------------
attr_reader :path
# ---------------
# *** METHODS ***
# ---------------
def initialize(platform, path)
@platform, @path = platform, path
end
def build_list
return @build_list if !!@build_list
return nil if path !~ /\/(release|updates)+\/\w/
return nil unless repository_name = path.match(/\/[\w]+\/(release|updates)\//)
repository_name = repository_name[0].gsub(/\/(release|updates)\/$/, '').gsub('/', '')
repository = @platform.repositories.where(:name => repository_name).first
return nil unless repository
if @platform.main?
build_for_platform = @platform
else
bfp_name = path.match(/\/#{@platform.name}\/repository\/[\w]+\//)
return nil unless bfp_name
bfp_name = bfp_name[0].gsub(/\/#{@platform.name}\/repository\//, '').gsub('/', '')
build_for_platform = Platform.main.find_by_name bfp_name
return nil unless build_for_platform
end
@build_list = BuildList.for_status(BuildList::BUILD_PUBLISHED)
.for_platform(build_for_platform)
.scoped_to_save_platform(@platform)
.where(:save_to_repository_id => repository)
.where(:build_list_packages => {:fullname => name, :actual => true})
.joins(:packages)
.last
return @build_list
end
def name
@name ||= @path.gsub(/.*#{File::SEPARATOR}/, '')
end
def size
@size ||= File.size(@path)
end
def is_folder?
@is_folder.nil? ? (@is_folder = File.directory?(path)) : @is_folder
end
def download_url
suffix = path.gsub(/^#{@platform.path}/, '')
"#{APP_CONFIG['downloads_url']}/#{@platform.name}#{suffix}"
end
# ---------------------
# *** CLASS METHODS ***
# ---------------------
def self.find_by_platform(platform, path, term)
term = (term.present? && term =~ /\w/) ? term : ''
path = path.split(File::SEPARATOR)
.select{ |p| p.present? && p =~ /\w/ }
.join(File::SEPARATOR)
results = Dir.glob(File.join(platform.path, path, "*#{term}*"))
if term
results = results.sort_by(&:length)
else
results = results.sort
end
results.map{ |p| PlatformContent.new(platform, p) }
end
end

View File

@ -10,6 +10,8 @@
= link_to t("layout.platforms.about"), platform_path(@platform) = link_to t("layout.platforms.about"), platform_path(@platform)
%li{:class => (contr == :repositories) ? 'active' : ''} %li{:class => (contr == :repositories) ? 'active' : ''}
= link_to t("layout.repositories.list_header"), platform_repositories_path(@platform) = link_to t("layout.repositories.list_header"), platform_repositories_path(@platform)
%li{:class => (contr == :contents) ? 'active' : ''}
= link_to t('layout.platforms.contents'), platform_contents_path(@platform)
- if can? :show, @platform - if can? :show, @platform
%li{:class => (act == :index && contr == :maintainers) ? 'active' : nil} %li{:class => (act == :index && contr == :maintainers) ? 'active' : nil}
= link_to t("layout.platforms.maintainers"), platform_maintainers_path(@platform) = link_to t("layout.platforms.maintainers"), platform_maintainers_path(@platform)

View File

@ -0,0 +1,30 @@
- set_meta_tags :title => [title_object(@platform), t('layout.platforms.contents')]
= render 'platforms/base/submenu'
= render 'platforms/base/sidebar'
%div
= "#{@platform.name}: #{@path}"
%table.tablesorter.project{:cellpadding => "0", :cellspacing => "0"}
%tbody
- if @path != '/'
%tr
%td= link_to '../', "#{platform_contents_path(@platform)}#{@path}/../"
%td
%td
- (@contents.select(&:is_folder?) | @contents).each do |content|
%tr
%td
- if content.is_folder?
- pic = 'folder.png'
- path = "#{platform_contents_path(@platform)}#{@path}/#{content.name}"
.pic= image_tag pic || 'code.png'
.name= link_to(content.name, path || content.download_url, :class => 'files-see')
%td= link_to t('activerecord.models.build_list'), content.build_list if content.build_list
%td= number_to_human_size(content.size) unless content.is_folder?
.both
= will_paginate @contents

View File

@ -2,7 +2,9 @@
= render 'submenu' = render 'submenu'
= render 'sidebar' = render 'sidebar'
%h3.fix= "#{t("layout.platforms.about")} #{@platform.name}" %h3.fix
= t 'layout.platforms.about'
= link_to @platform.name, platform_contents_path(@platform)
%p= @platform.description %p= @platform.description

View File

@ -161,6 +161,9 @@ Rosa::Application.routes.draw do
get :advisories get :advisories
end end
resources :contents, :only => [:index]
match '/contents/*path' => 'contents#index', :format => false
resources :mass_builds, :only => [:create, :new, :index] do resources :mass_builds, :only => [:create, :new, :index] do
member do member do
post :cancel post :cancel