myexperiment-hackers
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[myexperiment-hackers] [2556] branches/neiss: added pack relationships


From: noreply
Subject: [myexperiment-hackers] [2556] branches/neiss: added pack relationships
Date: Tue, 1 Feb 2011 11:31:07 -0500 (EST)

Revision
2556
Author
dgc
Date
2011-02-01 11:31:07 -0500 (Tue, 01 Feb 2011)

Log Message

added pack relationships

Modified Paths

Added Paths

Diff

Added: branches/neiss/app/controllers/relationships_controller.rb (0 => 2556)


--- branches/neiss/app/controllers/relationships_controller.rb	                        (rev 0)
+++ branches/neiss/app/controllers/relationships_controller.rb	2011-02-01 16:31:07 UTC (rev 2556)
@@ -0,0 +1,88 @@
+# myExperiment: app/controllers/relationships_controller.rb
+#
+# Copyright (c) 2010 University of Manchester and the University of Southampton.
+# See license.txt for details.
+
+class RelationshipsController < ApplicationController
+  
+  helper PacksHelper
+
+  before_filter :find_resource_context
+  before_filter :find_resource, :except => [ :edit_relationships, :create ]
+
+  # GET /:context_type/:context_id/edit_relationships
+  def edit_relationships
+
+    @relationships = Relationship.find(:all, :conditions => ['pack_id = ?', @context.id])
+
+    @concepts = Vocabulary.find(:all).map do |v| v.concepts end.flatten
+
+    @pack_entries = @context.contributable_entries + @context.remote_entries
+
+    @pack_entry_select_options = @pack_entries.map do |pe|
+      if pe.class == PackContributableEntry
+        [pe.contributable.label, "contributable:#{pe.id}"]
+      else
+        [pe.title, "remote:#{pe.id}"]
+      end
+    end
+  end
+
+  # POST /:context_type/:context_id/relationships
+  def create 
+
+    subject = @context.find_pack_item(params[:subject])
+    objekt  = @context.find_pack_item(params[:objekt])
+
+    prefix, term = params[:predicate].split(":")
+
+    label = Label.find(:first, :conditions =>
+        ['label_type = ? AND vocabulary_id = ? AND text = ?',
+           'preferred',
+           Vocabulary.find_by_prefix(prefix).id,
+           term])
+
+    raise("Invalid form data") if subject.nil? || objekt.nil? || label.nil?
+
+    @relationship = Relationship.new(:pack => @context, :concept => label.concept)
+
+    @relationship.subject = subject
+    @relationship.objekt  = objekt
+
+    @relationship.save
+
+    redirect_to(:action ="" :edit_relationships)
+  end
+  
+  # DELETE /:context_type/:context_id/relationships/:id
+  def destroy
+
+   if Authorization.is_authorized?('destroy', nil, @relationship, current_user)
+      @relationship.destroy
+    end
+    
+    render :partial => "relationships/relationships",
+           :locals  => { :relationships => @context.relationships }
+  end
+
+  private
+
+  def find_resource
+
+    @context      = extract_resource_context(params)
+    @relationship = Relationship.find_by_id(params[:id])
+
+    return false if @relationship.nil? || @context.nil? || @relationship.pack != @context
+    return false if Authorization.is_authorized?('view', nil, @context, current_user) == false
+  end
+
+  def find_resource_context
+
+    @context = extract_resource_context(params)
+
+    return false if @context.nil?
+    return false if Authorization.is_authorized?('view', nil, @context, current_user) == false
+  end
+
+end
+

Modified: branches/neiss/app/helpers/packs_helper.rb (2555 => 2556)


--- branches/neiss/app/helpers/packs_helper.rb	2011-02-01 16:29:16 UTC (rev 2555)
+++ branches/neiss/app/helpers/packs_helper.rb	2011-02-01 16:31:07 UTC (rev 2556)
@@ -78,4 +78,18 @@
     
     return tags
   end
+
+  def pack_entry_link(entry)
+
+    case entry.class.name
+    when "PackContributableEntry"
+      text = entry.contributable.label
+      href = ""
+    when "PackRemoteEntry"
+      text = entry.title
+      href = ""
+    end
+
+    link_to(text, href)
+  end
 end

Modified: branches/neiss/app/models/pack.rb (2555 => 2556)


--- branches/neiss/app/models/pack.rb	2011-02-01 16:29:16 UTC (rev 2555)
+++ branches/neiss/app/models/pack.rb	2011-02-01 16:31:07 UTC (rev 2556)
@@ -22,6 +22,8 @@
   acts_as_rateable
   acts_as_taggable
 
+  acts_as_structured_data
+
   validates_presence_of :title
   
   format_attribute :description
@@ -610,6 +612,36 @@
     contributable_entries.map do |e| e.contributable end
   end
 
+  # This function takes a string, such as 'contributable:4' (which would return
+  # a PackContributableEntry with the id of 4) or 'remote:8' (which would
+  # return a PackRemoteEntry with an id of 8) and returns the appropriate pack
+  # item if this pack contains that item.
+
+  def find_pack_item(str)
+
+    thing_type, thing_id = str.split(":")
+
+    case thing_type
+      when 'contributable'
+        ob = PackContributableEntry.find(:first,
+            :conditions => ['id = ? AND pack_id = ?', thing_id, id])
+
+      when 'remote'
+        ob = PackRemoteEntry.find(:first,
+            :conditions => ['id = ? AND pack_id = ?', thing_id, id])
+    end
+  end
+
+  # This method determines which pack relationships refer to contributables
+  # that are not included as pack entries in this pack.  Such relationships
+  # might occur when deleting entries from a pack.
+
+  def dangling_relationships
+    relationships.select do |relationship|
+      relationship.subject.nil? || relationship.objekt.nil?
+    end
+  end
+ 
   protected
   
   # produces html string containing the required messaged, enclosed within left-padded P tag, belonging to 'none_text' class

Added: branches/neiss/app/models/relationship.rb (0 => 2556)


--- branches/neiss/app/models/relationship.rb	                        (rev 0)
+++ branches/neiss/app/models/relationship.rb	2011-02-01 16:31:07 UTC (rev 2556)
@@ -0,0 +1,13 @@
+# myExperiment: app/models/relationship.rb
+#
+# Copyright (c) 2010 University of Manchester and the University of Southampton.
+# See license.txt for details.
+
+class Relationship < ActiveRecord::Base
+
+  acts_as_structured_data
+
+  validates_uniqueness_of :concept_id, :scope => [:subject_id, :objekt_id]
+
+end
+

Modified: branches/neiss/app/views/packs/show.rhtml (2555 => 2556)


--- branches/neiss/app/views/packs/show.rhtml	2011-02-01 16:29:16 UTC (rev 2555)
+++ branches/neiss/app/views/packs/show.rhtml	2011-02-01 16:31:07 UTC (rev 2556)
@@ -81,8 +81,24 @@
 			</h4>
 			
 			<%= render :partial => "items", :locals => { :pack => @pack, :authorised_to_edit => @authorised_to_edit } -%>
+
+			<br/><br/>
+			<h4>
+				<%= info_icon_with_tooltip("This section shows all the relationships between the items in this pack.") -%>
+				Relationships <span class="count_text">(<%= @pack.relationships.length -%>)</span>
+			</h4>
+
+			<%= render :partial => "relationships/relationships", :locals => { :relationships => @pack.relationships } -%>
 			
+      <% if @authorised_to_edit %>
+        <br />
+        <ul class="sectionIcons">
+          <li><%= icon('manage', edit_relationships_pack_relationships_path(@pack), nil, nil, 'Edit Relationships') -%></li>
+        </ul>
+      <% end %>
+
 			<br/><br/>
+
 			<h3>
 				<%= info_icon_with_tooltip("This section provides a link to download all items in this pack as a single .zip archive") %>
 				Download

Added: branches/neiss/app/views/relationships/_breadcrumbs.rhtml ( => )


Added: branches/neiss/app/views/relationships/_relationships.rhtml
===================================================================
--- branches/neiss/app/views/relationships/_relationships.rhtml	                        (rev 0)
+++ branches/neiss/app/views/relationships/_relationships.rhtml	2011-02-01 16:31:07 UTC (rev 2556)
@@ -0,0 +1,25 @@
+<% if relationships.empty? %>
+
+  <p><i>There are no relationships in this pack.</i></p>
+
+<% else %>
+
+  <ol>
+    <% relationships.each do |relationship| %>
+      <li>
+        <%= pack_entry_link(relationship.subject) -%>
+        <%=h relationship.concept.preferred_label.text.underscore.humanize.downcase -%>
+        <%= pack_entry_link(relationship.objekt) -%>
+
+        <%= link_to_remote( "delete_relationship",
+            :update => "relationshipsElement", 
+            :url ="" pack_relationship_path(relationship.pack, relationship.id),
+            :method => :delete,
+            :complete => "new Effect.Highlight('relationshipsElement', { duration: 1.5 });",
+            :confirm => "Are you sure you want to delete this relationship?" ) %>
+
+      </li>
+    <% end %>
+</ol>
+
+<% end %>

Added: branches/neiss/app/views/relationships/_subnav.rhtml ( => )


Added: branches/neiss/app/views/relationships/edit_relationships.rhtml
===================================================================
--- branches/neiss/app/views/relationships/edit_relationships.rhtml	                        (rev 0)
+++ branches/neiss/app/views/relationships/edit_relationships.rhtml	2011-02-01 16:31:07 UTC (rev 2556)
@@ -0,0 +1,40 @@
+<% t "Edit relationships" %>
+
+<h1>Edit relationships for: "<%=h @context.title -%>".</h1>
+
+<center>
+	<%= error_messages_for :relationship %>
+</center>
+
+<h2>Add new relationship</h2>
+
+<% if @pack_entry_select_options.length < 2 %>
+
+  <p><i>There must be at least two entries in a pack before you can add relationships.</i></p>
+
+<% else %>
+
+  <div>
+
+    <% form_tag("#{rest_resource_uri(@context)}/relationships") do -%>
+
+      <%= select_tag("subject", options_for_select(@pack_entry_select_options)) -%>
+
+      <%= select_tag("predicate", options_for_select(@concepts.map do |c| "#{c.vocabulary.prefix}:#{c.preferred_labels.first.text}" end)) -%>
+
+      <%= select_tag("objekt", options_for_select(@pack_entry_select_options)) -%>
+     
+      <input type="submit" value="Add relationship"/>
+
+    <% end %>
+
+  </div>
+
+<% end %>
+
+<h2>Relationships</h2>
+
+<div id="relationshipsElement">
+  <%= render(:partial => "relationships", :locals => { :relationships => @relationships }) -%>
+</div>
+

Modified: branches/neiss/config/routes.rb (2555 => 2556)


--- branches/neiss/config/routes.rb	2011-02-01 16:29:16 UTC (rev 2555)
+++ branches/neiss/config/routes.rb	2011-02-01 16:31:07 UTC (rev 2556)
@@ -94,9 +94,9 @@
                  :resolve_link => :post,
                  :items => :get } do |pack|
     pack.resources :comments, :collection => { :timeline => :get }
+    pack.resources :relationships, :collection => { :edit_relationships => :get }
   end
     
-
   # workflows (downloadable)
   map.resources :workflows, 
     :collection => { :search => :get }, 

Added: branches/neiss/config/schema.d/packs.xml (0 => 2556)


--- branches/neiss/config/schema.d/packs.xml	                        (rev 0)
+++ branches/neiss/config/schema.d/packs.xml	2011-02-01 16:31:07 UTC (rev 2556)
@@ -0,0 +1,62 @@
+<?xml version="1.0"?>
+<schema>
+
+  <table name="packs">
+
+    <column type="integer"  name="contributor_id"/>
+    <column type="string"   name="contributor_type"/>
+    <column type="string"   name="title"/>
+    <column type="text"     name="description"/>
+    <column type="text"     name="description_html"/>
+    <column type="datetime" name="created_at"/>
+    <column type="datetime" name="updated_at"/>
+
+    <has-many target="relationships" dependent="destroy"/>
+
+  </table>
+
+  <table name="pack_contributable_entries">
+
+    <column type="integer"  name="pack_id"/>
+    <column type="integer"  name="contributable_id"/>
+    <column type="integer"  name="contributable_version"/>
+    <column type="string"   name="contributable_type"/>
+    <column type="text"     name="comment"/>
+    <column type="integer"  name="user_id"/>
+    <column type="datetime" name="created_at"/>
+    <column type="datetime" name="updated_at"/>
+
+  </table>
+
+  <table name="pack_remote_entries">
+
+    <column type="integer"  name="pack_id"/>
+    <column type="string"   name="title"/>
+    <column type="string"   name="uri"/>
+    <column type="string"   name="alternate_uri"/>
+    <column type="text"     name="comment"/>
+    <column type="integer"  name="user_id"/>
+    <column type="datetime" name="created_at"/>
+    <column type="datetime" name="updated_at"/>
+
+  </table>
+
+  <table name="relationships">
+
+    <column type="integer" name="pack_id"/>
+    <column type="integer" name="concept_id"/>
+    <column type="string"  name="subject_type"/>
+    <column type="integer" name="subject_id"/>
+    <column type="string"  name="objekt_type"/>
+    <column type="integer" name="objekt_id"/>
+
+    <belongs-to target="subjects" polymorphic="true"/>
+    <belongs-to target="objekts"  polymorphic="true"/>
+
+    <belongs-to target="packs"/>
+    <belongs-to target="concepts"/>
+
+  </table>
+ 
+</schema>
+

Modified: branches/neiss/lib/authorization.rb (2555 => 2556)


--- branches/neiss/lib/authorization.rb	2011-02-01 16:29:16 UTC (rev 2555)
+++ branches/neiss/lib/authorization.rb	2011-02-01 16:31:07 UTC (rev 2556)
@@ -498,7 +498,7 @@
     case action_name
       when 'show', 'index', 'view', 'search', 'favourite', 'favourite_delete', 'comment', 'comment_delete', 'comments', 'comments_timeline', 'rate', 'tag',  'items', 'statistics', 'curation', 'tag_suggestions', 'extra_metadata', 'read', 'verify', 'explore'
         action = ''
-      when 'edit', 'new', 'create', 'update', 'new_version', 'create_version', 'destroy_version', 'edit_version', 'update_version', 'new_item', 'create_item', 'edit_item', 'update_item', 'quick_add', 'resolve_link', 'process_tag_suggestions', 'process_extra_metadata'
+      when 'edit', 'new', 'create', 'update', 'new_version', 'create_version', 'destroy_version', 'edit_version', 'update_version', 'new_item', 'create_item', 'edit_item', 'update_item', 'quick_add', 'resolve_link', 'process_tag_suggestions', 'process_extra_metadata', 'edit_relationships'
         action = ''
       when 'download', 'named_download', 'launch', 'submit_job', 'save_inputs', 'refresh_status', 'rerun', 'refresh_outputs', 'render_output', 'outputs_xml', 'outputs_package'
         action = ''

reply via email to

[Prev in Thread] Current Thread [Next in Thread]