Skip to content

Commit

Permalink
Linked articles
Browse files Browse the repository at this point in the history
  • Loading branch information
Mariusz Michalak committed Apr 22, 2015
1 parent 6e80f6c commit 867d4da
Show file tree
Hide file tree
Showing 15 changed files with 125 additions and 12 deletions.
10 changes: 10 additions & 0 deletions app/controllers/tickets_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,15 @@ class TicketsController < ApplicationController
# this is needed for brimir integration in other sites
before_filter :allow_cors, only: [:create, :new]

def edit_articles
@articles = Article.all
@linked_articles_ids = @ticket.articles.pluck(:id)

respond_to do |format|
format.html { render :layout => !request.xhr? }
end
end

def ping
@ticket = Ticket.find(params[:id])
authorize! :read, @ticket
Expand Down Expand Up @@ -231,6 +240,7 @@ def ticket_params
:deadline,
:group_id,
:labels_list,
articles_ids: [],
attachments_attributes: [
:file
])
Expand Down
2 changes: 2 additions & 0 deletions app/models/ability.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ def agent(user)
can :manage, Reply, ticket: { assignee_id: user.id }
can :manage, Reply, ticket: { group_id: user.group_ids }

can :manage, [Article, TicketArticle]

can [:read, :create], Label
can :manage, ::Template, user_id: user.id
can :read, ::Template, public: true
Expand Down
7 changes: 6 additions & 1 deletion app/models/article.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
class Article < ActiveRecord::Base

validates_presence_of :title, :description, :body

attr_accessor :labels_list

after_save :update_labels
after_initialize :set_labels_list

has_many :labelings, as: :labelable, dependent: :destroy
has_many :labels, through: :labelings
after_initialize :set_labels_list
has_many :ticket_articles
has_many :tickets, through: :ticket_articles

scope :search, ->(term) {
if !term.nil?
Expand Down
11 changes: 11 additions & 0 deletions app/models/ticket.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ class Ticket < ActiveRecord::Base

has_many :status_changes, dependent: :destroy

has_many :ticket_articles
has_many :articles, through: :ticket_articles

enum status: [:open, :closed, :deleted, :waiting]
enum priority: [:unknown, :low, :medium, :high]

Expand All @@ -47,10 +50,12 @@ class Ticket < ActiveRecord::Base
after_create :create_labels
after_create :set_start_time
after_save :set_end_time
after_save :update_linked_articles

attr_accessor :consumed_days, :consumed_hours, :consumed_minutes
attr_accessor :sender
attr_accessor :labels_list
attr_accessor :articles_ids

def self.active_labels(status)
label_ids = where(status: Ticket.statuses[status])
Expand Down Expand Up @@ -163,6 +168,12 @@ def find_matching_articles

protected

def update_linked_articles
unless self.articles_ids.nil?
self.articles = Article.where(id: self.articles_ids)
end
end

def set_start_time
self.update_column(:start_time, self.created_at)
end
Expand Down
4 changes: 4 additions & 0 deletions app/models/ticket_article.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class TicketArticle < ActiveRecord::Base
belongs_to :ticket
belongs_to :article
end
4 changes: 4 additions & 0 deletions app/views/articles/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
<thead>
<tr>
<th><%= sortable 'title', Article.human_attribute_name(:title) %></th>
<th><%= t(:linked_tickets_count, default: 'Linked tickets count')%></th>
<th><%= Article.human_attribute_name(:labels) %></th>
<th><%= sortable 'description', Article.human_attribute_name(:description) %></th>
</tr>
Expand All @@ -34,6 +35,9 @@
<% @articles.each do |article| %>
<tr data-article-url="<%= article_url(article) %>">
<td><%= link_to article.title, article %></td>
<td>
<span class="secondary ba radius label ml" title="<%= article.tickets.size %>"><%= article.tickets.size %></span>
</td>
<td class="text-secondary table-nowrap">
<% article.labels.viewable_by(current_user).each do |label| %>
<%= render label %>
Expand Down
9 changes: 9 additions & 0 deletions app/views/articles/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,15 @@
</ul>
<% end %>
</dd>
<dt><%= t(:linked_tickets)%></dt>
<dd>
<% if @article.tickets.size == 0 %>
<%= t(:no_linked_articles)%>
<% end %>
<% @article.tickets.each do |ticket| %>
<div><%= link_to (ticket.subject || "<em>(#{t(:no_subject)})</em>".html_safe), ticket_path(ticket)%></div>
<% end %>
</dd>
</div>
</dl>
</div>
Expand Down
3 changes: 3 additions & 0 deletions app/views/tickets/_link_articles_form.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<div id="link-articles" class="reveal-modal small" data-reveal>
<a class="close-reveal-modal">&#215;</a>
</div>
16 changes: 16 additions & 0 deletions app/views/tickets/edit_articles.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<% if @articles.empty? %>
<%= t(:no_articles)%>
<% else %>
<%= form_for @ticket, method: :patch do |f| %>
<%= hidden_field_tag "ticket[articles_ids][]", nil %>
<table class="full">
<% @articles.each do |article| %>
<tr>
<td><input type="checkbox" name="ticket[articles_ids][]" value="<%= article.id %>" <%= @linked_articles_ids.include?(article.id) ? "checked='true'" : "" %>></td>
<td><%= article.title%></td>
</tr>
<% end %>
</table>
<%= f.submit t(:change), class: 'button' %>
<% end %>
<% end %>
35 changes: 25 additions & 10 deletions app/views/tickets/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -234,19 +234,33 @@
<% end %>
<% if can? :read, Article%>
<div class="content" id="panel3">
<% matching_articles = @ticket.find_matching_articles %>
<% if matching_articles.empty? %>
<%= t(:no_matching_articles, default: 'No matching articles')%>
<% else %>
<dt><%= t(:matching_articles, default: 'Matching articles')%></dt>
<% matching_articles.each do |matching_article|%>
<dd><%= link_to matching_article.title, article_path(matching_article), {target: '_BLANK'} %></dd>
<div>
<% linked_articles = @ticket.articles %>
<% if linked_articles.empty? %>
<%= t(:no_linked_articles, default: 'No linked articles')%>
<% else %>
<dt><%= t(:linked_articles, default: 'Linked articles')%></dt>
<% linked_articles.each do |linked_article|%>
<dd><%= link_to linked_article.title, article_path(linked_article), {target: '_BLANK'} %></dd>
<% end %>
<% end %>
<% end %>


<% if can? :create, TicketArticle %>
<div><a href="#" data-reveal-id="link-articles" class="tiny button radius" data-reveal-ajax="<%= edit_articles_ticket_path(@ticket)%>"><%= t(:manage)%></a></div>
<% end %>

<%= button_to t(:add), new_article_path, method: :get, class: 'tiny radius button mtm' %>
</div>
<div>
<% matching_articles = @ticket.find_matching_articles %>
<% if matching_articles.empty? %>
<%= t(:no_matching_articles, default: 'No matching articles')%>
<% else %>
<dt><%= t(:matching_articles, default: 'Matching articles')%></dt>
<% matching_articles.each do |matching_article|%>
<dd><%= link_to matching_article.title, article_path(matching_article), {target: '_BLANK'} %></dd>
<% end %>
<% end %>
</div>
</div>
<% end %>
</div>
Expand All @@ -261,3 +275,4 @@
<%= render 'change_sender_form', { ticket: @ticket } %>
<%= render 'change_group_form', { ticket: @ticket } %>
<%= render 'change_subject_form', { ticket: @ticket } %>
<%= render 'link_articles_form', { ticket: @ticket } %>
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
post :ping
put :closed
get :clone
get :edit_articles
end
end

Expand Down
10 changes: 10 additions & 0 deletions db/migrate/20150421192628_create_ticket_articles.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class CreateTicketArticles < ActiveRecord::Migration
def change
create_table :ticket_articles do |t|
t.integer :ticket_id
t.integer :article_id

t.timestamps null: false
end
end
end
9 changes: 8 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20150421091653) do
ActiveRecord::Schema.define(version: 20150421192628) do

create_table "articles", force: :cascade do |t|
t.string "title"
Expand Down Expand Up @@ -132,6 +132,13 @@
t.boolean "public", default: false
end

create_table "ticket_articles", force: :cascade do |t|
t.integer "ticket_id"
t.integer "article_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end

create_table "tickets", force: :cascade do |t|
t.string "subject"
t.text "content"
Expand Down
9 changes: 9 additions & 0 deletions test/fixtures/ticket_articles.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html

one:
ticket_id: 1
article_id: 1

two:
ticket_id: 1
article_id: 1
7 changes: 7 additions & 0 deletions test/models/ticket_article_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require 'test_helper'

class TicketArticleTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
end

0 comments on commit 867d4da

Please sign in to comment.