Implemented first version of link feature.

This commit is contained in:
Martin Edenhofer 2012-08-24 00:08:27 +02:00
parent 33bea597bc
commit c031b06aef
7 changed files with 213 additions and 118 deletions

View file

@ -2,15 +2,18 @@ $ = jQuery.sub()
class App.LinkInfo extends App.Controller
events:
'focusout [data-type=edit]': 'update',
'click [data-type=add]': 'add',
'click [data-type=remove]': 'remove',
constructor: ->
super
@fetch()
fetch: () =>
# fetch item on demand
# get data
App.Com.ajax(
id: 'links_' + @object_id + '_' + @object,
id: 'links_' + @object.id + '_' + @object_type,
type: 'GET',
url: '/links',
data: {
@ -30,7 +33,7 @@ class App.LinkInfo extends App.Controller
@render()
)
render: () ->
render: () =>
list = {}
for item in @links
@ -40,10 +43,8 @@ class App.LinkInfo extends App.Controller
if item['link_object'] is 'Ticket'
list[ item['link_type'] ].push App.Ticket.find( item['link_object_value'] )
return if _.isEmpty( @links )
# insert data
@html App.view('link_info')(
@html App.view('link/info')(
links: list,
)
@ -52,11 +53,72 @@ class App.LinkInfo extends App.Controller
# user_id: user_id,
# )
update: (e) =>
remove: (e) =>
e.preventDefault()
link_type = $(e.target).data('link-type')
link_object_source = $(e.target).data('object')
link_object_source_value = $(e.target).data('object-id')
link_object_target = @object_type
link_object_target_value = @object.id
# update changes
note = $(e.target).parent().find('[data-type=edit]').val()
user = App.User.find(@user_id)
if user.note isnt note
user.updateAttributes( note: note )
@log 'update', e, note, user
# get data
App.Com.ajax(
id: 'links_remove_' + @object.id + '_' + @object_type,
type: 'GET',
url: '/links/remove',
data: {
link_type: link_type,
link_object_source: link_object_source,
link_object_source_value: link_object_source_value,
link_object_target: link_object_target,
link_object_target_value: link_object_target_value,
}
processData: true,
success: (data, status, xhr) =>
@fetch()
)
add: (e) =>
e.preventDefault()
new App.LinkAdd(
link_object: @object_type,
link_object_id: @object.id,
object: @object,
parent: @,
)
class App.LinkAdd extends App.ControllerModal
constructor: ->
super
@render()
render: =>
@html App.view('link/add')(
link_object: @link_object,
link_object_id: @link_object_id,
object: @object,
)
@modalShow()
submit: (e) =>
e.preventDefault()
params = @formParam(e.target)
@log 'link', params
# get data
App.Com.ajax(
id: 'links_add_' + @object.id + '_' + @object_type,
type: 'GET',
url: '/links/add',
data: {
link_type: params['link_type'],
link_object_source: 'Ticket',
link_object_source_value: @object.id,
link_object_target: 'Ticket',
link_object_target_number: params['ticket_number'],
}
processData: true,
success: (data, status, xhr) =>
@modalHide()
@parent.fetch()
)

View file

@ -0,0 +1,30 @@
<form class="form-horizontal">
<div class="modal-header">
<a href="#" class="close">&times;</a>
<h3><%- T('Link') %></h3>
</div>
<div class="modal-body">
<%- T('Link Ticket#') %>
<input type="text" name="ticket_number" value="" class="span2" required/>
<%- T('as') %>
<select name="link_type" class="span2" required>
<option value="">-</option>
<option value="normal"><%- T('normal') %></option>
<option value="child"><%- T('child') %></option>
<option value="parent"><%- T('parent') %></option>
</select>
<%- T('with') %>
<%- T(@link_object) %>
<%= @object.number %>.
<!--
<ul>
<li>Ticket#</li>
<li>Recent Views Tickets</li>
<li>Recent Customer Tickets</li>
</ul>
-->
</div>
<div class="modal-footer">
<button type="submit" class="btn btn-primary"><%- T('Submit') %></button>
</div>
</form>

View file

@ -0,0 +1,15 @@
<div class="well">
<h3><%- T('Linked Objects') %></h3>
<% for type of @links: %>
<strong><%- T( type ) %></strong>
<ul>
<% for item in @links[type]: %>
<li><a href="#ticket/zoom/<%= item.id %>" data-type="" title="<%= item.title %>">T:<%= item.number %> <%= item.title %></a> <a href="" data-object="Ticket" data-object-id="<%= item.id %>" data-link-type="<%= type %>" data-type="remove" class="icon-remove" title="<%- T('remove') %>"></a></li>
<% end %>
</ul>
<% end %>
<div><a href="" data-type="add" class="icon-plus" title="<%- T('add') %>"></a></div>
</div>

View file

@ -1,13 +0,0 @@
<div class="well">
<h3><%- T('Linked Objects') %></h3>
<% for type of @links: %>
<strong><%- T( type ) %></strong>
<ul>
<% for item in @links[type]: %>
<li><a href="#ticket/zoom/<%= item.id %>" data-type="" title="<%= item.title %>">T:<%= item.number %></a></li>
<% end %>
</ul>
<% end %>
</div>

View file

@ -40,12 +40,14 @@ class LinksController < ApplicationController
# POST /links/add
def add
# @template.created_by_id = current_user.id
# lookup object id
object_id = Ticket.where( :number => params[:link_object_target_number] ).first.id
link = Link.add(
:link_type => params[:link_type],
:link_object_source => params[:link_object_source],
:link_object_source_value => params[:link_object_source_value],
:link_object_target => params[:link_object_target],
:link_object_target_value => params[:link_object_target_value]
:link_object_target_value => object_id
)
if link
@ -55,14 +57,14 @@ class LinksController < ApplicationController
end
end
# DELETE /links/delete
def delete
@template = Template.find(params[:id])
# DELETE /links/remove
def remove
link = Link.remove(params)
if @template.update_attributes(params[:template])
render :json => @template, :status => :ok
if link
render :json => link, :status => :created
else
render :json => @template.errors, :status => :unprocessable_entity
render :json => link.errors, :status => :unprocessable_entity
end
end

View file

@ -1,15 +1,58 @@
class Link < ActiveRecord::Base
belongs_to :link_type, :class_name => 'Link::Type'
belongs_to :link_object, :class_name => 'Link::Object'
belongs_to :link_type, :class_name => 'Link::Type'
belongs_to :link_object, :class_name => 'Link::Object'
#before_create :check_object
#after_create :after_create, :cache_delete
#after_update :cache_delete
#after_destroy :cache_delete
@map = {
'normal' => 'normal',
'parent' => 'child',
'child' => 'parent',
}
=begin
Link.add(
Link.list(
:link_object => 'Ticket',
:link_object_value => 1
)
=end
def self.list(data)
linkobject = self.link_object_get( :name => data[:link_object] )
return if !linkobject
items = []
# get links for one site
list = Link.where(
'link_object_source_id = ? AND link_object_source_value = ?', linkobject.id, data[:link_object_value]
)
list.each { |item|
link = {}
link['link_type'] = @map[ Link::Type.find( item.link_type_id ).name ]
link['link_object'] = Link::Object.find( item.link_object_target_id ).name
link['link_object_value'] = item.link_object_target_value
items.push link
}
# get links for the other site
list = Link.where(
'link_object_target_id = ? AND link_object_target_value = ?', linkobject.id, data[:link_object_value]
)
list.each { |item|
link = {}
link['link_type'] = Link::Type.find( item.link_type_id ).name
link['link_object'] = Link::Object.find( item.link_object_source_id ).name
link['link_object_value'] = item.link_object_source_value
items.push link
}
return items
end
=begin
Link.add(
:link_type => 'normal',
:link_object_source => 'Ticket',
:link_object_source_value => 6,
@ -25,78 +68,8 @@ class Link < ActiveRecord::Base
:link_object_target_value => 1
)
Link.list(
:link_object => 'Ticket',
:link_object_value => 1
)
Link.get_links_for_source_object(
:link_object => 'Ticket',
:link_object_value => 1
)
Link.get_links_for_target_object(
:link_object => 'Ticket',
:link_object_value => 1,
)
Link.delete_link_by_source( :source_value => 1 )
Link.delete_link_by_target( :target_value => 1 )
Link.delete_all_links_by_value( :object_value => 1 )
=end
def self.list(data)
linkobject = self.link_object_get( :name => data[:link_object] )
return if !linkobject
list = Link.where(
'link_object_source_id = ? AND link_object_source_value = ?', linkobject.id, data[:link_object_value]
)
map = {
'normal' => 'normal',
'parent' => 'child',
'child' => 'parent',
}
items = []
list.each { |item|
link = {}
link['link_type'] = map[ Link::Type.find( item.link_type_id ).name ]
link['link_object'] = Link::Object.find( item.link_object_target_id ).name
link['link_object_value'] = item.link_object_target_value
items.push link
}
list = Link.where(
'link_object_target_id = ? AND link_object_target_value = ?', linkobject.id, data[:link_object_value]
)
list.each { |item|
link = {}
link['link_type'] = Link::Type.find( item.link_type_id ).name
link['link_object'] = Link::Object.find( item.link_object_source_id ).name
link['link_object_value'] = item.link_object_source_value
items.push link
}
return items
end
def self.get_links_for_source_object(data)
linkobject_id = self.get_linktype_by_name( :name => data[:link_object] )
if linkobject_id
Link.where( :link_object_source_id => linkobject_id, :link_object_source_value => data[:link_object_value] )
end
end
def self.get_links_for_target_object(data)
linkobject_id = self.get_linktype_by_name( :name => data[:link_object] )
if linkobject_id
Link.where( :link_object_target_id => linkobject_id, :link_object_target_value => data[:link_object_value] )
end
end
def self.add(data)
if data.has_key?(:link_type)
@ -120,16 +93,60 @@ class Link < ActiveRecord::Base
Link.create(data)
end
def self.delete_link_by_source(data)
Link.where( :link_object_source_value => data[:source_value] ).destroy_all
end
=begin
def self.delete_link_by_target(data)
Link.where( :link_object_target_value => data[:target_value] ).destroy_all
end
Link.remove(
:link_type => 'normal',
:link_object_source => 'Ticket',
:link_object_source_value => 6,
:link_object_target => 'Ticket',
:link_object_target_value => 31
)
def self.delete_all_links_by_value(data)
Link.where( ["link_object_source_value = ? or link_object_target_value = ?", data[:object_value], data[:object_value]] ).destroy_all
=end
def self.remove(data)
if data.has_key?(:link_object_source)
linkobject = self.link_object_get( :name => data[:link_object_source] )
data[:link_object_source_id] = linkobject.id
end
if data.has_key?(:link_object_target)
linkobject = self.link_object_get( :name => data[:link_object_target] )
data[:link_object_target_id] = linkobject.id
end
# from one site
if data.has_key?(:link_type)
linktype = self.link_type_get( :name => data[:link_type] )
data[:link_type_id] = linktype.id
end
links = Link.where(
:link_type_id => data[:link_type_id],
:link_object_source_id => data[:link_object_source_id],
:link_object_source_value => data[:link_object_source_value],
:link_object_target_id => data[:link_object_target_id],
:link_object_target_value => data[:link_object_target_value]
)
links.each { |link|
link.destroy
}
# from the other site
if data.has_key?(:link_type)
linktype = self.link_type_get( :name => @map[ data[:link_type] ] )
data[:link_type_id] = linktype.id
end
links = Link.where(
:link_type_id => data[:link_type_id],
:link_object_target_id => data[:link_object_source_id],
:link_object_target_value => data[:link_object_source_value],
:link_object_source_id => data[:link_object_target_id],
:link_object_source_value => data[:link_object_target_value]
)
links.each { |link|
link.destroy
}
end
private
@ -153,24 +170,6 @@ class Link < ActiveRecord::Base
return linkobject
end
def self.get_linktype_by_name(data)
linkid = Link::Object.where( :name => data[:name] ).first
end
#checks for a valid link type
def check_valid_link_type
Rails.logger.info "Logger Test"
puts "pre check link type"
end
def get_linkobject_by_key
puts "check for exisiting link"
end
#checks for an exisiting ling
def check_existing_link
puts "check for exisiting link"
end
end
class Link::Type < ActiveRecord::Base

View file

@ -24,7 +24,7 @@ module ExtraRoutes
# links
map.match '/links', :to => 'links#index'
map.match '/links/add', :to => 'links#add'
map.match '/links/delete', :to => 'links#delete'
map.match '/links/remove', :to => 'links#remove'
# overviews
map.resources :overviews