links_controller.rb revision 08464a6b0d787688a7a3d60e22c7dc9a62e30b71
72ae534f5aef6d2e5d3f2f51299aede5abf9687eJakub Hrozek#
72ae534f5aef6d2e5d3f2f51299aede5abf9687eJakub Hrozek# Controller for Links
72ae534f5aef6d2e5d3f2f51299aede5abf9687eJakub Hrozek#
72ae534f5aef6d2e5d3f2f51299aede5abf9687eJakub Hrozekclass LinksController < InheritedResources::Base
72ae534f5aef6d2e5d3f2f51299aede5abf9687eJakub Hrozek
72ae534f5aef6d2e5d3f2f51299aede5abf9687eJakub Hrozek respond_to :json, :xml
72ae534f5aef6d2e5d3f2f51299aede5abf9687eJakub Hrozek has_pagination
72ae534f5aef6d2e5d3f2f51299aede5abf9687eJakub Hrozek has_scope :search
72ae534f5aef6d2e5d3f2f51299aede5abf9687eJakub Hrozek belongs_to :ontology, :optional => true
72ae534f5aef6d2e5d3f2f51299aede5abf9687eJakub Hrozek load_and_authorize_resource :except => [:index, :show]
72ae534f5aef6d2e5d3f2f51299aede5abf9687eJakub Hrozek before_filter :check_read_permissions
72ae534f5aef6d2e5d3f2f51299aede5abf9687eJakub Hrozek
72ae534f5aef6d2e5d3f2f51299aede5abf9687eJakub Hrozek def index
72ae534f5aef6d2e5d3f2f51299aede5abf9687eJakub Hrozek super do |format|
72ae534f5aef6d2e5d3f2f51299aede5abf9687eJakub Hrozek format.html do
72ae534f5aef6d2e5d3f2f51299aede5abf9687eJakub Hrozek @search = params[:search]
72ae534f5aef6d2e5d3f2f51299aede5abf9687eJakub Hrozek @search = nil if @search.blank?
72ae534f5aef6d2e5d3f2f51299aede5abf9687eJakub Hrozek end
72ae534f5aef6d2e5d3f2f51299aede5abf9687eJakub Hrozek end
72ae534f5aef6d2e5d3f2f51299aede5abf9687eJakub Hrozek end
72ae534f5aef6d2e5d3f2f51299aede5abf9687eJakub Hrozek
72ae534f5aef6d2e5d3f2f51299aede5abf9687eJakub Hrozek def new
72ae534f5aef6d2e5d3f2f51299aede5abf9687eJakub Hrozek @version = build_resource.versions.build
72ae534f5aef6d2e5d3f2f51299aede5abf9687eJakub Hrozek end
72ae534f5aef6d2e5d3f2f51299aede5abf9687eJakub Hrozek
72ae534f5aef6d2e5d3f2f51299aede5abf9687eJakub Hrozek def create
72ae534f5aef6d2e5d3f2f51299aede5abf9687eJakub Hrozek @version = build_resource.versions.first
72ae534f5aef6d2e5d3f2f51299aede5abf9687eJakub Hrozek @version.source = Ontology.find(params[:link][:source_id]).versions.current
72ae534f5aef6d2e5d3f2f51299aede5abf9687eJakub Hrozek @version.target = Ontology.find(params[:link][:target_id]).versions.current
72ae534f5aef6d2e5d3f2f51299aede5abf9687eJakub Hrozek super
72ae534f5aef6d2e5d3f2f51299aede5abf9687eJakub Hrozek end
72ae534f5aef6d2e5d3f2f51299aede5abf9687eJakub Hrozek
72ae534f5aef6d2e5d3f2f51299aede5abf9687eJakub Hrozek def update_version
72ae534f5aef6d2e5d3f2f51299aede5abf9687eJakub Hrozek @version = resource.versions.current.dup
72ae534f5aef6d2e5d3f2f51299aede5abf9687eJakub Hrozek @version.version_number = @version.version_number + 1
72ae534f5aef6d2e5d3f2f51299aede5abf9687eJakub Hrozek @version.save
72ae534f5aef6d2e5d3f2f51299aede5abf9687eJakub Hrozek redirect_to edit_link_link_version_path(resource, @version)
72ae534f5aef6d2e5d3f2f51299aede5abf9687eJakub Hrozek end
72ae534f5aef6d2e5d3f2f51299aede5abf9687eJakub Hrozek
72ae534f5aef6d2e5d3f2f51299aede5abf9687eJakub Hrozek
72ae534f5aef6d2e5d3f2f51299aede5abf9687eJakub Hrozek private
72ae534f5aef6d2e5d3f2f51299aede5abf9687eJakub Hrozek
72ae534f5aef6d2e5d3f2f51299aede5abf9687eJakub Hrozek def collection
72ae534f5aef6d2e5d3f2f51299aede5abf9687eJakub Hrozek if params[:ontology_id]
72ae534f5aef6d2e5d3f2f51299aede5abf9687eJakub Hrozek onto = params[:ontology_id]
72ae534f5aef6d2e5d3f2f51299aede5abf9687eJakub Hrozek @links = Link.where("ontology_id =#{onto} OR source_id = #{onto} OR target_id = #{onto}")
72ae534f5aef6d2e5d3f2f51299aede5abf9687eJakub Hrozek collection = Kaminari.paginate_array(Link.where("ontology_id =#{onto} OR source_id = #{onto} OR target_id = #{onto}").
72ae534f5aef6d2e5d3f2f51299aede5abf9687eJakub Hrozek select { |link| can?(:show, link.source.repository) && can?(:show, link.target.repository) }).page(params[:page])
72ae534f5aef6d2e5d3f2f51299aede5abf9687eJakub Hrozek else
72ae534f5aef6d2e5d3f2f51299aede5abf9687eJakub Hrozek Kaminari.paginate_array(super.select { |link| can?(:show, link.source.repository) && can?(:show, link.target.repository) }).page(params[:page])
72ae534f5aef6d2e5d3f2f51299aede5abf9687eJakub Hrozek end
72ae534f5aef6d2e5d3f2f51299aede5abf9687eJakub Hrozek end
72ae534f5aef6d2e5d3f2f51299aede5abf9687eJakub Hrozek
72ae534f5aef6d2e5d3f2f51299aede5abf9687eJakub Hrozek def build_resource
72ae534f5aef6d2e5d3f2f51299aede5abf9687eJakub Hrozek @link ||= Link.new params[:link]
72ae534f5aef6d2e5d3f2f51299aede5abf9687eJakub Hrozek end
72ae534f5aef6d2e5d3f2f51299aede5abf9687eJakub Hrozek
72ae534f5aef6d2e5d3f2f51299aede5abf9687eJakub Hrozek def check_read_permissions
72ae534f5aef6d2e5d3f2f51299aede5abf9687eJakub Hrozek unless params[:action] == 'index'
72ae534f5aef6d2e5d3f2f51299aede5abf9687eJakub Hrozek if resource.source
72ae534f5aef6d2e5d3f2f51299aede5abf9687eJakub Hrozek authorize! :show, resource.source.repository
72ae534f5aef6d2e5d3f2f51299aede5abf9687eJakub Hrozek end
72ae534f5aef6d2e5d3f2f51299aede5abf9687eJakub Hrozek if resource.target
72ae534f5aef6d2e5d3f2f51299aede5abf9687eJakub Hrozek authorize! :show, resource.target.repository
72ae534f5aef6d2e5d3f2f51299aede5abf9687eJakub Hrozek end
72ae534f5aef6d2e5d3f2f51299aede5abf9687eJakub Hrozek end
72ae534f5aef6d2e5d3f2f51299aede5abf9687eJakub Hrozek end
72ae534f5aef6d2e5d3f2f51299aede5abf9687eJakub Hrozekend
72ae534f5aef6d2e5d3f2f51299aede5abf9687eJakub Hrozek