From 91914cb877497417dea72af676752f878f75bde4 Mon Sep 17 00:00:00 2001 From: Eric Davis Date: Tue, 26 Apr 2011 13:17:18 -0700 Subject: [PATCH] [#604] Add simple Liquid drops for Projects and Principals --- app/drops/principal_drop.rb | 3 +++ app/drops/project_drop.rb | 3 +++ app/models/principal.rb | 4 +++ app/models/project.rb | 9 +++++++ test/unit/principal_drop_test.rb | 15 ++++++++++++ test/unit/project_drop_test.rb | 42 ++++++++++++++++++++++++++++++++ 6 files changed, 76 insertions(+) create mode 100644 app/drops/principal_drop.rb create mode 100644 app/drops/project_drop.rb create mode 100644 test/unit/principal_drop_test.rb create mode 100644 test/unit/project_drop_test.rb diff --git a/app/drops/principal_drop.rb b/app/drops/principal_drop.rb new file mode 100644 index 00000000..24a94ea9 --- /dev/null +++ b/app/drops/principal_drop.rb @@ -0,0 +1,3 @@ +class PrincipalDrop < BaseDrop + allowed_methods :name +end diff --git a/app/drops/project_drop.rb b/app/drops/project_drop.rb new file mode 100644 index 00000000..f14a7799 --- /dev/null +++ b/app/drops/project_drop.rb @@ -0,0 +1,3 @@ +class ProjectDrop < BaseDrop + allowed_methods :name, :identifier +end diff --git a/app/models/principal.rb b/app/models/principal.rb index 00207f5e..3c390bb2 100644 --- a/app/models/principal.rb +++ b/app/models/principal.rb @@ -31,6 +31,10 @@ class Principal < ActiveRecord::Base before_create :set_default_empty_values + def to_liquid + PrincipalDrop.new(self) + end + def name(formatter = nil) to_s end diff --git a/app/models/project.rb b/app/models/project.rb index 5fe6a8b8..299d7017 100644 --- a/app/models/project.rb +++ b/app/models/project.rb @@ -83,6 +83,10 @@ class Project < ActiveRecord::Base named_scope :all_public, { :conditions => { :is_public => true } } named_scope :visible, lambda { { :conditions => Project.visible_by(User.current) } } + def to_liquid + ProjectDrop.new(self) + end + def initialize(attributes = nil) super @@ -131,6 +135,11 @@ class Project < ActiveRecord::Base end end + # Is the project visible to the current user + def visible? + User.current.allowed_to?(:view_project, self) + end + def self.allowed_to_condition(user, permission, options={}) base_statement = "#{Project.table_name}.status=#{Project::STATUS_ACTIVE}" if perm = Redmine::AccessControl.permission(permission) diff --git a/test/unit/principal_drop_test.rb b/test/unit/principal_drop_test.rb new file mode 100644 index 00000000..b19adfd0 --- /dev/null +++ b/test/unit/principal_drop_test.rb @@ -0,0 +1,15 @@ +require File.expand_path('../../test_helper', __FILE__) + +class PrincipalDropTest < ActiveSupport::TestCase + def setup + @principal = Principal.generate! + @drop = @principal.to_liquid + end + + + context "#name" do + should "return the name" do + assert_equal @principal.name, @drop.name + end + end +end diff --git a/test/unit/project_drop_test.rb b/test/unit/project_drop_test.rb new file mode 100644 index 00000000..677bda04 --- /dev/null +++ b/test/unit/project_drop_test.rb @@ -0,0 +1,42 @@ +require File.expand_path('../../test_helper', __FILE__) + +class ProjectDropTest < ActiveSupport::TestCase + def setup + @project = Project.generate! + User.current = @user = User.generate! + @role = Role.generate! + Member.generate!(:principal => @user, :project => @project, :roles => [@role]) + @drop = @project.to_liquid + end + + context "drop" do + should "be a ProjectDrop" do + assert @drop.is_a?(ProjectDrop), "drop is not a ProjectDrop" + end + end + + + context "#name" do + should "return the project name" do + assert_equal @project.name, @drop.name + end + end + + context "#identifier" do + should "return the project identifier" do + assert_equal @project.identifier, @drop.identifier + end + end + + should "only load an object if it's visible to the current user" do + assert User.current.logged? + assert @project.visible? + + @private_project = Project.generate!(:is_public => false) + + assert !@private_project.visible?, "Project is visible" + @private_drop = ProjectDrop.new(@private_project) + assert_equal nil, @private_drop.instance_variable_get("@object") + assert_equal nil, @private_drop.name + end +end