Skip to content

Commit

Permalink
Actual code, release 1.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
thegcat committed Aug 6, 2011
1 parent 2e551d5 commit b603678
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 9 deletions.
13 changes: 12 additions & 1 deletion 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

Expand Down Expand Up @@ -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

Expand Down
6 changes: 6 additions & 0 deletions 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"
6 changes: 6 additions & 0 deletions 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}\""
10 changes: 7 additions & 3 deletions init.rb
Expand Up @@ -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
@@ -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)
15 changes: 10 additions & 5 deletions spec/controllers/projects_controller_spec.rb
Expand Up @@ -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

Expand All @@ -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
Expand Down

0 comments on commit b603678

Please sign in to comment.