Commit 4be59d5ee227d2819566d6a812164c8b52ec7cbd

Authored by Rafael Manzo
1 parent caaa5b13

ProjectImage and ProjectOwnership merged into ProjectAttributes

* Unit tests pass
  * Missing acceptance tests
  * The models were kept so the migrations works

Still has to filter the project list by not hidden.
app/controllers/concerns/ownership_authentication.rb
@@ -52,7 +52,7 @@ module OwnershipAuthentication @@ -52,7 +52,7 @@ module OwnershipAuthentication
52 private 52 private
53 53
54 def check_project_ownership(id) 54 def check_project_ownership(id)
55 - if current_user.project_ownerships.find_by_project_id(id).nil? 55 + if current_user.project_attributes.find_by_project_id(id).nil?
56 respond_to do |format| 56 respond_to do |format|
57 format.html { redirect_to projects_url, notice: "You're not allowed to do this operation" } 57 format.html { redirect_to projects_url, notice: "You're not allowed to do this operation" }
58 format.json { head :no_content } 58 format.json { head :no_content }
app/controllers/projects_controller.rb
@@ -9,7 +9,6 @@ class ProjectsController < ApplicationController @@ -9,7 +9,6 @@ class ProjectsController < ApplicationController
9 # GET /projects/new 9 # GET /projects/new
10 def new 10 def new
11 @project = Project.new 11 @project = Project.new
12 - @project_image = ProjectImage.new  
13 end 12 end
14 13
15 # GET /projects 14 # GET /projects
@@ -25,7 +24,7 @@ class ProjectsController < ApplicationController @@ -25,7 +24,7 @@ class ProjectsController < ApplicationController
25 @project = Project.new(project_params) 24 @project = Project.new(project_params)
26 respond_to do |format| 25 respond_to do |format|
27 create_and_redir(format) 26 create_and_redir(format)
28 - ProjectImage.create(url: image_url, project_id: @project.id ) 27 + @project.attributes.update(image_url: image_url) unless @project.attributes.nil?
29 end 28 end
30 end 29 end
31 30
@@ -45,7 +44,7 @@ class ProjectsController < ApplicationController @@ -45,7 +44,7 @@ class ProjectsController < ApplicationController
45 def update 44 def update
46 set_project 45 set_project
47 image_url = project_params.delete(:image_url) 46 image_url = project_params.delete(:image_url)
48 - if @project.update(project_params) && @project_image.update(url: image_url) 47 + if @project.update(project_params) && @project.attributes.update(image_url: image_url)
49 redirect_to(project_path(@project.id)) 48 redirect_to(project_path(@project.id))
50 else 49 else
51 render "edit" 50 render "edit"
@@ -56,7 +55,7 @@ class ProjectsController < ApplicationController @@ -56,7 +55,7 @@ class ProjectsController < ApplicationController
56 # DELETE /project/1.json 55 # DELETE /project/1.json
57 def destroy 56 def destroy
58 set_project 57 set_project
59 - current_user.project_ownerships.find_by_project_id(@project.id).destroy 58 + current_user.project_attributes.find_by_project_id(@project.id).destroy
60 @project.destroy 59 @project.destroy
61 respond_to do |format| 60 respond_to do |format|
62 format.html { redirect_to projects_url } 61 format.html { redirect_to projects_url }
@@ -68,7 +67,6 @@ class ProjectsController < ApplicationController @@ -68,7 +67,6 @@ class ProjectsController < ApplicationController
68 # Use callbacks to share common setup or constraints between actions. 67 # Use callbacks to share common setup or constraints between actions.
69 def set_project 68 def set_project
70 @project = find_resource(Project, params[:id].to_i) 69 @project = find_resource(Project, params[:id].to_i)
71 - @project_image = ProjectImage.find_by_project_id(@project.id) if @project.is_a?(Project)  
72 end 70 end
73 71
74 # Never trust parameters from the scary internet, only allow the white list through. 72 # Never trust parameters from the scary internet, only allow the white list through.
@@ -80,7 +78,7 @@ class ProjectsController < ApplicationController @@ -80,7 +78,7 @@ class ProjectsController < ApplicationController
80 # Extracted code from create action 78 # Extracted code from create action
81 def create_and_redir(format) 79 def create_and_redir(format)
82 if @project.save 80 if @project.save
83 - current_user.project_ownerships.create project_id: @project.id 81 + current_user.project_attributes.create(project_id: @project.id)
84 format.html { redirect_to project_path(@project.id), notice: 'Project was successfully created.' } 82 format.html { redirect_to project_path(@project.id), notice: 'Project was successfully created.' }
85 format.json { render action: 'show', status: :created, location: @project } 83 format.json { render action: 'show', status: :created, location: @project }
86 else 84 else
app/helpers/projects_helper.rb
1 module ProjectsHelper 1 module ProjectsHelper
2 def project_owner? project_id 2 def project_owner? project_id
3 - user_signed_in? && !current_user.project_ownerships.find_by_project_id(project_id).nil? 3 + user_signed_in? && !current_user.project_attributes.find_by_project_id(project_id).nil?
4 end 4 end
5 end 5 end
6 \ No newline at end of file 6 \ No newline at end of file
app/models/project.rb
1 class Project < KalibroClient::Entities::Processor::Project 1 class Project < KalibroClient::Entities::Processor::Project
2 include KalibroRecord 2 include KalibroRecord
3 def self.latest(count = 1) 3 def self.latest(count = 1)
4 - all.sort { |a,b| b.id <=> a.id }.first(count) 4 + all.sort { |a,b| b.id <=> a.id }.select { |project| !project.attributes.hidden}.first(count)
  5 + end
  6 +
  7 + def attributes
  8 + ProjectAttributes.find_by_project_id(self.id)
5 end 9 end
6 end 10 end
app/models/project_attributes.rb 0 → 100644
@@ -0,0 +1,8 @@ @@ -0,0 +1,8 @@
  1 +class ProjectAttributes < ActiveRecord::Base
  2 + belongs_to :user
  3 + belongs_to :project
  4 +
  5 + def project
  6 + Project.find(self.project_id)
  7 + end
  8 +end
app/models/project_image.rb
  1 +#FIXME: remove this after the migration has been done and modify the migration accordingly
1 class ProjectImage < ActiveRecord::Base 2 class ProjectImage < ActiveRecord::Base
2 belongs_to :project 3 belongs_to :project
3 end 4 end
app/models/project_ownership.rb
  1 +#FIXME: remove this after the migration has been done and modify the migration accordingly
1 class ProjectOwnership < ActiveRecord::Base 2 class ProjectOwnership < ActiveRecord::Base
2 belongs_to :user 3 belongs_to :user
3 validates :project_id, presence: true 4 validates :project_id, presence: true
app/models/user.rb
@@ -8,12 +8,12 @@ class User &lt; ActiveRecord::Base @@ -8,12 +8,12 @@ class User &lt; ActiveRecord::Base
8 validates :email, presence: true 8 validates :email, presence: true
9 validates :email, uniqueness: true 9 validates :email, uniqueness: true
10 10
11 - has_many :project_ownerships 11 + has_many :project_attributes, class_name: 'ProjectAttributes'
12 has_many :reading_group_ownerships 12 has_many :reading_group_ownerships
13 has_many :kalibro_configuration_ownerships 13 has_many :kalibro_configuration_ownerships
14 # Alert: when adding new parameters to this model, they should also be added to registrations_controller 14 # Alert: when adding new parameters to this model, they should also be added to registrations_controller
15 15
16 def projects 16 def projects
17 - project_ownerships.map { |project_ownership| project_ownership.project } 17 + project_attributes.map { |project_attr| project_attr.project }
18 end 18 end
19 end 19 end
app/views/projects/_form.html.erb
@@ -17,7 +17,7 @@ @@ -17,7 +17,7 @@
17 <div class="form-row"> 17 <div class="form-row">
18 <div class="field-container"> 18 <div class="field-container">
19 <%= f.label "Image url", class: 'control-label' %><br> 19 <%= f.label "Image url", class: 'control-label' %><br>
20 - <%= f.text_field :image_url, class: 'text-area', value: @project_image.nil? ? '#' : @project_image.url %> 20 + <%= f.text_field :image_url, class: 'text-area', value: @project.attributes.image_url.nil? ? '#' : @project.attributes.image_url %>
21 </div> 21 </div>
22 </div> 22 </div>
23 </div> 23 </div>
db/migrate/20150225170704_create_project_attributes.rb 0 → 100644
@@ -0,0 +1,56 @@ @@ -0,0 +1,56 @@
  1 +class CreateProjectAttributes < ActiveRecord::Migration
  2 + def up
  3 + create_table :project_attributes do |t|
  4 + t.integer :project_id
  5 + t.string :image_url
  6 + t.integer :user_id
  7 + t.boolean :hidden, default: false
  8 +
  9 + t.timestamps null: false
  10 + end
  11 +
  12 + ProjectOwnership.all.each do |project_ownership|
  13 + project_image = ProjectImage.find_by_project_id(project_ownership.project_id)
  14 + image_url = project_image.nil? ? "": project_image.url
  15 +
  16 + begin
  17 + # We want to hides projects prior this date since they probably have a invalid configuration
  18 + if Project.find(project_ownership.project_id).updated_at < DateTime.parse("Mon, 23 Feb 2015")
  19 + hidden = true
  20 + else
  21 + hidden = false
  22 + end
  23 +
  24 + ProjectAttributes.create(user_id: project_ownership.user_id, project_id: project_ownership.project_id, image_url: image_url, hidden: hidden)
  25 + rescue KalibroClient::Errors::RecordNotFound
  26 + puts "Could not find project with id #{project_ownership.project_id} owned by user with #{project_ownership.user_id} and image url #{image_url}"
  27 + end
  28 + end
  29 +
  30 + drop_table :project_ownerships
  31 + drop_table :project_images
  32 + end
  33 +
  34 + def down
  35 + create_table :project_ownerships do |t|
  36 + t.integer :user_id
  37 + t.integer :project_id
  38 +
  39 + t.timestamps
  40 + end
  41 +
  42 + create_table :project_images do |t|
  43 + t.belongs_to :project
  44 + t.string :url
  45 +
  46 + t.timestamps
  47 + end
  48 +
  49 + ProjectAttributes.all.each do |project_attribute|
  50 + ProjectOwnership.create(user_id: project_attribute.user_id, project_id: project_attribute.project_id)
  51 + ProjectImage.create(url: project_attribute.image_url, project_id: project_attribute.project_id)
  52 + end
  53 +
  54 + drop_table :project_attributes
  55 + end
  56 +end
@@ -11,7 +11,7 @@ @@ -11,7 +11,7 @@
11 # 11 #
12 # It's strongly recommended that you check this file into your version control system. 12 # It's strongly recommended that you check this file into your version control system.
13 13
14 -ActiveRecord::Schema.define(version: 20141211114023) do 14 +ActiveRecord::Schema.define(version: 20150225170704) do
15 15
16 create_table "kalibro_configuration_ownerships", force: :cascade do |t| 16 create_table "kalibro_configuration_ownerships", force: :cascade do |t|
17 t.integer "user_id" 17 t.integer "user_id"
@@ -20,18 +20,13 @@ ActiveRecord::Schema.define(version: 20141211114023) do @@ -20,18 +20,13 @@ ActiveRecord::Schema.define(version: 20141211114023) do
20 t.datetime "updated_at" 20 t.datetime "updated_at"
21 end 21 end
22 22
23 - create_table "project_images", force: :cascade do |t| 23 + create_table "project_attributes", force: :cascade do |t|
24 t.integer "project_id" 24 t.integer "project_id"
25 - t.string "url", limit: 255  
26 - t.datetime "created_at"  
27 - t.datetime "updated_at"  
28 - end  
29 -  
30 - create_table "project_ownerships", force: :cascade do |t| 25 + t.string "image_url"
31 t.integer "user_id" 26 t.integer "user_id"
32 - t.integer "project_id"  
33 - t.datetime "created_at"  
34 - t.datetime "updated_at" 27 + t.boolean "hidden", default: false
  28 + t.datetime "created_at", null: false
  29 + t.datetime "updated_at", null: false
35 end 30 end
36 31
37 create_table "reading_group_ownerships", force: :cascade do |t| 32 create_table "reading_group_ownerships", force: :cascade do |t|
spec/controllers/concerns/ownership_authentication_spec.rb
@@ -111,12 +111,12 @@ describe OwnershipAuthentication, type: :controller do @@ -111,12 +111,12 @@ describe OwnershipAuthentication, type: :controller do
111 end 111 end
112 112
113 context 'when the user owns the Repository' do 113 context 'when the user owns the Repository' do
114 - let!(:project_ownership) { FactoryGirl.build(:project_ownership, {user_id: current_user.id, project_id: project.id}) } 114 + let!(:project_attributes) { FactoryGirl.build(:project_attributes, {user_id: current_user.id, project_id: project.id}) }
115 115
116 before do 116 before do
117 - project_ownerships = Object.new  
118 - project_ownerships.expects(:find_by_project_id).with(project.id).returns(project_ownership)  
119 - current_user.expects(:project_ownerships).returns(project_ownerships) 117 + project_attrs = Object.new
  118 + project_attrs.expects(:find_by_project_id).with(project.id).returns(project_attributes)
  119 + current_user.expects(:project_attributes).returns(project_attrs)
120 end 120 end
121 121
122 it 'should return true' do 122 it 'should return true' do
@@ -126,9 +126,9 @@ describe OwnershipAuthentication, type: :controller do @@ -126,9 +126,9 @@ describe OwnershipAuthentication, type: :controller do
126 126
127 context 'when the user does not own the Repository' do 127 context 'when the user does not own the Repository' do
128 before do 128 before do
129 - project_ownerships = Object.new  
130 - project_ownerships.expects(:find_by_project_id).with(project.id).returns(nil)  
131 - current_user.expects(:project_ownerships).returns(project_ownerships) 129 + project_attrs = Object.new
  130 + project_attrs.expects(:find_by_project_id).with(project.id).returns(nil)
  131 + current_user.expects(:project_attributes).returns(project_attrs)
132 end 132 end
133 133
134 it 'should respond' do # FIXME: this is not the best test, but it it's the closest we can do I think 134 it 'should respond' do # FIXME: this is not the best test, but it it's the closest we can do I think
spec/controllers/projects_controller_spec.rb
@@ -91,21 +91,20 @@ describe ProjectsController, :type =&gt; :controller do @@ -91,21 +91,20 @@ describe ProjectsController, :type =&gt; :controller do
91 context 'with a User logged in' do 91 context 'with a User logged in' do
92 before do 92 before do
93 sign_in FactoryGirl.create(:user) 93 sign_in FactoryGirl.create(:user)
94 - @ownership = FactoryGirl.build(:project_ownership)  
95 - @ownerships = []  
96 - 94 + @project_attributes = FactoryGirl.build(:project_attributes)
  95 + @attributes = []
97 end 96 end
98 97
99 context 'when the user owns the project' do 98 context 'when the user owns the project' do
100 before :each do 99 before :each do
101 - @ownership.expects(:destroy) 100 + @project_attributes.expects(:destroy)
102 @subject.expects(:destroy) 101 @subject.expects(:destroy)
103 102
104 #Those two mocks looks the same but they are necessary since params[:id] is a String and @project.id is an Integer :( 103 #Those two mocks looks the same but they are necessary since params[:id] is a String and @project.id is an Integer :(
105 - @ownerships.expects(:find_by_project_id).with("#{@subject.id}").returns(@ownership)  
106 - @ownerships.expects(:find_by_project_id).with(@subject.id).returns(@ownership) 104 + @attributes.expects(:find_by_project_id).with("#{@subject.id}").returns(@project_attributes)
  105 + @attributes.expects(:find_by_project_id).with(@subject.id).returns(@project_attributes)
107 106
108 - User.any_instance.expects(:project_ownerships).at_least_once.returns(@ownerships) 107 + User.any_instance.expects(:project_attributes).at_least_once.returns(@attributes)
109 108
110 subject.expects(:find_resource).with(Project, @subject.id).returns(@subject) 109 subject.expects(:find_resource).with(Project, @subject.id).returns(@subject)
111 delete :destroy, :id => @subject.id 110 delete :destroy, :id => @subject.id
@@ -120,8 +119,8 @@ describe ProjectsController, :type =&gt; :controller do @@ -120,8 +119,8 @@ describe ProjectsController, :type =&gt; :controller do
120 119
121 context "when the user doesn't own the project" do 120 context "when the user doesn't own the project" do
122 before :each do 121 before :each do
123 - @ownerships.expects(:find_by_project_id).with("#{@subject.id}").returns(nil)  
124 - User.any_instance.expects(:project_ownerships).at_least_once.returns(@ownerships) 122 + @attributes.expects(:find_by_project_id).with("#{@subject.id}").returns(nil)
  123 + User.any_instance.expects(:project_attributes).at_least_once.returns(@attributes)
125 124
126 delete :destroy, :id => @subject.id 125 delete :destroy, :id => @subject.id
127 end 126 end
@@ -152,15 +151,14 @@ describe ProjectsController, :type =&gt; :controller do @@ -152,15 +151,14 @@ describe ProjectsController, :type =&gt; :controller do
152 describe 'edit' do 151 describe 'edit' do
153 before do 152 before do
154 @subject = FactoryGirl.build(:project_with_id) 153 @subject = FactoryGirl.build(:project_with_id)
155 - @project_image = FactoryGirl.create(:project_image)  
156 end 154 end
157 155
158 context 'with an User logged in' do 156 context 'with an User logged in' do
159 before do 157 before do
160 @user = FactoryGirl.create(:user) 158 @user = FactoryGirl.create(:user)
161 - @ownership = FactoryGirl.build(:project_ownership)  
162 - @ownerships = []  
163 - User.any_instance.expects(:project_ownerships).at_least_once.returns(@ownerships) 159 + @attribute = FactoryGirl.build(:project_attributes)
  160 + @attributes = []
  161 + User.any_instance.expects(:project_attributes).at_least_once.returns(@attributes)
164 162
165 sign_in @user 163 sign_in @user
166 end 164 end
@@ -168,8 +166,7 @@ describe ProjectsController, :type =&gt; :controller do @@ -168,8 +166,7 @@ describe ProjectsController, :type =&gt; :controller do
168 context 'when the user owns the project' do 166 context 'when the user owns the project' do
169 before :each do 167 before :each do
170 subject.expects(:find_resource).with(Project, @subject.id).returns(@subject) 168 subject.expects(:find_resource).with(Project, @subject.id).returns(@subject)
171 - @ownerships.expects(:find_by_project_id).with("#{@subject.id}").returns(@ownership)  
172 - ProjectImage.expects(:find_by_project_id).with(@subject.id).returns(@project_image) 169 + @attributes.expects(:find_by_project_id).with("#{@subject.id}").returns(@attribute)
173 170
174 get :edit, :id => @subject.id 171 get :edit, :id => @subject.id
175 end 172 end
@@ -184,7 +181,7 @@ describe ProjectsController, :type =&gt; :controller do @@ -184,7 +181,7 @@ describe ProjectsController, :type =&gt; :controller do
184 context 'when the user does not own the project' do 181 context 'when the user does not own the project' do
185 before do 182 before do
186 @subject = FactoryGirl.build(:another_project) 183 @subject = FactoryGirl.build(:another_project)
187 - @ownerships.expects(:find_by_project_id).with("#{@subject.id}").returns(nil) 184 + @attributes.expects(:find_by_project_id).with("#{@subject.id}").returns(nil)
188 185
189 get :edit, :id => @subject.id 186 get :edit, :id => @subject.id
190 end 187 end
@@ -205,7 +202,6 @@ describe ProjectsController, :type =&gt; :controller do @@ -205,7 +202,6 @@ describe ProjectsController, :type =&gt; :controller do
205 202
206 describe 'update' do 203 describe 'update' do
207 before do 204 before do
208 - @project_image = FactoryGirl.build(:project_image)  
209 @subject = FactoryGirl.build(:project_with_id) 205 @subject = FactoryGirl.build(:project_with_id)
210 @subject_params = @subject.to_hash 206 @subject_params = @subject.to_hash
211 end 207 end
@@ -217,17 +213,18 @@ describe ProjectsController, :type =&gt; :controller do @@ -217,17 +213,18 @@ describe ProjectsController, :type =&gt; :controller do
217 213
218 context 'when user owns the project' do 214 context 'when user owns the project' do
219 before do 215 before do
220 - @ownership = FactoryGirl.build(:project_ownership)  
221 - @ownerships = []  
222 - @ownerships.expects(:find_by_project_id).with("#{@subject.id}").returns(@ownership)  
223 - User.any_instance.expects(:project_ownerships).at_least_once.returns(@ownerships)  
224 - ProjectImage.expects(:find_by_project_id).with(@subject.id).returns(@project_image) 216 + @project_attributes = FactoryGirl.build(:project_attributes)
  217 + @attributes = []
  218 + @attributes.expects(:find_by_project_id).with("#{@subject.id}").returns(@project_attributes)
  219 + User.any_instance.expects(:project_attributes).at_least_once.returns(@attributes)
225 end 220 end
226 221
227 context 'with valid fields' do 222 context 'with valid fields' do
228 before :each do 223 before :each do
229 subject.expects(:find_resource).with(Project, @subject.id).returns(@subject) 224 subject.expects(:find_resource).with(Project, @subject.id).returns(@subject)
230 Project.any_instance.expects(:update).with(@subject_params).returns(true) 225 Project.any_instance.expects(:update).with(@subject_params).returns(true)
  226 + @project_attributes.expects(:update).with(image_url: @subject_params[:image_url]).returns(true)
  227 + @subject.expects(:attributes).returns(@project_attributes)
231 end 228 end
232 229
233 context 'rendering the show' do 230 context 'rendering the show' do
spec/factories/project_attributes.rb 0 → 100644
@@ -0,0 +1,8 @@ @@ -0,0 +1,8 @@
  1 +FactoryGirl.define do
  2 + factory :project_attributes, :class => 'ProjectAttributes' do
  3 + project_id 1
  4 + image_url "MyString"
  5 + user_id 1
  6 + hidden false
  7 + end
  8 +end
spec/factories/project_images.rb
@@ -1,12 +0,0 @@ @@ -1,12 +0,0 @@
1 -# Read about factories at https://github.com/thoughtbot/factory_girl  
2 -  
3 -FactoryGirl.define do  
4 - factory :project_image do  
5 - project_id 1  
6 - url "logo.png"  
7 - end  
8 -  
9 - factory :project_no_image, class: ProjectImage do  
10 - url nil  
11 - end  
12 -end  
spec/factories/project_ownerships.rb
@@ -1,8 +0,0 @@ @@ -1,8 +0,0 @@
1 -# Read about factories at https://github.com/thoughtbot/factory_girl  
2 -  
3 -FactoryGirl.define do  
4 - factory :project_ownership do  
5 - user_id 1  
6 - project_id 1  
7 - end  
8 -end  
spec/helpers/projects_helper_spec.rb
@@ -19,10 +19,10 @@ describe ProjectsHelper, :type =&gt; :helper do @@ -19,10 +19,10 @@ describe ProjectsHelper, :type =&gt; :helper do
19 helper.expects(:user_signed_in?).returns(true) 19 helper.expects(:user_signed_in?).returns(true)
20 helper.expects(:current_user).returns(FactoryGirl.build(:user)) 20 helper.expects(:current_user).returns(FactoryGirl.build(:user))
21 21
22 - @ownerships = []  
23 - @ownerships.expects(:find_by_project_id).with(@subject.id).returns(nil) 22 + @attributes = []
  23 + @attributes.expects(:find_by_project_id).with(@subject.id).returns(nil)
24 24
25 - User.any_instance.expects(:project_ownerships).returns(@ownerships) 25 + User.any_instance.expects(:project_attributes).returns(@attributes)
26 end 26 end
27 27
28 it { expect(helper.project_owner?(@subject.id)).to be_falsey } 28 it { expect(helper.project_owner?(@subject.id)).to be_falsey }
@@ -33,10 +33,10 @@ describe ProjectsHelper, :type =&gt; :helper do @@ -33,10 +33,10 @@ describe ProjectsHelper, :type =&gt; :helper do
33 helper.expects(:user_signed_in?).returns(true) 33 helper.expects(:user_signed_in?).returns(true)
34 helper.expects(:current_user).returns(FactoryGirl.build(:user)) 34 helper.expects(:current_user).returns(FactoryGirl.build(:user))
35 35
36 - @ownership = FactoryGirl.build(:project_ownership)  
37 - @ownerships = []  
38 - @ownerships.expects(:find_by_project_id).with(@subject.id).returns(@ownership)  
39 - User.any_instance.expects(:project_ownerships).returns(@ownerships) 36 + @project_attributes = FactoryGirl.build(:project_attributes)
  37 + @attributes = []
  38 + @attributes.expects(:find_by_project_id).with(@subject.id).returns(@project_attributes)
  39 + User.any_instance.expects(:project_attributes).returns(@attributes)
40 end 40 end
41 41
42 it { expect(helper.project_owner?(@subject.id)).to be_truthy } 42 it { expect(helper.project_owner?(@subject.id)).to be_truthy }
spec/models/project_attributes_spec.rb 0 → 100644
@@ -0,0 +1,23 @@ @@ -0,0 +1,23 @@
  1 +require 'rails_helper'
  2 +
  3 +RSpec.describe ProjectAttributes, type: :model do
  4 + describe 'associations' do
  5 + it { is_expected.to belong_to(:user) }
  6 + it { is_expected.to belong_to(:project) }
  7 + end
  8 +
  9 + describe 'methods' do
  10 + describe 'project' do
  11 + subject { FactoryGirl.build(:project_attributes) }
  12 + let(:project) {FactoryGirl.build(:project_with_id)}
  13 +
  14 + before :each do
  15 + Project.expects(:find).with(subject.project_id).returns(project)
  16 + end
  17 +
  18 + it 'should return the project' do
  19 + expect(subject.project).to eq(project)
  20 + end
  21 + end
  22 + end
  23 +end
spec/models/project_image_spec.rb
@@ -1,7 +0,0 @@ @@ -1,7 +0,0 @@
1 -require 'rails_helper'  
2 -  
3 -RSpec.describe ProjectImage, :type => :model do  
4 - describe 'associations' do  
5 - it { is_expected.to belong_to(:project) }  
6 - end  
7 -end  
spec/models/project_ownership_spec.rb
@@ -1,22 +0,0 @@ @@ -1,22 +0,0 @@
1 -require 'rails_helper'  
2 -  
3 -describe ProjectOwnership, :type => :model do  
4 - describe 'associations' do  
5 - it { is_expected.to belong_to(:user) }  
6 - end  
7 -  
8 - describe 'methods' do  
9 - describe 'project' do  
10 - subject {FactoryGirl.build(:project_ownership)}  
11 - let(:project) {FactoryGirl.build(:project_with_id)}  
12 -  
13 - before :each do  
14 - Project.expects(:find).with(subject.project_id).returns(project)  
15 - end  
16 -  
17 - it 'should return the project' do  
18 - expect(subject.project).to eq(project)  
19 - end  
20 - end  
21 - end  
22 -end  
spec/models/project_spec.rb
@@ -3,22 +3,40 @@ require &#39;rails_helper&#39; @@ -3,22 +3,40 @@ require &#39;rails_helper&#39;
3 describe Project, :type => :model do 3 describe Project, :type => :model do
4 describe 'methods' do 4 describe 'methods' do
5 describe 'latest' do 5 describe 'latest' do
  6 + let!(:project) { FactoryGirl.build(:project_with_id, id: 1) }
  7 + let!(:another_project) { FactoryGirl.build(:another_project, id: 2) }
  8 + let!(:project_attributes) { FactoryGirl.build(:project_attributes) }
  9 +
6 before :each do 10 before :each do
7 - @qt = FactoryGirl.build(:project_with_id)  
8 - @kalibro = FactoryGirl.build(:another_project) 11 + project.expects(:attributes).returns(project_attributes)
  12 + another_project.expects(:attributes).returns(project_attributes)
9 13
10 - Project.expects(:all).returns([@qt, @kalibro]) 14 + Project.expects(:all).returns([project, another_project])
11 end 15 end
12 16
13 it 'should return the two projects ordered' do 17 it 'should return the two projects ordered' do
14 - expect(Project.latest(2)).to eq([@kalibro, @qt]) 18 + expect(Project.latest(2)).to eq([another_project, project])
15 end 19 end
16 20
17 context 'when no parameter is passed' do 21 context 'when no parameter is passed' do
18 it 'should return just the most recent project' do 22 it 'should return just the most recent project' do
19 - expect(Project.latest).to eq([@kalibro]) 23 + expect(Project.latest).to eq([another_project])
20 end 24 end
21 end 25 end
22 end 26 end
  27 +
  28 + describe 'attributes' do
  29 + subject { FactoryGirl.build(:project_with_id) }
  30 +
  31 + let!(:project_attributes) { FactoryGirl.build(:project_attributes) }
  32 +
  33 + before :each do
  34 + ProjectAttributes.expects(:find_by_project_id).returns(project_attributes)
  35 + end
  36 +
  37 + it 'is expected to return the project attributes' do
  38 + expect(subject.attributes).to eq(project_attributes)
  39 + end
  40 + end
23 end 41 end
24 end 42 end
spec/models/user_spec.rb
@@ -10,7 +10,7 @@ describe User, :type =&gt; :model do @@ -10,7 +10,7 @@ describe User, :type =&gt; :model do
10 end 10 end
11 11
12 describe 'associations' do 12 describe 'associations' do
13 - it { is_expected.to have_many(:project_ownerships) } 13 + it { is_expected.to have_many(:project_attributes) }
14 it { is_expected.to have_many(:reading_group_ownerships) } 14 it { is_expected.to have_many(:reading_group_ownerships) }
15 it { is_expected.to have_many(:kalibro_configuration_ownerships) } 15 it { is_expected.to have_many(:kalibro_configuration_ownerships) }
16 end 16 end
@@ -19,11 +19,11 @@ describe User, :type =&gt; :model do @@ -19,11 +19,11 @@ describe User, :type =&gt; :model do
19 describe 'projects' do 19 describe 'projects' do
20 subject { FactoryGirl.build(:user) } 20 subject { FactoryGirl.build(:user) }
21 let(:project) {FactoryGirl.build(:project_with_id)} 21 let(:project) {FactoryGirl.build(:project_with_id)}
22 - let(:project_ownership) {FactoryGirl.build(:project_ownership)} 22 + let(:project_attributes) {FactoryGirl.build(:project_attributes)}
23 23
24 before :each do 24 before :each do
25 - project_ownership.expects(:project).returns(project)  
26 - subject.expects(:project_ownerships).returns([project_ownership]) 25 + project_attributes.expects(:project).returns(project)
  26 + subject.expects(:project_attributes).returns([project_attributes])
27 end 27 end
28 28
29 it 'should return a list of projects owned by the user' do 29 it 'should return a list of projects owned by the user' do