Skip to content

Commit

Permalink
Fix ProjectAttributes association with Project
Browse files Browse the repository at this point in the history
  * Renamed ProjectAttributes' column hidden to public and inverted
    default values
  * Implemented Project#"public_or_owned_by_user"
  * All features now create ProjectAttributes after creating a Project
  * Implemented ProjectAttributes#project and ProjectAttributes#project=
  * Fix Project#attributes to return nil when there is no
    ProjectAttribute
  * Fixes #263

Signed off by: Diego Araújo <[email protected]>
  • Loading branch information
marcheing authored and diegoamc committed Nov 9, 2015
1 parent df99935 commit 057737c
Show file tree
Hide file tree
Showing 15 changed files with 170 additions and 57 deletions.
2 changes: 1 addition & 1 deletion app/controllers/projects_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def new
# GET /projects
# GET /projects.json
def index
@projects = Project.all.select { |project| !project.attributes.hidden }
@projects = Project.public_or_owned_by_user(current_user)
end

# POST /projects
Expand Down
22 changes: 17 additions & 5 deletions app/models/project.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,30 @@ class Project < KalibroClient::Entities::Processor::Project

attr_writer :attributes

def self.public_or_owned_by_user(user = nil)
project_attributes = ProjectAttributes.where(public: true)
project_attributes += ProjectAttributes.where(user_id: user.id, public: false) if user

project_attributes.map do |attribute|
begin
self.find(attribute.project_id)
rescue KalibroClient::Errors::RecordNotFound
nil
end
end.compact
end

def self.latest(count = 1)
all.sort { |a,b| b.id <=> a.id }.select { |project| !project.attributes.hidden}.first(count)
all.sort { |a, b| b.id <=> a.id }.select { |project| project.attributes.public }.first(count)
end

def attributes
@project_attributes ||= ProjectAttributes.find_by_project_id(self.id)
@project_attributes.nil? ? ProjectAttributes.new : @project_attributes
@attributes ||= ProjectAttributes.find_by_project_id(@id)
end

def destroy
self.attributes.destroy if self.attributes
@project_attributes = nil
self.attributes.destroy unless self.attributes.nil?
@attributes = nil
super
end
end
7 changes: 6 additions & 1 deletion app/models/project_attributes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ class ProjectAttributes < ActiveRecord::Base
validates :user, presence: true

def project
Project.find(self.project_id)
@project ||= Project.find(project_id)
end

def project=(project)
@project = project
self.project_id = project.id
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class ChangeProjectAttributesPublicDefault < ActiveRecord::Migration
def change
rename_column :project_attributes, :hidden, :public
change_column_default :project_attributes, :public, true
end
end
22 changes: 11 additions & 11 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20150616164352) do
ActiveRecord::Schema.define(version: 20151106182639) do

create_table "kalibro_configuration_attributes", force: :cascade do |t|
t.integer "user_id"
Expand All @@ -25,9 +25,9 @@
t.integer "project_id"
t.string "image_url"
t.integer "user_id"
t.boolean "hidden", default: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.boolean "public", default: true
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end

create_table "reading_group_attributes", force: :cascade do |t|
Expand All @@ -48,19 +48,19 @@
add_index "repository_attributes", ["user_id"], name: "index_repository_attributes_on_user_id"

create_table "users", force: :cascade do |t|
t.string "name", limit: 255, default: "", null: false
t.string "email", limit: 255, default: "", null: false
t.string "name", default: "", null: false
t.string "email", default: "", null: false
t.datetime "created_at"
t.datetime "updated_at"
t.string "encrypted_password", limit: 255, default: "", null: false
t.string "reset_password_token", limit: 255
t.string "encrypted_password", default: "", null: false
t.string "reset_password_token"
t.datetime "reset_password_sent_at"
t.datetime "remember_created_at"
t.integer "sign_in_count", default: 0
t.integer "sign_in_count", default: 0
t.datetime "current_sign_in_at"
t.datetime "last_sign_in_at"
t.string "current_sign_in_ip", limit: 255
t.string "last_sign_in_ip", limit: 255
t.string "current_sign_in_ip"
t.string "last_sign_in_ip"
end

add_index "users", ["email"], name: "index_users_on_email", unique: true
Expand Down
1 change: 0 additions & 1 deletion features/project/deletion.feature
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ Feature: Project Deletion
Given I am a regular user
And I am signed in
And I own a sample project
And I have sample project_attributes
And I am at the Sample Project page
When I click the Destroy Project link
Then I should be in the All Projects page
Expand Down
10 changes: 2 additions & 8 deletions features/project/edition.feature
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,20 @@ Feature: Project
Given I am a regular user
And I am signed in
And I own a sample project
And I have sample project_attributes
And I am at the All Projects page
When I click the Edit link
Then I should be in the Edit Project page

@kalibro_processor_restart
Scenario: Should not show edit links from projects that doesn't belongs to me
Scenario: Should not show edit links from projects that doesn't belong to me
Given I am a regular user
And I am signed in
And I have a sample project
And I am at the All Projects page
Then I should not see Edit within table

@kalibro_processor_restart
Scenario: Should not render the edit page if the project doesn't belongs to the current user
Scenario: Should not render the edit page if the project doesn't belong to the current user
Given I am a regular user
And I am signed in
And I have a sample project
Expand All @@ -35,7 +34,6 @@ Feature: Project
Given I am a regular user
And I am signed in
And I own a sample project
And I have sample project_attributes
And I am at the All Projects page
When I click the Edit link
Then The field "project[name]" should be filled with the sample project "name"
Expand All @@ -46,7 +44,6 @@ Feature: Project
Given I am a regular user
And I am signed in
And I own a sample project
And I have sample project_attributes
And I am at the sample project edit page
And I fill the Name field with "Kalibro"
And I fill the Description field with "Web Service to collect metrics"
Expand All @@ -61,7 +58,6 @@ Feature: Project
And I have a project named "Qt-Calculator"
And I own a project named "Kalibro"
And I am at the sample project edit page
And I have sample project_attributes
And I fill the Name field with "Qt-Calculator"
When I press the Save button
Then I should see "Name has already been taken"
Expand All @@ -71,7 +67,6 @@ Feature: Project
Given I am a regular user
And I am signed in
And I own a sample project
And I have sample project_attributes
And I am at the sample project edit page
And I fill the Description field with "Web Service to collect metrics"
When I press the Save button
Expand All @@ -82,7 +77,6 @@ Feature: Project
Given I am a regular user
And I am signed in
And I own a sample project
And I have sample project_attributes
And I am at the sample project edit page
And I fill the Name field with " "
When I press the Save button
Expand Down
1 change: 0 additions & 1 deletion features/project/listing.feature
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ Feature: Project listing
Given I am a regular user
And I am signed in
And I have a sample project
And I have sample project_attributes
And I am at the All Projects page
When I click the Show link
Then the sample project should be there
5 changes: 0 additions & 5 deletions features/project/show.feature
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ Feature: Show Project
Scenario: Should not show the create repository link to user that doesn't own the project
Given I am a regular user
And I have a sample project
And I have sample project_attributes
And I have a sample configuration with native metrics
And I have a sample repository within the sample project
When I am at the Sample Project page
Expand All @@ -20,7 +19,6 @@ Scenario: Should show the create repository link the project owner
Given I am a regular user
And I am signed in
And I own a sample project
And I have sample project_attributes
When I am at the Sample Project page
Then I should see "New Repository"

Expand All @@ -29,7 +27,6 @@ Scenario: Should not show the independent repositories for a project
Given I am a regular user
And I am signed in
And I own a sample project
And I have sample project_attributes
And I have a sample configuration
And I have a sample repository
When I am at the Sample Project page
Expand All @@ -39,7 +36,6 @@ Scenario: Should not show the independent repositories for a project
Scenario: Considering the project has no repositories
Given I am a regular user
And I have a sample project
And I have sample project_attributes
When I am at the Sample Project page
Then I should see "There are no Repositories yet!"

Expand All @@ -56,6 +52,5 @@ Scenario: Considering the project has repositories
Scenario: Checking project contents
Given I am a regular user
And I have a sample project
And I have sample project_attributes
When I am at the Sample Project page
Then the sample project should be there
1 change: 0 additions & 1 deletion features/repository/create.feature
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ Scenario: repository creation associated with a project
Given I am a regular user
And I am signed in
And I own a sample project
And I have sample project_attributes
And I have a sample configuration with native metrics
And I am at the New Repository page
And I fill the Name field with "Kalibro"
Expand Down
6 changes: 2 additions & 4 deletions features/step_definitions/project_steps.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,12 @@

Given(/^I have a sample project$/) do
@project = FactoryGirl.create(:project)
end

Given(/^I have sample project_attributes$/) do
@project_attributes = FactoryGirl.create(:project_attributes, {id: nil, user_id: @user.id})
@project.attributes = FactoryGirl.create(:project_attributes, project: @project)
end

Given(/^I have a project named "(.*?)"$/) do |name|
@project = FactoryGirl.create(:project, {name: name})
@project.attributes = FactoryGirl.create(:project_attributes, project: @project)
end

Given(/^I own a sample project$/) do
Expand Down
6 changes: 2 additions & 4 deletions spec/controllers/projects_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -148,12 +148,10 @@
end

describe 'index' do
let(:project_attributes) { FactoryGirl.build(:project_attributes) }
subject { FactoryGirl.build(:project_with_id) }

before :each do
@subject = FactoryGirl.build(:project_with_id)
Project.expects(:all).returns([@subject])
@subject.expects(:attributes).returns(project_attributes)
Project.expects(:public_or_owned_by_user).returns([subject])
get :index
end

Expand Down
15 changes: 12 additions & 3 deletions spec/factories/project_attributes.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
FactoryGirl.define do
factory :project_attributes, :class => 'ProjectAttributes' do
project_id 1
image_url ''
user_id 1
hidden false
self.public true
association :project, :with_id, strategy: :build
association :user, strategy: :build

trait :with_image do
image_url '#'
end

trait :private do
self.public false
end

trait :bare do
project_id nil
user_id nil
end
end
end
21 changes: 18 additions & 3 deletions spec/models/project_attributes_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,9 @@
end

describe 'methods' do
let(:project) { FactoryGirl.build(:project_with_id) }
describe 'project' do
subject { FactoryGirl.build(:project_attributes) }
let(:project) {FactoryGirl.build(:project_with_id)}

subject { FactoryGirl.build(:project_attributes, :bare, project_id: project.id) }
before :each do
Project.expects(:find).with(subject.project_id).returns(project)
end
Expand All @@ -23,5 +22,21 @@
expect(subject.project).to eq(project)
end
end

describe 'project=' do
subject { FactoryGirl.build(:project_attributes, :bare) }

before do
subject.project = project
end

it 'is expected to set the project' do
expect(subject.project).to eq project
end

it 'is expected to set the project_id' do
expect(subject.project_id).to eq project.id
end
end
end
end
Loading

0 comments on commit 057737c

Please sign in to comment.