Skip to content
Browse files

Actual code, release 1.0.0

  • Loading branch information...
1 parent 2e551d5 commit b6036786ac6475e18250b828f56f84e49df1802d @thegcat committed Aug 7, 2011
View
13 README.rdoc
@@ -1,6 +1,10 @@
= ChiliProject Ensure Project Hierarchy Plugin
-This plugin ensures subproject identifiers are prefixes with their parent project's identifier.
+This plugin ensures subproject identifiers are prefixes with their parent
+project's identifier.
+
+WARNING: Admins are still allowed to set any identifier regardless of the parent
+project.
== Requirements
@@ -37,6 +41,13 @@ You should be able to execute the tests with
If these instructions are insufficient, please open a ticket in the GitHub issue
tracker with information about where you are stuck.
+== Known issues
+
+* The validation should be moved from the projects controller to the model
+* The parent project drop-down on the project settings page contains all
+projects the user can set the parent project to, though as the project
+identifier is frozen, only the project's parents satisfy the identifier
+hierarchy condition.
== License
View
6 config/locales/de.yml
@@ -0,0 +1,6 @@
+# German strings go here for Rails i18n
+de:
+ activerecord:
+ errors:
+ messages:
+ prefix_invalid: "muss den Präfix \"%{prefix}\" haben"
View
6 config/locales/en.yml
@@ -0,0 +1,6 @@
+# English strings go here for Rails i18n
+en:
+ activerecord:
+ errors:
+ messages:
+ prefix_invalid: "must start with \"%{prefix}\""
View
10 init.rb
@@ -4,8 +4,12 @@
name 'Ensure Project Hierarchy plugin'
author 'Felix Schäfer'
description 'This plugin ensures subproject identifiers are prefixes with their parent project\'s identifier.'
- version '0.0.1'
+ version '1.0.0'
url 'https://github.com/thegcat/chiliproject_ensure_project_hierarchy'
-
- ::Project::IDENTIFIER_SEPARATOR = "-"
end
+
+require 'dispatcher'
+
+Dispatcher.to_prepare do
+ require_dependency 'chiliproject_ensure_project_hierarchy/projects_controller_patch'
+end
View
52 lib/chiliproject_ensure_project_hierarchy/projects_controller_patch.rb
@@ -0,0 +1,52 @@
+::Project::IDENTIFIER_SEPARATOR = "-"
+
+module Plugin
+ module EnsureProjectHierarchy
+ module ProjectsController
+ module ClassMethods
+
+ end
+
+ module InstanceMethods
+ private
+
+ def new_with_default_identifier_for_new_subprojects
+ new_without_default_identifier_for_new_subprojects
+ if params[:parent_id]
+ parent_project = Project.find params[:parent_id]
+ puts "Do I get here? And who is the parent_project? " + parent_project.inspect
+ @project.identifier = "#{parent_project.identifier}#{::Project::IDENTIFIER_SEPARATOR}"
+ puts "wheeeee! " + @project.inspect
+ end
+ end
+
+ def validate_parent_id_with_identifier_hierarchy
+ return true if User.current.admin?
+ return false unless validate_parent_id_without_identifier_hierarchy
+ parent_id = params[:project] && params[:project][:parent_id]
+ if parent_id || @project.new_record?
+ parent = parent_id.blank? ? nil : Project.find_by_id(parent_id.to_i)
+ if parent && !@project.identifier.match("#{Regexp.escape(parent.identifier + ::Project::IDENTIFIER_SEPARATOR)}.+")
+ @project.errors.add :identifier, :prefix_invalid, :prefix => "#{parent.identifier}-"
+ return false
+ end
+ end
+ true
+ end
+ end
+
+ def self.included(receiver)
+ receiver.extend ClassMethods
+ receiver.send :include, InstanceMethods
+ receiver.class_eval do
+ unloadable
+
+ alias_method_chain :new, :default_identifier_for_new_subprojects
+ alias_method_chain :validate_parent_id, :identifier_hierarchy
+ end
+ end
+ end
+ end
+end
+
+ProjectsController.send(:include, Plugin::EnsureProjectHierarchy::ProjectsController)
View
15 spec/controllers/projects_controller_spec.rb
@@ -5,14 +5,15 @@
let(:parent_project) {mock_model Project, :identifier => "something-witty"}
describe "when I navigate to the new project page after having clicked the 'New subproject' link" do
- let(:do_action) {get :new, :parent_id => parent_project}
+ let(:do_action) {get :new, :parent_id => parent_project.identifier}
before do
# not testing the other stuff
IssueCustomField.stub!(:find)
Tracker.stub!(:all)
Project.stub!(:new).and_return project
+ Project.stub!(:find).with(parent_project.identifier).and_return parent_project
@controller.stub!(:authorize_global).and_return true
end
@@ -26,28 +27,32 @@
describe "when I try to save a project with a parent project" do
describe "when the project to save is an existing project" do
- let(:do_action) {put :create, :project_id => "abc", :project => {:name => "abc", :parent_id => parent_project.id, :identifier => identifier}}
+ let(:do_action) {put :update, :id => "abc", :project => {:parent_id => parent_project.id, :identifier => identifier}}
let(:project) {mock_model Project, :name => "abc", :parent_id => parent_project.id, :identifier => identifier}
before do
+ Project.stub!(:find).with("abc").and_return project
+ Project.stub!(:find_by_id).with(parent_project.id).and_return parent_project
project.stub!(:safe_attributes=)
+ project.stub!(:allowed_parents).and_return [parent_project]
project.stub!(:save).and_return true
project.stub!(:set_allowed_parent!)
@controller.stub!(:authorize).and_return true
+ @controller.stub!(:load_project_settings)
end
describe "and the submitted identifier is correct" do
- let(:identifier) {"#{parent_project.identifier}#{::Project::IDENTIFIER_SEPARATOR}"}
+ let(:identifier) {"#{parent_project.identifier}#{::Project::IDENTIFIER_SEPARATOR}abc"}
it "then the creation should be successful" do
do_action
- response.should be_success
+ response.should be_redirect
end
end
describe "and the submitted identifier is incorrect" do
let(:identifier) {""}
- let(:errors) {mock ActiveRecord::Errors}
+ let(:errors) {mock "ActiveRecord::Errors"}
before do
project.stub!(:errors).and_return errors

0 comments on commit b603678

Please sign in to comment.
Something went wrong with that request. Please try again.