From: Lukasz Rekucki Date: Wed, 19 Aug 2009 14:19:43 +0000 (+0200) Subject: Opcje konfguracyjne pluginu. Walidacja przy wpisywaniu publikacji do zagadnienia... X-Git-Url: https://git.mdrn.pl/redakcja_redmine.git/commitdiff_plain/3cd0bfe5475b72e64c26b534e388f772878a408d?ds=inline;hp=378fe7c80dbc369bceb24e57e2cd81d3bd690ee9 Opcje konfguracyjne pluginu. Walidacja przy wpisywaniu publikacji do zagadnienia. Skrypt do zapelniania bazy publikacji z repozytorium. Refs #25 --- diff --git a/app/controllers/publications_controller.rb b/app/controllers/publications_controller.rb index c7d16e3..1220bea 100644 --- a/app/controllers/publications_controller.rb +++ b/app/controllers/publications_controller.rb @@ -1,14 +1,35 @@ - class PublicationsController < ApplicationController unloadable - @@source_file_field_id = 1 +# before_filter :authorize, :only => [:issues] + + def index + @publications = Publication.all + respond_to do |format| + format.html + format.xml { render :xml => @publications } + format.json { render :json => @publications } + end + end + + def refresh + regexp = Regexp.new(Setting.plugin_redmine_publications['pattern']) + Repository.all.each do |repo| + repo.entries.each do |entry| + match = entry.path.match(regexp) + if match + Publication.find_or_create_by_name(:name => match[1], + :source_file => entry.path, :repository_id => repo.id) + end + end + end + end def issues - logger.info "Searching for issues with document name = " + params[:pub] + "." - joins = "JOIN issue_publications ON (issues.id = issue_publications.issue_id) JOIN publications ON (issue_publications.publication_id = publications.id)" + @publication = Publication.find_by_name(params[:pub]) - conditions = ['publications.source_file = ? ', params[:pub] ] + joins = "JOIN issue_publications ON (issues.id = issue_publications.issue_id)" + conditions = ['issue_publications.publication_id = ? ', @publication.id ] @issues = Issue.all(:joins => joins, :conditions => conditions) respond_to do |format| format.html @@ -27,4 +48,10 @@ class PublicationsController < ApplicationController render :text => "" end + private + + def find_project + @project = Project.find(params[:project_id]) + end + end diff --git a/app/models/publication.rb b/app/models/publication.rb index b0e567d..1544402 100644 --- a/app/models/publication.rb +++ b/app/models/publication.rb @@ -1,3 +1,4 @@ class Publication < ActiveRecord::Base has_many :issues, :through => :issuepublications + belongs_to :repository end diff --git a/app/views/issues/_issue_form_pub.html.erb b/app/views/issues/_issue_form_pub.html.erb index 638c0f6..f0f7a1a 100644 --- a/app/views/issues/_issue_form_pub.html.erb +++ b/app/views/issues/_issue_form_pub.html.erb @@ -1,6 +1,5 @@
-

-' /> +

+' />

diff --git a/app/views/publications/index.html.erb b/app/views/publications/index.html.erb index 423d724..9a7e3ce 100644 --- a/app/views/publications/index.html.erb +++ b/app/views/publications/index.html.erb @@ -1,4 +1,6 @@ -

Microrest#related-issues

-<%= @issues %> - - +

Publikacje

+
    +<% @publications.each do |pub| %> +
  1. <%= pub.name %>
  2. +<% end %> +
diff --git a/app/views/publications/issues.html.erb b/app/views/publications/issues.html.erb new file mode 100644 index 0000000..f36a875 --- /dev/null +++ b/app/views/publications/issues.html.erb @@ -0,0 +1,4 @@ +

Issues for publication: <%= @publication.name %>

+<% @issues.each do |issue| %> +

<%= issue.subject %>

+<% end %> diff --git a/app/views/settings/_publications_settings.html.erb b/app/views/settings/_publications_settings.html.erb new file mode 100644 index 0000000..65e436e --- /dev/null +++ b/app/views/settings/_publications_settings.html.erb @@ -0,0 +1,13 @@ +<% @tracker = Tracker.find(settings['tracker']) %> + +
+

+ + <%= collection_select(:tracker, :id, Tracker.all, :id, :name, {}, {:name => "settings[tracker]"}) %> +

+ +

+ + <%= text_field_tag 'settings[pattern]', @settings['pattern'] %> +

+
diff --git a/db/migrate/003_publications_add_repo_id.rb b/db/migrate/003_publications_add_repo_id.rb new file mode 100644 index 0000000..8f232c9 --- /dev/null +++ b/db/migrate/003_publications_add_repo_id.rb @@ -0,0 +1,8 @@ +class PublicationsAddRepoId < ActiveRecord::Migration + def self.up + add_column :publications, :repository_id, :integer, :null => false, :default => 0 + end + + def self.down + end +end diff --git a/db/migrate/004_publications_add_name.rb b/db/migrate/004_publications_add_name.rb new file mode 100644 index 0000000..6b5bac6 --- /dev/null +++ b/db/migrate/004_publications_add_name.rb @@ -0,0 +1,8 @@ +class PublicationsAddName < ActiveRecord::Migration + def self.up + add_column :publications, :name, :string + end + + def self.down + end +end diff --git a/init.rb b/init.rb index 1df1e93..f2b5b59 100644 --- a/init.rb +++ b/init.rb @@ -3,7 +3,7 @@ require 'redmine' # Patches to the Redmine core. require 'dispatcher' -Dispatcher.to_prepare :redmine_kanban do +Dispatcher.to_prepare :redmine_publications do require_dependency 'issue' # Guards against including the module multiple time (like in tests) # and registering multiple callbacks @@ -14,12 +14,18 @@ end require_dependency 'issue_publication_hook' -Redmine::Plugin.register :redmine_publicatons do +Redmine::Plugin.register :redmine_publications do name 'Publications managment plugin' author 'Łukasz Rekucki' description 'This plugn helps manage issues related to a publication.' - version '0.0.3' + version '0.0.9' + # permission :view_issues_for_publication, :publications => :issues + + settings :partial => 'settings/publications_settings', + :default => { :tracker => '1', :pattern => '.*/[a-z_].xml'} + + menu :application_menu, :publications, { :controller => 'publications', :action => 'index' }, :caption => 'Publikacje' requires_redmine :version_or_higher => '0.8.0' diff --git a/lib/issue_publication_hook.rb b/lib/issue_publication_hook.rb index 4cfc3ba..9c97233 100644 --- a/lib/issue_publication_hook.rb +++ b/lib/issue_publication_hook.rb @@ -1,15 +1,21 @@ # Provides a link to the document on the platform class IssuesPublicationHook < Redmine::Hook::ViewListener + def view_issues_show_details_bottom(context) - result = "Source File(s):" - names = context[:issue].source_files.map {|name| "" + name + ""} - result << names.join(', ') - result + "" + # TODO: złapać wyjątek konwersji + if context[:issue].tracker_id == Setting.plugin_redmine_publications['tracker'].to_i + result = "Publication(s):" + names = context[:issue].publication_names {|name| "" + name + ""} + result << names.join(', ') + result << "" + end end def controller_issues_edit_before_save(context) - pub_field = context[:params][:issue_source_files] - context[:issue].source_files = pub_field + if context[:issue].tracker_id == Setting.plugin_redmine_publications['tracker'].to_i + pub_field = context[:params][:publications] + context[:issue].publication_names = pub_field.split(',').map { |n| n.strip } + end end render_on :view_issues_form_details_bottom, :partial => 'issue_form_pub' diff --git a/lib/redmine_publications/.issue_patch.rb.swp b/lib/redmine_publications/.issue_patch.rb.swp deleted file mode 100644 index 56af602..0000000 Binary files a/lib/redmine_publications/.issue_patch.rb.swp and /dev/null differ diff --git a/lib/redmine_publications/issue_patch.rb b/lib/redmine_publications/issue_patch.rb index dbcbfa8..f8a83c4 100644 --- a/lib/redmine_publications/issue_patch.rb +++ b/lib/redmine_publications/issue_patch.rb @@ -11,8 +11,8 @@ module RedminePublications base.class_eval do unloadable # Send unloadable so it will not be unloaded in development + validate :check_relations after_save :update_relations - # after_destroy :check_relations # Add visible to Redmine 0.8.x unless respond_to?(:visible) @@ -27,43 +27,57 @@ module RedminePublications end module InstanceMethods - def source_files - if not @source_files - @source_files = self.lookup_source_files.map { |pub| pub.source_file } - end - @source_files + + def publication_names + if not @pubnames + self.publications.map { |pub| pub.name } + else + @pubnames + end end - def source_files=(value) - @source_files = value + def publication_names=(value) + @pubnames = value.sort! end - def lookup_source_files + def publications Publication.all( :joins => "JOIN issue_publications ON (issue_publications.publication_id = publications.id)", :conditions => - [" issue_publications.issue_id = ? ", self.id] ) + ["issue_publications.issue_id = ? ", self.id] ) end - - def update_relations + + def check_relations + current_names = self.publication_names + non_existant = [] + + pubs = Publication.find_all_by_name(current_names).map {|i| i.name} + missing = current_names.select {|name| not pubs.include?name } + + if not missing.empty? + errors.add("publications", "Missing publication(s): " + missing.join(', ')) + end + end + + def update_relations self.reload - current_assocs = self.lookup_source_files - new_assocs_names = self.source_files.split(' ') + old = self.publications + current_names = self.publication_names # delete unused relations - deleted = current_assocs.select { |v| not (new_assocs_names.include?(v.source_file)) } - deleted.each { |pub| IssuePublication.delete_all( - :contitions => ["issue_publications.issue_id = ? AND issue_publicatons.publication_id = ?", - self.id, pub.id]) } + deleted = old.select { |v| not (current_names.include?(v.name)) } + deleted.each do |pub| + IssuePublication.delete_all(["issue_publications.issue_id = ? AND issue_publications.publication_id = ?", self.id, pub.id]) + end - new_assocs_names.each do |name| - pub = Publication.find_or_create_by_source_file(name) - IssuePublication.find_or_create_by_publication_id_and_issue_id(pub.id, self.id) + current_names.each do |name| + pub = Publication.find_by_name(name) + IssuePublication.find_or_create_by_publication_id_and_issue_id(pub.id, self.id) end return true - end + end end end diff --git a/routes.rb b/routes.rb index bdca2b8..d1cf057 100644 --- a/routes.rb +++ b/routes.rb @@ -1,10 +1,13 @@ -connect 'publications/:pub', +connect 'publications/:action', :controller => 'publications', - :action => 'redirect_to_platform' + :format => 'html' + +connect 'publications/:action.:format', + :controller => 'publications' -connect 'publications/:pub/:action', +connect 'publications/:action/:pub', :controller => 'publications', :format => 'html' -connect 'publications/:pub/:action.:format', +connect 'publications/:action/:pub.:format', :controller => 'publications'