Project

General

Profile

[plugin-dev] question which model to extend for issue_category filtering

Added by Tobias Fischer over 9 years ago

hey there,

I'm trying to develop a plugin which adds a new column "archived" to the issue_categories table (see #16188).

so far wtih very poor ruby/rails knowledge, I managed to add a new column to the table, wrote a migration and patched the IssueCategoriesControll and I'm now able to archive / unarchive issue categories in the project settings tab (in an overridden view).

next thing I want to do is to filter the issue_categories dropdown in the issues/_attributes.html.erb view based on whether the category is archived or not. well, the goal should be to display only the categroies which aren't archived ;-)

this is the part of the view which renders the issue_categories dropdown:

<% if @issue.safe_attribute?('category_id') && @issue.project.issue_categories.any? %>
<p><%= f.select :category_id, (@issue.project.issue_categories.collect {|c| [c.name, c.id]}), :include_blank => true, :required => @issue.required_attribute?('category_id') %>
<%= link_to(image_tag('add.png', :style => 'vertical-align: middle;'),
            new_project_issue_category_path(@issue.project),
            :remote => true,
            :method => 'get',
            :title => l(:label_issue_category_new),
            :tabindex => 200) if User.current.allowed_to?(:manage_categories, @issue.project) %></p>
<% end %>

The second line is the important one.

Categories are selected based on the Project model, as far as I understood.

class Project < ActiveRecord::Base
  ...
  has_many :issue_categories, :dependent => :delete_all, :order => "#{IssueCategory.table_name}.name" 
So here come my questions:
  1. is there a way to "filter" the output of (@issue.project.issue_categories.collect {|c| [c.name, c.id]}) depending on whether the "archived" column is set to "false"?
  2. or do I have to override the view with my own and add a new method to output the active categories?
    • which Model or Controller do I have to patch then? Issue or Project?
    • and is it then possible to call the functions archived? / unarchived? which I managed to patch in the IssueCategoriesController?
    • or do I need new methods?

any help appreciated!
thanks :)

cheers,
toby


Replies (1)

RE: [plugin-dev] question which model to extend for issue_category filtering - Added by Tobias Fischer over 9 years ago

well, writing down a problem sometimes helps a lot.

So I figured out within minutes, that a cloned view of issues/_attributes and the following changed code works well:

<p><%= f.select :category_id, (@issue.project.issue_categories.reject {|c| c.archived }.collect {|c| [c.name, c.id] }), :include_blank => true, :required => @issue.required_attribute?('category_id') %>

however, I don't like the idea of overriding the fiew as this view in specific might change very often as it's fundamental core functionality.

is there any other method which answers my questions above – like patching/extending some redmine core classes without overriding the view?

    (1-1/1)