Commit ba131e659e2c2e10daf79f7be755a64c7da99ffe

Authored by Pedro
2 parents 119b56ec 04c3f27d

Merge pull request #221 from mezuro/configuration_visibility

Configuration visibility
Showing 60 changed files with 736 additions and 255 deletions   Show diff stats
@@ -29,3 +29,4 @@ db_bootstrap.sql @@ -29,3 +29,4 @@ db_bootstrap.sql
29 install.sh 29 install.sh
30 kalibro_gatekeeper.yml 30 kalibro_gatekeeper.yml
31 .capistrano 31 .capistrano
  32 +rerun.txt
app/assets/stylesheets/form_with_tooltip.css
1 .form-table { 1 .form-table {
2 margin: 0 !important; 2 margin: 0 !important;
  3 + padding: 20px;
3 background: #fff; 4 background: #fff;
4 -webkit-box-shadow: 0 1px 2px rgba(0, 0, 0, 0.075); 5 -webkit-box-shadow: 0 1px 2px rgba(0, 0, 0, 0.075);
5 box-shadow: 0 1px 2px rgba(0, 0, 0, 0.075); 6 box-shadow: 0 1px 2px rgba(0, 0, 0, 0.075);
@@ -13,11 +14,32 @@ @@ -13,11 +14,32 @@
13 display: inline-flex; 14 display: inline-flex;
14 } 15 }
15 16
  17 +.form-row:first-child {
  18 + border-top: none;
  19 +}
  20 +
  21 +.form-row:last-child {
  22 + border-bottom: none;
  23 +}
  24 +
16 .field-container { 25 .field-container {
17 position: relative; 26 position: relative;
18 float: left; 27 float: left;
19 width: 100%; 28 width: 100%;
20 - padding: 20px; 29 + padding: 20px 0 20px 0;
  30 +}
  31 +
  32 +.form-row:first-child .field-container {
  33 + padding-top: 0;
  34 +}
  35 +
  36 +.form-row:last-child .field-container {
  37 + padding-bottom: 0;
  38 +}
  39 +
  40 +.field-container.checkbox, .field-container.radio {
  41 + padding-top: 0;
  42 + padding-bottom: 0;
21 } 43 }
22 44
23 .help-container { 45 .help-container {
app/controllers/concerns/ownership_authentication.rb
@@ -63,7 +63,7 @@ module OwnershipAuthentication @@ -63,7 +63,7 @@ module OwnershipAuthentication
63 end 63 end
64 64
65 def check_reading_group_ownership(id) 65 def check_reading_group_ownership(id)
66 - if current_user.reading_group_ownerships.find_by_reading_group_id(id).nil? 66 + if current_user.reading_group_attributes.find_by_reading_group_id(id).nil?
67 respond_to do |format| 67 respond_to do |format|
68 format.html { redirect_to reading_group_url(id: id), notice: t('not_allowed') } 68 format.html { redirect_to reading_group_url(id: id), notice: t('not_allowed') }
69 format.json { head :no_content } 69 format.json { head :no_content }
@@ -74,7 +74,7 @@ module OwnershipAuthentication @@ -74,7 +74,7 @@ module OwnershipAuthentication
74 end 74 end
75 75
76 def check_kalibro_configuration_ownership(id) 76 def check_kalibro_configuration_ownership(id)
77 - if current_user.kalibro_configuration_ownerships.find_by_kalibro_configuration_id(id).nil? 77 + if current_user.kalibro_configuration_attributes.find_by_kalibro_configuration_id(id).nil?
78 respond_to do |format| 78 respond_to do |format|
79 format.html { redirect_to kalibro_configurations_url(id: id), notice: t('not_allowed') } 79 format.html { redirect_to kalibro_configurations_url(id: id), notice: t('not_allowed') }
80 format.json { head :no_content } 80 format.json { head :no_content }
app/controllers/kalibro_configurations_controller.rb
@@ -3,6 +3,7 @@ include OwnershipAuthentication @@ -3,6 +3,7 @@ include OwnershipAuthentication
3 class KalibroConfigurationsController < ApplicationController 3 class KalibroConfigurationsController < ApplicationController
4 before_action :authenticate_user!, except: [:index, :show] 4 before_action :authenticate_user!, except: [:index, :show]
5 before_action :kalibro_configuration_owner?, only: [:edit, :update, :destroy] 5 before_action :kalibro_configuration_owner?, only: [:edit, :update, :destroy]
  6 + before_action :set_kalibro_configuration, only: [:show, :edit, :update, :destroy]
6 7
7 # GET /kalibro_configurations/new 8 # GET /kalibro_configurations/new
8 def new 9 def new
@@ -12,7 +13,7 @@ class KalibroConfigurationsController &lt; ApplicationController @@ -12,7 +13,7 @@ class KalibroConfigurationsController &lt; ApplicationController
12 # GET /kalibro_configurations 13 # GET /kalibro_configurations
13 # GET /kalibro_configurations.json 14 # GET /kalibro_configurations.json
14 def index 15 def index
15 - @kalibro_configurations = KalibroConfiguration.all 16 + @kalibro_configurations = KalibroConfiguration.public_or_owned_by_user(current_user)
16 end 17 end
17 18
18 # POST /kalibro_configurations 19 # POST /kalibro_configurations
@@ -27,7 +28,6 @@ class KalibroConfigurationsController &lt; ApplicationController @@ -27,7 +28,6 @@ class KalibroConfigurationsController &lt; ApplicationController
27 # GET /kalibro_configurations/1 28 # GET /kalibro_configurations/1
28 # GET /kalibro_configurations/1.json 29 # GET /kalibro_configurations/1.json
29 def show 30 def show
30 - set_kalibro_configuration  
31 Rails.cache.fetch("#{@kalibro_configuration.id}_metric_configurations") do 31 Rails.cache.fetch("#{@kalibro_configuration.id}_metric_configurations") do
32 @kalibro_configuration.metric_configurations 32 @kalibro_configuration.metric_configurations
33 end 33 end
@@ -35,13 +35,9 @@ class KalibroConfigurationsController &lt; ApplicationController @@ -35,13 +35,9 @@ class KalibroConfigurationsController &lt; ApplicationController
35 35
36 # GET /kalibro_configurations/1/edit 36 # GET /kalibro_configurations/1/edit
37 # GET /kalibro_configurations/1/edit.json 37 # GET /kalibro_configurations/1/edit.json
38 - def edit  
39 - set_kalibro_configuration  
40 - end  
41 - 38 + def edit; end
42 39
43 def update 40 def update
44 - set_kalibro_configuration  
45 if @kalibro_configuration.update(kalibro_configuration_params) 41 if @kalibro_configuration.update(kalibro_configuration_params)
46 redirect_to(kalibro_configuration_path(@kalibro_configuration.id)) 42 redirect_to(kalibro_configuration_path(@kalibro_configuration.id))
47 else 43 else
@@ -52,13 +48,13 @@ class KalibroConfigurationsController &lt; ApplicationController @@ -52,13 +48,13 @@ class KalibroConfigurationsController &lt; ApplicationController
52 # DELETE /kalibro_configurations/1 48 # DELETE /kalibro_configurations/1
53 # DELETE /kalibro_configurations/1.json 49 # DELETE /kalibro_configurations/1.json
54 def destroy 50 def destroy
55 - set_kalibro_configuration  
56 - current_user.kalibro_configuration_ownerships.find_by_kalibro_configuration_id!(@kalibro_configuration.id).destroy  
57 @kalibro_configuration.destroy 51 @kalibro_configuration.destroy
  52 +
58 respond_to do |format| 53 respond_to do |format|
59 format.html { redirect_to kalibro_configurations_url } 54 format.html { redirect_to kalibro_configurations_url }
60 format.json { head :no_content } 55 format.json { head :no_content }
61 end 56 end
  57 +
62 Rails.cache.delete("#{@kalibro_configuration.id}_metrics") 58 Rails.cache.delete("#{@kalibro_configuration.id}_metrics")
63 end 59 end
64 60
@@ -77,7 +73,7 @@ class KalibroConfigurationsController &lt; ApplicationController @@ -77,7 +73,7 @@ class KalibroConfigurationsController &lt; ApplicationController
77 # Extracted code from create action 73 # Extracted code from create action
78 def create_and_redir(format) 74 def create_and_redir(format)
79 if @kalibro_configuration.save 75 if @kalibro_configuration.save
80 - current_user.kalibro_configuration_ownerships.create kalibro_configuration_id: @kalibro_configuration.id 76 + current_user.kalibro_configuration_attributes.create(kalibro_configuration_id: @kalibro_configuration.id)
81 77
82 format.html { redirect_to kalibro_configuration_path(@kalibro_configuration.id), notice: t('successfully_created', :record => @kalibro_configuration.model_name.human) } 78 format.html { redirect_to kalibro_configuration_path(@kalibro_configuration.id), notice: t('successfully_created', :record => @kalibro_configuration.model_name.human) }
83 format.json { render action: 'show', status: :created, location: @kalibro_configuration } 79 format.json { render action: 'show', status: :created, location: @kalibro_configuration }
app/controllers/metric_configurations_controller.rb
@@ -9,6 +9,9 @@ class MetricConfigurationsController &lt; BaseMetricConfigurationsController @@ -9,6 +9,9 @@ class MetricConfigurationsController &lt; BaseMetricConfigurationsController
9 super 9 super
10 # find_by_name throws an exception instead of returning nil, unlike ActiveRecord's API 10 # find_by_name throws an exception instead of returning nil, unlike ActiveRecord's API
11 metric_configuration.metric = KalibroClient::Entities::Processor::MetricCollectorDetails.find_by_name(params[:metric_collector_name]).find_metric_by_code params[:metric_code] 11 metric_configuration.metric = KalibroClient::Entities::Processor::MetricCollectorDetails.find_by_name(params[:metric_collector_name]).find_metric_by_code params[:metric_code]
  12 + @reading_groups = ReadingGroup.public_or_owned_by_user(current_user).map { |reading_group|
  13 + [reading_group.name, reading_group.id]
  14 + }
12 end 15 end
13 16
14 def create 17 def create
@@ -24,6 +27,9 @@ class MetricConfigurationsController &lt; BaseMetricConfigurationsController @@ -24,6 +27,9 @@ class MetricConfigurationsController &lt; BaseMetricConfigurationsController
24 #FIXME: set the configuration id just once! 27 #FIXME: set the configuration id just once!
25 @kalibro_configuration_id = params[:kalibro_configuration_id] 28 @kalibro_configuration_id = params[:kalibro_configuration_id]
26 @metric_configuration.kalibro_configuration_id = @kalibro_configuration_id 29 @metric_configuration.kalibro_configuration_id = @kalibro_configuration_id
  30 + @reading_groups = ReadingGroup.public_or_owned_by_user(current_user).map { |reading_group|
  31 + [reading_group.name, reading_group.id]
  32 + }
27 end 33 end
28 34
29 def update 35 def update
app/controllers/projects_controller.rb
@@ -54,7 +54,6 @@ class ProjectsController &lt; ApplicationController @@ -54,7 +54,6 @@ class ProjectsController &lt; ApplicationController
54 # DELETE /project/1.json 54 # DELETE /project/1.json
55 def destroy 55 def destroy
56 set_project 56 set_project
57 - current_user.project_attributes.find_by_project_id!(@project.id).destroy  
58 @project.destroy 57 @project.destroy
59 respond_to do |format| 58 respond_to do |format|
60 format.html { redirect_to projects_url } 59 format.html { redirect_to projects_url }
app/controllers/reading_groups_controller.rb
@@ -13,7 +13,7 @@ class ReadingGroupsController &lt; ApplicationController @@ -13,7 +13,7 @@ class ReadingGroupsController &lt; ApplicationController
13 # GET /reading_groups 13 # GET /reading_groups
14 # GET /reading_groups.json 14 # GET /reading_groups.json
15 def index 15 def index
16 - @reading_groups = ReadingGroup.all 16 + @reading_groups = ReadingGroup.public_or_owned_by_user(current_user)
17 end 17 end
18 18
19 # POST /reading_groups 19 # POST /reading_groups
@@ -44,7 +44,6 @@ class ReadingGroupsController &lt; ApplicationController @@ -44,7 +44,6 @@ class ReadingGroupsController &lt; ApplicationController
44 # DELETE /reading_group/1 44 # DELETE /reading_group/1
45 # DELETE /reading_group/1.json 45 # DELETE /reading_group/1.json
46 def destroy 46 def destroy
47 - current_user.reading_group_ownerships.find_by_reading_group_id!(@reading_group.id).destroy  
48 @reading_group.destroy 47 @reading_group.destroy
49 respond_to do |format| 48 respond_to do |format|
50 format.html { redirect_to reading_groups_url } 49 format.html { redirect_to reading_groups_url }
@@ -68,7 +67,7 @@ class ReadingGroupsController &lt; ApplicationController @@ -68,7 +67,7 @@ class ReadingGroupsController &lt; ApplicationController
68 # Extracted code from create action 67 # Extracted code from create action
69 def create_and_redir(format) 68 def create_and_redir(format)
70 if @reading_group.save 69 if @reading_group.save
71 - current_user.reading_group_ownerships.create reading_group_id: @reading_group.id 70 + current_user.reading_group_attributes.create(reading_group_id: @reading_group.id)
72 71
73 format.html { redirect_to reading_group_path(@reading_group.id), notice: t('successfully_created', :record => t(@reading_group.class)) } 72 format.html { redirect_to reading_group_path(@reading_group.id), notice: t('successfully_created', :record => t(@reading_group.class)) }
74 format.json { render action: 'show', status: :created, location: @reading_group } 73 format.json { render action: 'show', status: :created, location: @reading_group }
app/controllers/repositories_controller.rb
@@ -5,6 +5,7 @@ class RepositoriesController &lt; ApplicationController @@ -5,6 +5,7 @@ class RepositoriesController &lt; ApplicationController
5 before_action :project_owner?, only: [:new, :create] 5 before_action :project_owner?, only: [:new, :create]
6 before_action :repository_owner?, only: [:edit, :update, :destroy, :process_repository] 6 before_action :repository_owner?, only: [:edit, :update, :destroy, :process_repository]
7 before_action :set_repository, only: [:show, :edit, :update, :destroy, :state, :state_with_date, :process_repository] 7 before_action :set_repository, only: [:show, :edit, :update, :destroy, :state, :state_with_date, :process_repository]
  8 + before_action :set_project_id_repository_types_and_configurations, only: [:new, :edit]
8 9
9 # GET /projects/1/repositories/1 10 # GET /projects/1/repositories/1
10 # GET /projects/1/repositories/1.json 11 # GET /projects/1/repositories/1.json
@@ -16,16 +17,11 @@ class RepositoriesController &lt; ApplicationController @@ -16,16 +17,11 @@ class RepositoriesController &lt; ApplicationController
16 17
17 # GET projects/1/repositories/new 18 # GET projects/1/repositories/new
18 def new 19 def new
19 - @project_id = params[:project_id]  
20 @repository = Repository.new 20 @repository = Repository.new
21 - @repository_types = Repository.repository_types  
22 end 21 end
23 22
24 # GET /repositories/1/edit 23 # GET /repositories/1/edit
25 - def edit  
26 - @project_id = params[:project_id]  
27 - @repository_types = Repository.repository_types  
28 - end 24 + def edit; end
29 25
30 # POST /projects/1/repositories 26 # POST /projects/1/repositories
31 # POST /projects/1/repositories.json 27 # POST /projects/1/repositories.json
@@ -89,10 +85,17 @@ class RepositoriesController &lt; ApplicationController @@ -89,10 +85,17 @@ class RepositoriesController &lt; ApplicationController
89 end 85 end
90 86
91 private 87 private
92 - # Duplicated code on create and update actions extracted here  
93 - def failed_action(format, destiny_action) 88 + def set_project_id_repository_types_and_configurations
94 @project_id = params[:project_id] 89 @project_id = params[:project_id]
95 @repository_types = Repository.repository_types 90 @repository_types = Repository.repository_types
  91 + @configurations = KalibroConfiguration.public_or_owned_by_user(current_user).map { |conf|
  92 + [conf.name, conf.id]
  93 + }
  94 + end
  95 +
  96 + # Duplicated code on create and update actions extracted here
  97 + def failed_action(format, destiny_action)
  98 + set_project_id_repository_types_and_configurations
96 99
97 format.html { render action: destiny_action } 100 format.html { render action: destiny_action }
98 format.json { render json: @repository.errors, status: :unprocessable_entity } 101 format.json { render json: @repository.errors, status: :unprocessable_entity }
app/helpers/kalibro_configurations_helper.rb
1 module KalibroConfigurationsHelper 1 module KalibroConfigurationsHelper
2 def kalibro_configuration_owner?(kalibro_configuration_id) 2 def kalibro_configuration_owner?(kalibro_configuration_id)
3 - user_signed_in? && !current_user.kalibro_configuration_ownerships.find_by_kalibro_configuration_id(kalibro_configuration_id).nil? 3 + user_signed_in? && !current_user.kalibro_configuration_attributes.find_by_kalibro_configuration_id(kalibro_configuration_id).nil?
4 end 4 end
5 5
6 def link_to_edit_form(metric_configuration, kalibro_configuration_id) 6 def link_to_edit_form(metric_configuration, kalibro_configuration_id)
app/helpers/reading_groups_helper.rb
1 module ReadingGroupsHelper 1 module ReadingGroupsHelper
2 def reading_groups_owner? reading_group_id 2 def reading_groups_owner? reading_group_id
3 - user_signed_in? && !current_user.reading_group_ownerships.find_by_reading_group_id(reading_group_id).nil? 3 + user_signed_in? && !current_user.reading_group_attributes.find_by_reading_group_id(reading_group_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/kalibro_configuration.rb
1 class KalibroConfiguration < KalibroClient::Entities::Configurations::KalibroConfiguration 1 class KalibroConfiguration < KalibroClient::Entities::Configurations::KalibroConfiguration
2 include KalibroRecord 2 include KalibroRecord
  3 + attr_writer :attributes
  4 +
  5 + def self.public_or_owned_by_user(user=nil)
  6 + kalibro_configuration_attributes = KalibroConfigurationAttributes.where(public: true)
  7 + kalibro_configuration_attributes += KalibroConfigurationAttributes.where(user_id: user.id, public: false) if user
  8 +
  9 + kalibro_configuration_attributes.map { |kalibro_configuration_attribute|
  10 + begin
  11 + self.find(kalibro_configuration_attribute.kalibro_configuration_id)
  12 + rescue KalibroClient::Errors::RecordNotFound
  13 + nil
  14 + end
  15 + }.compact
  16 + end
  17 +
  18 + def self.public
  19 + self.public_or_owned_by_user
  20 + end
  21 +
  22 + def attributes
  23 + @attributes ||= KalibroConfigurationAttributes.find_by(kalibro_configuration_id: self.id)
  24 + end
  25 +
  26 + def destroy
  27 + attributes.destroy unless attributes.nil?
  28 + @attributes = nil
  29 + super
  30 + end
3 end 31 end
app/models/kalibro_configuration_attributes.rb 0 → 100644
@@ -0,0 +1,13 @@ @@ -0,0 +1,13 @@
  1 +class KalibroConfigurationAttributes < ActiveRecord::Base
  2 + belongs_to :user
  3 + validates :kalibro_configuration_id, presence: true
  4 +
  5 + def kalibro_configuration
  6 + @kalibro_configuration ||= KalibroConfiguration.find(kalibro_configuration_id)
  7 + end
  8 +
  9 + def kalibro_configuration=(cfg)
  10 + @kalibro_configuration = cfg
  11 + self.kalibro_configuration_id = cfg.id
  12 + end
  13 +end
app/models/kalibro_configuration_ownership.rb
@@ -1,4 +0,0 @@ @@ -1,4 +0,0 @@
1 -class KalibroConfigurationOwnership < ActiveRecord::Base  
2 - belongs_to :user  
3 - validates :kalibro_configuration_id, presence: true  
4 -end  
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 3
  4 + attr_writer :attributes
  5 +
4 def self.latest(count = 1) 6 def self.latest(count = 1)
5 all.sort { |a,b| b.id <=> a.id }.select { |project| !project.attributes.hidden}.first(count) 7 all.sort { |a,b| b.id <=> a.id }.select { |project| !project.attributes.hidden}.first(count)
6 end 8 end
7 9
8 def attributes 10 def attributes
9 - project_attributes = ProjectAttributes.find_by_project_id(self.id)  
10 - project_attributes.nil? ? ProjectAttributes.new : project_attributes 11 + @project_attributes ||= ProjectAttributes.find_by_project_id(self.id)
  12 + @project_attributes.nil? ? ProjectAttributes.new : @project_attributes
  13 + end
  14 +
  15 + def destroy
  16 + self.attributes.destroy if self.attributes
  17 + @project_attributes = nil
  18 + super
11 end 19 end
12 end 20 end
app/models/reading_group.rb
1 class ReadingGroup < KalibroClient::Entities::Configurations::ReadingGroup 1 class ReadingGroup < KalibroClient::Entities::Configurations::ReadingGroup
2 include KalibroRecord 2 include KalibroRecord
  3 + attr_writer :attributes
  4 +
  5 + def self.public_or_owned_by_user(user=nil)
  6 + reading_group_attributes = ReadingGroupAttributes.where(public: true)
  7 + reading_group_attributes += ReadingGroupAttributes.where(user_id: user.id, public: false) if user
  8 +
  9 + reading_group_attributes.map { |reading_group_attribute|
  10 + begin
  11 + self.find(reading_group_attribute.reading_group_id)
  12 + rescue KalibroClient::Errors::RecordNotFound
  13 + nil
  14 + end
  15 + }.compact
  16 + end
  17 +
  18 + def self.public
  19 + self.public_or_owned_by_user
  20 + end
  21 +
  22 + def attributes
  23 + @attributes ||= ReadingGroupAttributes.find_by(reading_group_id: self.id)
  24 + end
  25 +
  26 + def destroy
  27 + attributes.destroy unless attributes.nil?
  28 + @attributes = nil
  29 + super
  30 + end
3 end 31 end
app/models/reading_group_attributes.rb 0 → 100644
@@ -0,0 +1,13 @@ @@ -0,0 +1,13 @@
  1 +class ReadingGroupAttributes < ActiveRecord::Base
  2 + belongs_to :user
  3 + validates :reading_group_id, presence: true
  4 +
  5 + def reading_group
  6 + @reading_group ||= ReadingGroup.find(reading_group_id)
  7 + end
  8 +
  9 + def reading_group=(group)
  10 + @reading_group = group
  11 + self.reading_group_id = group.id
  12 + end
  13 +end
app/models/reading_group_ownership.rb
@@ -1,4 +0,0 @@ @@ -1,4 +0,0 @@
1 -class ReadingGroupOwnership < ActiveRecord::Base  
2 - belongs_to :user  
3 - validates :reading_group_id, presence: true  
4 -end  
app/models/user.rb
@@ -9,8 +9,8 @@ class User &lt; ActiveRecord::Base @@ -9,8 +9,8 @@ class User &lt; ActiveRecord::Base
9 validates :email, uniqueness: true 9 validates :email, uniqueness: true
10 10
11 has_many :project_attributes, class_name: 'ProjectAttributes' 11 has_many :project_attributes, class_name: 'ProjectAttributes'
12 - has_many :reading_group_ownerships  
13 - has_many :kalibro_configuration_ownerships 12 + has_many :reading_group_attributes, class_name: 'ReadingGroupAttributes'
  13 + has_many :kalibro_configuration_attributes, class_name: 'KalibroConfigurationAttributes'
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
app/views/kalibro_configurations/_form.html.erb
@@ -17,7 +17,6 @@ @@ -17,7 +17,6 @@
17 <%= f.text_area :description, class: 'text-area form-control' %> 17 <%= f.text_area :description, class: 'text-area form-control' %>
18 </div> 18 </div>
19 </div> 19 </div>
20 -  
21 </div> 20 </div>
22 </div> 21 </div>
23 22
app/views/reading_groups/_form.html.erb
@@ -3,7 +3,6 @@ @@ -3,7 +3,6 @@
3 3
4 <div class="row margin-left-none"> 4 <div class="row margin-left-none">
5 <div class="form-table col-md-9"> 5 <div class="form-table col-md-9">
6 -  
7 <div class="form-row"> 6 <div class="form-row">
8 <div class="field-container"> 7 <div class="field-container">
9 <%= f.label :name, class: 'control-label' %> 8 <%= f.label :name, class: 'control-label' %>
app/views/repositories/_form.html.erb
@@ -79,8 +79,7 @@ @@ -79,8 +79,7 @@
79 <div class="form-row"> 79 <div class="form-row">
80 <div class="field-container"> 80 <div class="field-container">
81 <%= f.label :kalibro_configuration_id, KalibroConfiguration.model_name.human, class: 'control-label' %> 81 <%= f.label :kalibro_configuration_id, KalibroConfiguration.model_name.human, class: 'control-label' %>
82 - <% configuration_list = KalibroClient::Entities::Configurations::KalibroConfiguration.all.map { |conf| [conf.name, conf.id] } %>  
83 - <%= f.select( :kalibro_configuration_id, configuration_list, class: 'tooltip-control' ) %> 82 + <%= f.select( :kalibro_configuration_id, @configurations, class: 'tooltip-control' ) %>
84 </div> 83 </div>
85 <div class="help-container"> 84 <div class="help-container">
86 <p> 85 <p>
db/migrate/20140124124835_create_mezuro_configuration_ownerships.rb
1 -class CreateKalibroConfigurationOwnerships < ActiveRecord::Migration 1 +class CreateMezuroConfigurationOwnerships < ActiveRecord::Migration
2 def change 2 def change
3 - create_table :kalibro_configuration_ownerships do |t| 3 + create_table :mezuro_configuration_ownerships do |t|
4 t.integer :user_id 4 t.integer :user_id
5 - t.integer :kalibro_configuration_id 5 + t.integer :mezuro_configuration_id
6 6
7 t.timestamps 7 t.timestamps
8 end 8 end
db/migrate/20150511180712_rename_kalibro_configuration_ownerships_to_kalibro_confiugration_attributes.rb 0 → 100644
@@ -0,0 +1,6 @@ @@ -0,0 +1,6 @@
  1 +class RenameKalibroConfigurationOwnershipsToKalibroConfiugrationAttributes < ActiveRecord::Migration
  2 + def change
  3 + rename_table :kalibro_configuration_ownerships, :kalibro_configuration_attributes
  4 + add_column :kalibro_configuration_attributes, :public, :boolean, default: false
  5 + end
  6 +end
db/migrate/20150511181035_rename_reading_group_ownerships_to_reading_group_attributes.rb 0 → 100644
@@ -0,0 +1,6 @@ @@ -0,0 +1,6 @@
  1 +class RenameReadingGroupOwnershipsToReadingGroupAttributes < ActiveRecord::Migration
  2 + def change
  3 + rename_table :reading_group_ownerships, :reading_group_attributes
  4 + add_column :reading_group_attributes, :public, :boolean, default: false
  5 + end
  6 +end
db/migrate/20150515193445_change_kalibro_configuration_attributes_public_default.rb 0 → 100644
@@ -0,0 +1,9 @@ @@ -0,0 +1,9 @@
  1 +class ChangeKalibroConfigurationAttributesPublicDefault < ActiveRecord::Migration
  2 + def up
  3 + change_column_default :kalibro_configuration_attributes, :public, true
  4 + end
  5 +
  6 + def down
  7 + change_column_default :kalibro_configuration_attributes, :public, false
  8 + end
  9 +end
db/migrate/20150515195059_change_reading_group_attributes_public_default.rb 0 → 100644
@@ -0,0 +1,9 @@ @@ -0,0 +1,9 @@
  1 +class ChangeReadingGroupAttributesPublicDefault < ActiveRecord::Migration
  2 + def up
  3 + change_column_default :reading_group_attributes, :public, true
  4 + end
  5 +
  6 + def down
  7 + change_column_default :reading_group_attributes, :public, false
  8 + end
  9 +end
@@ -11,13 +11,14 @@ @@ -11,13 +11,14 @@
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: 20150225170704) do 14 +ActiveRecord::Schema.define(version: 20150515195059) do
15 15
16 - create_table "kalibro_configuration_ownerships", force: :cascade do |t| 16 + create_table "kalibro_configuration_attributes", force: :cascade do |t|
17 t.integer "user_id" 17 t.integer "user_id"
18 t.integer "kalibro_configuration_id" 18 t.integer "kalibro_configuration_id"
19 t.datetime "created_at" 19 t.datetime "created_at"
20 t.datetime "updated_at" 20 t.datetime "updated_at"
  21 + t.boolean "public", default: true
21 end 22 end
22 23
23 create_table "project_attributes", force: :cascade do |t| 24 create_table "project_attributes", force: :cascade do |t|
@@ -29,11 +30,12 @@ ActiveRecord::Schema.define(version: 20150225170704) do @@ -29,11 +30,12 @@ ActiveRecord::Schema.define(version: 20150225170704) do
29 t.datetime "updated_at", null: false 30 t.datetime "updated_at", null: false
30 end 31 end
31 32
32 - create_table "reading_group_ownerships", force: :cascade do |t| 33 + create_table "reading_group_attributes", force: :cascade do |t|
33 t.integer "user_id" 34 t.integer "user_id"
34 t.integer "reading_group_id" 35 t.integer "reading_group_id"
35 t.datetime "created_at" 36 t.datetime "created_at"
36 t.datetime "updated_at" 37 t.datetime "updated_at"
  38 + t.boolean "public", default: true
37 end 39 end
38 40
39 create_table "users", force: :cascade do |t| 41 create_table "users", force: :cascade do |t|
features/kalibro_configuration/listing.feature
@@ -27,4 +27,16 @@ Feature: Configuration listing @@ -27,4 +27,16 @@ Feature: Configuration listing
27 And I have a sample configuration 27 And I have a sample configuration
28 And I am at the All Configurations page 28 And I am at the All Configurations page
29 When I click the Show link 29 When I click the Show link
30 - Then the sample configuration should be there  
31 \ No newline at end of file 30 \ No newline at end of file
  31 + Then the sample configuration should be there
  32 +
  33 + @kalibro_configuration_restart
  34 + Scenario: Should show only the public or owned configurations
  35 + Given I am a regular user
  36 + And I am signed in
  37 + And I have a sample configuration
  38 + And there is a public configuration created
  39 + And there is a private configuration created
  40 + When I am at the All Configurations page
  41 + Then the sample configuration should be there
  42 + And the public configuration should be there
  43 + And the private configuration should not be there
features/reading_group/listing.feature 0 → 100644
@@ -0,0 +1,33 @@ @@ -0,0 +1,33 @@
  1 +Feature: Reading Group
  2 + In Order to be see the reading groups
  3 + As a regular user
  4 + I should be able to see the public and my own reading groups
  5 +
  6 + Scenario: Not logged in and no Reading Groups
  7 + Given I am at the homepage
  8 + When I click the Reading Group link
  9 + Then I should see "Reading Groups"
  10 + And I should see "Name"
  11 + And I should see "Description"
  12 + And I should see "You must be logged in to Create Reading Group"
  13 +
  14 + @kalibro_configuration_restart @javascript
  15 + Scenario: Logged in, should list Reading Groups
  16 + Given I am a regular user
  17 + And I am signed in
  18 + And I own a sample reading group
  19 + When I am at the All Reading Groups page
  20 + Then the sample reading group should be there
  21 + And I should not see "You must be logged in to Create Reading Group"
  22 +
  23 + @kalibro_configuration_restart
  24 + Scenario: Should show only the public or owned reading groups
  25 + Given I am a regular user
  26 + And I am signed in
  27 + And I own a sample reading group
  28 + And there is a public reading group created
  29 + And there is a private reading group created
  30 + When I am at the All Reading Groups page
  31 + Then the sample reading group should be there
  32 + And the public reading group should be there
  33 + And the private reading group should not be there
features/step_definitions/kalibro_configuration_steps.rb
@@ -12,11 +12,13 @@ end @@ -12,11 +12,13 @@ end
12 12
13 Given(/^I have a sample configuration$/) do 13 Given(/^I have a sample configuration$/) do
14 @kalibro_configuration = FactoryGirl.create(:kalibro_configuration) 14 @kalibro_configuration = FactoryGirl.create(:kalibro_configuration)
  15 + FactoryGirl.create(:kalibro_configuration_attributes, user_id: FactoryGirl.create(:another_user).id, kalibro_configuration_id: @kalibro_configuration.id)
  16 +
15 end 17 end
16 18
17 Given(/^I own a sample configuration$/) do 19 Given(/^I own a sample configuration$/) do
18 @kalibro_configuration = FactoryGirl.create(:kalibro_configuration) 20 @kalibro_configuration = FactoryGirl.create(:kalibro_configuration)
19 - FactoryGirl.create(:kalibro_configuration_ownership, {id: nil, user_id: @user.id, kalibro_configuration_id: @kalibro_configuration.id}) 21 + FactoryGirl.create(:kalibro_configuration_attributes, {id: nil, user_id: @user.id, kalibro_configuration_id: @kalibro_configuration.id})
20 end 22 end
21 23
22 Given(/^I am at the Sample Configuration page$/) do 24 Given(/^I am at the Sample Configuration page$/) do
@@ -29,7 +31,7 @@ end @@ -29,7 +31,7 @@ end
29 31
30 Given(/^I own a configuration named "(.*?)"$/) do |name| 32 Given(/^I own a configuration named "(.*?)"$/) do |name|
31 @kalibro_configuration = FactoryGirl.create(:kalibro_configuration, {name: name}) 33 @kalibro_configuration = FactoryGirl.create(:kalibro_configuration, {name: name})
32 - FactoryGirl.create(:kalibro_configuration_ownership, {id: nil, user_id: @user.id, kalibro_configuration_id: @kalibro_configuration.id}) 34 + FactoryGirl.create(:kalibro_configuration_attributes, {id: nil, user_id: @user.id, kalibro_configuration_id: @kalibro_configuration.id})
33 end 35 end
34 36
35 When(/^I visit the sample configuration edit page$/) do 37 When(/^I visit the sample configuration edit page$/) do
@@ -49,6 +51,7 @@ Then(/^I should be in the All configurations page$/) do @@ -49,6 +51,7 @@ Then(/^I should be in the All configurations page$/) do
49 end 51 end
50 52
51 Then(/^the sample configuration should not be there$/) do 53 Then(/^the sample configuration should not be there$/) do
  54 + expect(@kalibro_configuration.attributes).to be_nil
52 expect { KalibroConfiguration.find(@kalibro_configuration.id) }.to raise_error 55 expect { KalibroConfiguration.find(@kalibro_configuration.id) }.to raise_error
53 end 56 end
54 57
@@ -56,3 +59,24 @@ Then(/^the sample configuration should be there$/) do @@ -56,3 +59,24 @@ Then(/^the sample configuration should be there$/) do
56 expect(page).to have_content(@kalibro_configuration.name) 59 expect(page).to have_content(@kalibro_configuration.name)
57 expect(page).to have_content(@kalibro_configuration.description) 60 expect(page).to have_content(@kalibro_configuration.description)
58 end 61 end
  62 +
  63 +Given(/^there is a public configuration created$/) do
  64 + @public_kc = FactoryGirl.create(:public_kalibro_configuration)
  65 + FactoryGirl.create(:kalibro_configuration_attributes, kalibro_configuration_id: @public_kc.id)
  66 +end
  67 +
  68 +Given(/^there is a private configuration created$/) do
  69 + @private_kc = FactoryGirl.create(:another_kalibro_configuration)
  70 + FactoryGirl.create(:kalibro_configuration_attributes, :private, kalibro_configuration_id: @private_kc.id, user: FactoryGirl.create(:another_user, id: nil, email: "private@email.com"))
  71 +end
  72 +
  73 +Then(/^the public configuration should be there$/) do
  74 + expect(page).to have_content(@public_kc.name)
  75 + expect(page).to have_content(@public_kc.description)
  76 +end
  77 +
  78 +Then(/^the private configuration should not be there$/) do
  79 + expect(page).to have_no_content(@private_kc.name)
  80 + expect(page).to have_no_content(@private_kc.description)
  81 +end
  82 +
features/step_definitions/reading_group_steps.rb
@@ -14,7 +14,7 @@ end @@ -14,7 +14,7 @@ end
14 14
15 Given(/^I own a sample reading group$/) do 15 Given(/^I own a sample reading group$/) do
16 @reading_group = FactoryGirl.create(:reading_group) 16 @reading_group = FactoryGirl.create(:reading_group)
17 - FactoryGirl.create(:reading_group_ownership, {user_id: @user.id, reading_group_id: @reading_group.id}) 17 + FactoryGirl.create(:reading_group_attributes, user_id: @user.id, reading_group_id: @reading_group.id)
18 end 18 end
19 19
20 Given(/^I have a sample reading group$/) do 20 Given(/^I have a sample reading group$/) do
@@ -31,7 +31,7 @@ end @@ -31,7 +31,7 @@ end
31 31
32 Given(/^I own a reading group named "(.*?)"$/) do |name| 32 Given(/^I own a reading group named "(.*?)"$/) do |name|
33 @reading_group = FactoryGirl.create(:reading_group, {name: name}) 33 @reading_group = FactoryGirl.create(:reading_group, {name: name})
34 - FactoryGirl.create(:reading_group_ownership, {user_id: @user.id, reading_group_id: @reading_group.id}) 34 + FactoryGirl.create(:reading_group_attributes, {user_id: @user.id, reading_group_id: @reading_group.id})
35 end 35 end
36 36
37 When(/^I visit the sample reading group edit page$/) do 37 When(/^I visit the sample reading group edit page$/) do
@@ -61,5 +61,31 @@ Then(/^I should be in the Edit Reading Group page$/) do @@ -61,5 +61,31 @@ Then(/^I should be in the Edit Reading Group page$/) do
61 end 61 end
62 62
63 Then(/^the Sample Reading Group should not be there$/) do 63 Then(/^the Sample Reading Group should not be there$/) do
  64 + expect(@reading_group.attributes).to be_nil
64 expect { ReadingGroup.find(@reading_group.id) }.to raise_error 65 expect { ReadingGroup.find(@reading_group.id) }.to raise_error
65 end 66 end
  67 +
  68 +Then(/^the sample reading group should be there$/) do
  69 + expect(page).to have_content(@reading_group.name)
  70 + expect(page).to have_content(@reading_group.description)
  71 +end
  72 +
  73 +Given(/^there is a public reading group created$/) do
  74 + @public_rg = FactoryGirl.create(:public_reading_group)
  75 + FactoryGirl.create(:reading_group_attributes, reading_group_id: @public_rg.id)
  76 +end
  77 +
  78 +Given(/^there is a private reading group created$/) do
  79 + @private_rg = FactoryGirl.create(:another_reading_group)
  80 + FactoryGirl.create(:reading_group_attributes, :private, reading_group_id: @private_rg.id, user: FactoryGirl.create(:another_user))
  81 +end
  82 +
  83 +Then(/^the public reading group should be there$/) do
  84 + expect(page).to have_content(@public_rg.name)
  85 + expect(page).to have_content(@public_rg.description)
  86 +end
  87 +
  88 +Then(/^the private reading group should not be there$/) do
  89 + expect(page).to have_no_content(@private_rg.name)
  90 + expect(page).to have_no_content(@private_rg.description)
  91 +end
features/step_definitions/repository_steps.rb
@@ -13,6 +13,8 @@ Given(/^I have a sample configuration with native metrics$/) do @@ -13,6 +13,8 @@ Given(/^I have a sample configuration with native metrics$/) do
13 reading = FactoryGirl.create(:reading, {reading_group_id: reading_group.id}) 13 reading = FactoryGirl.create(:reading, {reading_group_id: reading_group.id})
14 14
15 @kalibro_configuration = FactoryGirl.create(:kalibro_configuration) 15 @kalibro_configuration = FactoryGirl.create(:kalibro_configuration)
  16 + FactoryGirl.create(:kalibro_configuration_attributes, {id: nil, user_id: nil, kalibro_configuration_id: @kalibro_configuration.id})
  17 +
16 metric_configuration = FactoryGirl.create(:metric_configuration, 18 metric_configuration = FactoryGirl.create(:metric_configuration,
17 {metric: FactoryGirl.build(:loc), 19 {metric: FactoryGirl.build(:loc),
18 reading_group_id: reading_group.id, 20 reading_group_id: reading_group.id,
spec/controllers/base_metric_configurations_controller_spec.rb
@@ -41,7 +41,7 @@ end @@ -41,7 +41,7 @@ end
41 41
42 42
43 describe InheritsFromBaseMetricConfigurationsController, :type => :controller do 43 describe InheritsFromBaseMetricConfigurationsController, :type => :controller do
44 - let(:kalibro_configuration) { FactoryGirl.build(:kalibro_configuration_with_id) } 44 + let(:kalibro_configuration) { FactoryGirl.build(:kalibro_configuration, :with_id) }
45 45
46 before do 46 before do
47 Rails.application.routes.draw do 47 Rails.application.routes.draw do
@@ -111,7 +111,7 @@ describe InheritsFromBaseMetricConfigurationsController, :type =&gt; :controller do @@ -111,7 +111,7 @@ describe InheritsFromBaseMetricConfigurationsController, :type =&gt; :controller do
111 111
112 describe 'show' do 112 describe 'show' do
113 let(:metric_configuration) { FactoryGirl.build(:metric_configuration_with_id) } 113 let(:metric_configuration) { FactoryGirl.build(:metric_configuration_with_id) }
114 - let(:reading_group) { FactoryGirl.build(:reading_group_with_id) } 114 + let(:reading_group) { FactoryGirl.build(:reading_group, :with_id) }
115 let(:kalibro_range) { FactoryGirl.build(:kalibro_range) } 115 let(:kalibro_range) { FactoryGirl.build(:kalibro_range) }
116 116
117 context 'with a valid metric_configuration' do 117 context 'with a valid metric_configuration' do
spec/controllers/compound_metric_configurations_controller_spec.rb
1 require 'rails_helper' 1 require 'rails_helper'
2 2
3 describe CompoundMetricConfigurationsController, :type => :controller do 3 describe CompoundMetricConfigurationsController, :type => :controller do
4 - let(:kalibro_configuration) { FactoryGirl.build(:kalibro_configuration_with_id) } 4 + let(:kalibro_configuration) { FactoryGirl.build(:kalibro_configuration, :with_id) }
5 5
6 describe 'new' do 6 describe 'new' do
7 before :each do 7 before :each do
@@ -69,7 +69,7 @@ describe CompoundMetricConfigurationsController, :type =&gt; :controller do @@ -69,7 +69,7 @@ describe CompoundMetricConfigurationsController, :type =&gt; :controller do
69 69
70 describe 'show' do 70 describe 'show' do
71 let(:compound_metric_configuration) { FactoryGirl.build(:compound_metric_configuration_with_id) } 71 let(:compound_metric_configuration) { FactoryGirl.build(:compound_metric_configuration_with_id) }
72 - let(:reading_group) { FactoryGirl.build(:reading_group_with_id) } 72 + let(:reading_group) { FactoryGirl.build(:reading_group, :with_id) }
73 let(:kalibro_range) { FactoryGirl.build(:kalibro_range) } 73 let(:kalibro_range) { FactoryGirl.build(:kalibro_range) }
74 74
75 before :each do 75 before :each do
spec/controllers/concerns/ownership_authentication_spec.rb
@@ -4,7 +4,7 @@ describe OwnershipAuthentication, type: :controller do @@ -4,7 +4,7 @@ describe OwnershipAuthentication, type: :controller do
4 #TODO: test other methods 4 #TODO: test other methods
5 5
6 describe 'reading_group_owner?' do 6 describe 'reading_group_owner?' do
7 - let(:reading_group) { FactoryGirl.build(:reading_group_with_id) } 7 + let(:reading_group) { FactoryGirl.build(:reading_group, :with_id) }
8 8
9 context 'Not ReadingGroupsController nor ReadingsController' do 9 context 'Not ReadingGroupsController nor ReadingsController' do
10 let!(:projects_controller) { ProjectsController.new } 10 let!(:projects_controller) { ProjectsController.new }
@@ -34,12 +34,12 @@ describe OwnershipAuthentication, type: :controller do @@ -34,12 +34,12 @@ describe OwnershipAuthentication, type: :controller do
34 end 34 end
35 35
36 context 'when the user owns the ReadingGroup' do 36 context 'when the user owns the ReadingGroup' do
37 - let!(:reading_group_ownership) { FactoryGirl.build(:reading_group_ownership, {user_id: current_user.id, reading_group_id: reading_group.id}) } 37 + let!(:reading_group_attributes) { FactoryGirl.build(:reading_group_attributes, {user_id: current_user.id, reading_group_id: reading_group.id}) }
38 38
39 before do 39 before do
40 - reading_group_ownerships = Object.new  
41 - reading_group_ownerships.expects(:find_by_reading_group_id).with(reading_group.id).returns(reading_group_ownership)  
42 - current_user.expects(:reading_group_ownerships).returns(reading_group_ownerships) 40 + reading_group_attributes = Object.new
  41 + reading_group_attributes.expects(:find_by_reading_group_id).with(reading_group.id).returns(reading_group_attributes)
  42 + current_user.expects(:reading_group_attributes).returns(reading_group_attributes)
43 end 43 end
44 44
45 it 'should return true' do 45 it 'should return true' do
@@ -49,9 +49,9 @@ describe OwnershipAuthentication, type: :controller do @@ -49,9 +49,9 @@ describe OwnershipAuthentication, type: :controller do
49 49
50 context 'when the user does not own the ReadingGroup' do 50 context 'when the user does not own the ReadingGroup' do
51 before do 51 before do
52 - reading_group_ownerships = Object.new  
53 - reading_group_ownerships.expects(:find_by_reading_group_id).with(reading_group.id).returns(nil)  
54 - current_user.expects(:reading_group_ownerships).returns(reading_group_ownerships) 52 + reading_group_attributes = Object.new
  53 + reading_group_attributes.expects(:find_by_reading_group_id).with(reading_group.id).returns(nil)
  54 + current_user.expects(:reading_group_attributes).returns(reading_group_attributes)
55 end 55 end
56 56
57 it 'should respond' do # FIXME: this is not the best test, but it it's the closest we can do I think 57 it 'should respond' do # FIXME: this is not the best test, but it it's the closest we can do I think
@@ -65,7 +65,7 @@ describe OwnershipAuthentication, type: :controller do @@ -65,7 +65,7 @@ describe OwnershipAuthentication, type: :controller do
65 end 65 end
66 66
67 describe 'kalibro_configuration_owner?' do 67 describe 'kalibro_configuration_owner?' do
68 - let(:kalibro_configuration) { FactoryGirl.build(:kalibro_configuration_with_id) } 68 + let(:kalibro_configuration) { FactoryGirl.build(:kalibro_configuration, :with_id) }
69 69
70 context 'Not KalibroConfigurationsController nor MetricConfigurationsController nor CompoundMetricConfigurationsController' do 70 context 'Not KalibroConfigurationsController nor MetricConfigurationsController nor CompoundMetricConfigurationsController' do
71 let!(:projects_controller) { ProjectsController.new } 71 let!(:projects_controller) { ProjectsController.new }
spec/controllers/kalibro_configurations_controller_spec.rb
@@ -2,6 +2,14 @@ require &#39;rails_helper&#39; @@ -2,6 +2,14 @@ require &#39;rails_helper&#39;
2 2
3 describe KalibroConfigurationsController, :type => :controller do 3 describe KalibroConfigurationsController, :type => :controller do
4 4
  5 + def call_action(method)
  6 + if method == :update
  7 + post method, :id => kalibro_configuration.id, :kalibro_configuration => kalibro_configuration_params, :attributes => attributes
  8 + elsif method == :create
  9 + put method, :kalibro_configuration => kalibro_configuration_params, :attributes => attributes
  10 + end
  11 + end
  12 +
5 describe 'new' do 13 describe 'new' do
6 before :each do 14 before :each do
7 sign_in FactoryGirl.create(:user) 15 sign_in FactoryGirl.create(:user)
@@ -18,16 +26,17 @@ describe KalibroConfigurationsController, :type =&gt; :controller do @@ -18,16 +26,17 @@ describe KalibroConfigurationsController, :type =&gt; :controller do
18 end 26 end
19 27
20 context 'with valid fields' do 28 context 'with valid fields' do
21 - let(:kalibro_configuration) { FactoryGirl.build(:kalibro_configuration_with_id) }  
22 - let(:subject_params) { kalibro_configuration.to_hash } 29 + let(:kalibro_configuration) { FactoryGirl.build(:kalibro_configuration, :with_id) }
  30 + let(:kalibro_configuration_params) { kalibro_configuration.to_hash }
  31 + let(:attributes) { {public: "1"} }
23 32
24 - before :each do 33 + before :each do
25 KalibroConfiguration.any_instance.expects(:save).returns(true) 34 KalibroConfiguration.any_instance.expects(:save).returns(true)
26 end 35 end
27 36
28 context 'rendering the show' do 37 context 'rendering the show' do
29 before :each do 38 before :each do
30 - post :create, :kalibro_configuration => subject_params 39 + call_action :create
31 end 40 end
32 41
33 it 'should redirect to the show view' do 42 it 'should redirect to the show view' do
@@ -37,7 +46,7 @@ describe KalibroConfigurationsController, :type =&gt; :controller do @@ -37,7 +46,7 @@ describe KalibroConfigurationsController, :type =&gt; :controller do
37 46
38 context 'without rendering the show view' do 47 context 'without rendering the show view' do
39 before :each do 48 before :each do
40 - post :create, :kalibro_configuration => subject_params 49 + call_action :create
41 end 50 end
42 51
43 it { is_expected.to respond_with(:redirect) } 52 it { is_expected.to respond_with(:redirect) }
@@ -46,7 +55,7 @@ describe KalibroConfigurationsController, :type =&gt; :controller do @@ -46,7 +55,7 @@ describe KalibroConfigurationsController, :type =&gt; :controller do
46 55
47 context 'with an invalid field' do 56 context 'with an invalid field' do
48 before :each do 57 before :each do
49 - @subject = FactoryGirl.build(:kalibro_configuration_with_id) 58 + @subject = FactoryGirl.build(:kalibro_configuration, :with_id)
50 @subject_params = @subject.to_hash 59 @subject_params = @subject.to_hash
51 60
52 KalibroConfiguration.expects(:new).at_least_once.with(@subject_params).returns(@subject) 61 KalibroConfiguration.expects(:new).at_least_once.with(@subject_params).returns(@subject)
@@ -60,8 +69,8 @@ describe KalibroConfigurationsController, :type =&gt; :controller do @@ -60,8 +69,8 @@ describe KalibroConfigurationsController, :type =&gt; :controller do
60 end 69 end
61 70
62 describe 'show' do 71 describe 'show' do
63 - let(:kalibro_configuration) { FactoryGirl.build(:kalibro_configuration_with_id) }  
64 - let(:metric_configuration) { FactoryGirl.build(:metric_configuration_with_id) } 72 + let(:kalibro_configuration) { FactoryGirl.build(:kalibro_configuration, :with_id) }
  73 + let(:metric_configuration) { FactoryGirl.build(:metric_configuration_with_id) }
65 74
66 before :each do 75 before :each do
67 kalibro_configuration.expects(:metric_configurations).returns(metric_configuration) 76 kalibro_configuration.expects(:metric_configurations).returns(metric_configuration)
@@ -78,30 +87,22 @@ describe KalibroConfigurationsController, :type =&gt; :controller do @@ -78,30 +87,22 @@ describe KalibroConfigurationsController, :type =&gt; :controller do
78 end 87 end
79 88
80 describe 'destroy' do 89 describe 'destroy' do
81 - before do  
82 - @subject = FactoryGirl.build(:kalibro_configuration_with_id)  
83 - end 90 + let(:kalibro_configuration) { FactoryGirl.build(:kalibro_configuration, :with_id) }
84 91
85 context 'with an User logged in' do 92 context 'with an User logged in' do
  93 + let(:kalibro_configuration_attribute) { FactoryGirl.build(:kalibro_configuration_attributes) }
  94 +
86 before do 95 before do
87 sign_in FactoryGirl.create(:user) 96 sign_in FactoryGirl.create(:user)
88 - @ownership = FactoryGirl.build(:kalibro_configuration_ownership)  
89 - @ownerships = []  
90 end 97 end
91 98
92 context 'when the user owns the kalibro_configuration' do 99 context 'when the user owns the kalibro_configuration' do
93 before :each do 100 before :each do
94 - @ownership.expects(:destroy)  
95 - @subject.expects(:destroy)  
96 -  
97 - #Those two mocks looks the same but they are necessary since params[:id] is a String and @configuration.id is an Integer :(  
98 - @ownerships.expects(:find_by_kalibro_configuration_id).with("#{@subject.id}").returns(@ownership)  
99 - @ownerships.expects(:find_by_kalibro_configuration_id!).with(@subject.id).returns(@ownership) 101 + kalibro_configuration.expects(:destroy)
  102 + KalibroConfiguration.expects(:find).with(kalibro_configuration.id).returns(kalibro_configuration)
  103 + subject.expects(:kalibro_configuration_owner?)
100 104
101 - User.any_instance.expects(:kalibro_configuration_ownerships).at_least_once.returns(@ownerships)  
102 -  
103 - KalibroConfiguration.expects(:find).with(@subject.id).returns(@subject)  
104 - delete :destroy, :id => @subject.id 105 + delete :destroy, :id => kalibro_configuration.id
105 end 106 end
106 107
107 it 'should redirect to the kalibro_configurations page' do 108 it 'should redirect to the kalibro_configurations page' do
@@ -113,19 +114,16 @@ describe KalibroConfigurationsController, :type =&gt; :controller do @@ -113,19 +114,16 @@ describe KalibroConfigurationsController, :type =&gt; :controller do
113 114
114 context "when the user doesn't own the kalibro_configuration" do 115 context "when the user doesn't own the kalibro_configuration" do
115 before :each do 116 before :each do
116 - @ownerships.expects(:find_by_kalibro_configuration_id).with("#{@subject.id}").returns(nil)  
117 - User.any_instance.expects(:kalibro_configuration_ownerships).at_least_once.returns(@ownerships)  
118 -  
119 - delete :destroy, :id => @subject.id 117 + delete :destroy, :id => kalibro_configuration.id
120 end 118 end
121 119
122 - it { is_expected.to redirect_to(kalibro_configurations_path(id: @subject.id)) } 120 + it { is_expected.to redirect_to(kalibro_configurations_path(id: kalibro_configuration.id)) }
123 end 121 end
124 end 122 end
125 123
126 context 'with no User logged in' do 124 context 'with no User logged in' do
127 before :each do 125 before :each do
128 - delete :destroy, :id => @subject.id 126 + delete :destroy, :id => kalibro_configuration.id
129 end 127 end
130 128
131 it { is_expected.to redirect_to new_user_session_path } 129 it { is_expected.to redirect_to new_user_session_path }
@@ -133,9 +131,11 @@ describe KalibroConfigurationsController, :type =&gt; :controller do @@ -133,9 +131,11 @@ describe KalibroConfigurationsController, :type =&gt; :controller do
133 end 131 end
134 132
135 describe 'index' do 133 describe 'index' do
  134 + let!(:kalibro_configuration_attribute) { FactoryGirl.build(:kalibro_configuration_attributes) }
136 before :each do 135 before :each do
137 - @subject = FactoryGirl.build(:kalibro_configuration_with_id)  
138 - KalibroConfiguration.expects(:all).returns([@subject]) 136 + @subject = FactoryGirl.build(:kalibro_configuration, :with_id)
  137 + KalibroConfigurationAttributes.expects(:where).with(public: true).returns([kalibro_configuration_attribute])
  138 + KalibroConfiguration.expects(:find).with(kalibro_configuration_attribute.kalibro_configuration_id).returns(@subject)
139 get :index 139 get :index
140 end 140 end
141 141
@@ -143,52 +143,50 @@ describe KalibroConfigurationsController, :type =&gt; :controller do @@ -143,52 +143,50 @@ describe KalibroConfigurationsController, :type =&gt; :controller do
143 end 143 end
144 144
145 describe 'edit' do 145 describe 'edit' do
146 - before do  
147 - @subject = FactoryGirl.build(:kalibro_configuration_with_id)  
148 - end 146 + let!(:user) { FactoryGirl.create(:user) }
  147 + let(:kalibro_configuration) { FactoryGirl.build(:kalibro_configuration, :with_id) }
  148 + let(:kalibro_configuration_attribute) { FactoryGirl.build(:kalibro_configuration_attributes) }
  149 + let(:kalibro_configuration_attributes) { mock('kalibro_configuration_attributes') }
149 150
150 context 'with an User logged in' do 151 context 'with an User logged in' do
151 before do 152 before do
152 - @user = FactoryGirl.create(:user)  
153 - @ownership = FactoryGirl.build(:kalibro_configuration_ownership)  
154 - @ownerships = [] 153 + User.any_instance.expects(:kalibro_configuration_attributes).at_least_once.returns(kalibro_configuration_attributes)
155 154
156 - User.any_instance.expects(:kalibro_configuration_ownerships).at_least_once.returns(@ownerships)  
157 -  
158 - sign_in @user 155 + sign_in user
159 end 156 end
160 157
161 context 'when the user owns the kalibro_configuration' do 158 context 'when the user owns the kalibro_configuration' do
162 before :each do 159 before :each do
163 - KalibroConfiguration.expects(:find).with(@subject.id).returns(@subject)  
164 - @ownerships.expects(:find_by_kalibro_configuration_id).with("#{@subject.id}").returns(@ownership) 160 + KalibroConfiguration.expects(:find).with(kalibro_configuration.id).returns(kalibro_configuration)
  161 + kalibro_configuration_attributes.expects(:find_by_kalibro_configuration_id).with("#{kalibro_configuration.id}").returns(kalibro_configuration_attribute)
165 162
166 - get :edit, :id => @subject.id 163 + get :edit, :id => kalibro_configuration.id
167 end 164 end
168 165
169 it { is_expected.to render_template(:edit) } 166 it { is_expected.to render_template(:edit) }
170 167
171 - it 'should assign to @kalibro_configuration the @subject' do  
172 - expect(assigns(:kalibro_configuration)).to eq(@subject) 168 + it 'should assign to @kalibro_configuration the kalibro_configuration' do
  169 + expect(assigns(:kalibro_configuration)).to eq(kalibro_configuration)
173 end 170 end
174 end 171 end
175 172
176 context 'when the user does not own the kalibro_configuration' do 173 context 'when the user does not own the kalibro_configuration' do
177 - before do  
178 - @subject = FactoryGirl.build(:another_kalibro_configuration_with_id)  
179 - @ownerships.expects(:find_by_kalibro_configuration_id).with("#{@subject.id}").returns(nil) 174 + let!(:another_kalibro_configuration) { FactoryGirl.build(:another_kalibro_configuration, :with_id) }
  175 +
  176 + before :each do
  177 + kalibro_configuration_attributes.expects(:find_by_kalibro_configuration_id).with("#{another_kalibro_configuration.id}").returns(nil)
180 178
181 - get :edit, :id => @subject.id 179 + get :edit, :id => another_kalibro_configuration.id
182 end 180 end
183 181
184 - it { is_expected.to redirect_to(kalibro_configurations_path(id: @subject.id)) } 182 + it { is_expected.to redirect_to(kalibro_configurations_path(id: another_kalibro_configuration.id)) }
185 it { is_expected.to set_flash[:notice].to("You're not allowed to do this operation") } 183 it { is_expected.to set_flash[:notice].to("You're not allowed to do this operation") }
186 end 184 end
187 end 185 end
188 186
189 context 'with no user logged in' do 187 context 'with no user logged in' do
190 before :each do 188 before :each do
191 - get :edit, :id => @subject.id 189 + get :edit, :id => kalibro_configuration.id
192 end 190 end
193 191
194 it { is_expected.to redirect_to new_user_session_path } 192 it { is_expected.to redirect_to new_user_session_path }
@@ -196,8 +194,9 @@ describe KalibroConfigurationsController, :type =&gt; :controller do @@ -196,8 +194,9 @@ describe KalibroConfigurationsController, :type =&gt; :controller do
196 end 194 end
197 195
198 describe 'update' do 196 describe 'update' do
199 - let(:kalibro_configuration) {FactoryGirl.build(:kalibro_configuration_with_id)}  
200 - let(:kalibro_configuration_params) { kalibro_configuration.to_hash } 197 + let!(:kalibro_configuration) {FactoryGirl.build(:kalibro_configuration, :with_id)}
  198 + let!(:kalibro_configuration_params) { kalibro_configuration.to_hash }
  199 + let!(:attributes) { {public: "1"} }
201 200
202 context 'when the user is logged in' do 201 context 'when the user is logged in' do
203 before do 202 before do
@@ -205,12 +204,12 @@ describe KalibroConfigurationsController, :type =&gt; :controller do @@ -205,12 +204,12 @@ describe KalibroConfigurationsController, :type =&gt; :controller do
205 end 204 end
206 205
207 context 'when user owns the kalibro_configuration' do 206 context 'when user owns the kalibro_configuration' do
208 - before do  
209 - @ownership = FactoryGirl.build(:kalibro_configuration_ownership)  
210 - @ownerships = [] 207 + let(:kalibro_configuration_attribute) { FactoryGirl.build(:kalibro_configuration_attributes) }
  208 + let(:kalibro_configuration_attributes) { mock('kalibro_configuration_attributes') }
211 209
212 - @ownerships.expects(:find_by_kalibro_configuration_id).with("#{kalibro_configuration.id}").returns(@ownership)  
213 - User.any_instance.expects(:kalibro_configuration_ownerships).at_least_once.returns(@ownerships) 210 + before :each do
  211 + kalibro_configuration_attributes.expects(:find_by_kalibro_configuration_id).with("#{kalibro_configuration.id}").returns(kalibro_configuration_attribute)
  212 + User.any_instance.expects(:kalibro_configuration_attributes).at_least_once.returns(kalibro_configuration_attributes)
214 end 213 end
215 214
216 context 'with valid fields' do 215 context 'with valid fields' do
@@ -221,7 +220,7 @@ describe KalibroConfigurationsController, :type =&gt; :controller do @@ -221,7 +220,7 @@ describe KalibroConfigurationsController, :type =&gt; :controller do
221 220
222 context 'rendering the show' do 221 context 'rendering the show' do
223 before :each do 222 before :each do
224 - post :update, :id => kalibro_configuration.id, :kalibro_configuration => kalibro_configuration_params 223 + call_action :update
225 end 224 end
226 225
227 it 'should redirect to the show view' do 226 it 'should redirect to the show view' do
@@ -231,7 +230,7 @@ describe KalibroConfigurationsController, :type =&gt; :controller do @@ -231,7 +230,7 @@ describe KalibroConfigurationsController, :type =&gt; :controller do
231 230
232 context 'without rendering the show view' do 231 context 'without rendering the show view' do
233 before :each do 232 before :each do
234 - post :update, :id => kalibro_configuration.id, :kalibro_configuration => kalibro_configuration_params 233 + call_action :update
235 end 234 end
236 235
237 it { is_expected.to respond_with(:redirect) } 236 it { is_expected.to respond_with(:redirect) }
@@ -243,7 +242,7 @@ describe KalibroConfigurationsController, :type =&gt; :controller do @@ -243,7 +242,7 @@ describe KalibroConfigurationsController, :type =&gt; :controller do
243 KalibroConfiguration.expects(:find).with(kalibro_configuration.id).returns(kalibro_configuration) 242 KalibroConfiguration.expects(:find).with(kalibro_configuration.id).returns(kalibro_configuration)
244 KalibroConfiguration.any_instance.expects(:update).with(kalibro_configuration_params).returns(false) 243 KalibroConfiguration.any_instance.expects(:update).with(kalibro_configuration_params).returns(false)
245 244
246 - post :update, :id => kalibro_configuration.id, :kalibro_configuration => kalibro_configuration_params 245 + call_action :update
247 end 246 end
248 247
249 it { is_expected.to render_template(:edit) } 248 it { is_expected.to render_template(:edit) }
@@ -252,7 +251,7 @@ describe KalibroConfigurationsController, :type =&gt; :controller do @@ -252,7 +251,7 @@ describe KalibroConfigurationsController, :type =&gt; :controller do
252 251
253 context 'when the user does not own the kalibro_configuration' do 252 context 'when the user does not own the kalibro_configuration' do
254 before :each do 253 before :each do
255 - post :update, :id => kalibro_configuration.id, :kalibro_configuration => kalibro_configuration_params 254 + call_action :update
256 end 255 end
257 256
258 it { is_expected.to redirect_to kalibro_configurations_path(id: kalibro_configuration.id) } 257 it { is_expected.to redirect_to kalibro_configurations_path(id: kalibro_configuration.id) }
@@ -261,7 +260,7 @@ describe KalibroConfigurationsController, :type =&gt; :controller do @@ -261,7 +260,7 @@ describe KalibroConfigurationsController, :type =&gt; :controller do
261 260
262 context 'with no user logged in' do 261 context 'with no user logged in' do
263 before :each do 262 before :each do
264 - post :update, :id => kalibro_configuration.id, :kalibro_configuration => kalibro_configuration_params 263 + call_action :update
265 end 264 end
266 265
267 it { is_expected.to redirect_to new_user_session_path } 266 it { is_expected.to redirect_to new_user_session_path }
spec/controllers/kalibro_ranges_controller_spec.rb
@@ -5,7 +5,7 @@ describe KalibroRangesController, :type =&gt; :controller do @@ -5,7 +5,7 @@ describe KalibroRangesController, :type =&gt; :controller do
5 let(:kalibro_range) { FactoryGirl.build(:kalibro_range_with_id, metric_configuration_id: metric_configuration.id) } 5 let(:kalibro_range) { FactoryGirl.build(:kalibro_range_with_id, metric_configuration_id: metric_configuration.id) }
6 6
7 describe 'new' do 7 describe 'new' do
8 - let(:kalibro_configuration) { FactoryGirl.build(:kalibro_configuration_with_id) } 8 + let(:kalibro_configuration) { FactoryGirl.build(:kalibro_configuration, :with_id) }
9 9
10 before :each do 10 before :each do
11 sign_in FactoryGirl.create(:user) 11 sign_in FactoryGirl.create(:user)
@@ -35,7 +35,7 @@ describe KalibroRangesController, :type =&gt; :controller do @@ -35,7 +35,7 @@ describe KalibroRangesController, :type =&gt; :controller do
35 35
36 describe 'create' do 36 describe 'create' do
37 let(:kalibro_range_params) { kalibro_range.to_hash } 37 let(:kalibro_range_params) { kalibro_range.to_hash }
38 - let(:kalibro_configuration) { FactoryGirl.build(:kalibro_configuration_with_id) } 38 + let(:kalibro_configuration) { FactoryGirl.build(:kalibro_configuration, :with_id) }
39 39
40 before do 40 before do
41 sign_in FactoryGirl.create(:user) 41 sign_in FactoryGirl.create(:user)
spec/controllers/metric_configurations_controller_spec.rb
1 require 'rails_helper' 1 require 'rails_helper'
2 2
3 describe MetricConfigurationsController, :type => :controller do 3 describe MetricConfigurationsController, :type => :controller do
4 - let(:kalibro_configuration) { FactoryGirl.build(:kalibro_configuration_with_id) } 4 + let(:kalibro_configuration) { FactoryGirl.build(:kalibro_configuration, :with_id) }
5 describe 'choose_metric' do 5 describe 'choose_metric' do
6 let(:metric_collector) { FactoryGirl.build(:metric_collector) } 6 let(:metric_collector) { FactoryGirl.build(:metric_collector) }
7 before :each do 7 before :each do
@@ -24,12 +24,16 @@ describe MetricConfigurationsController, :type =&gt; :controller do @@ -24,12 +24,16 @@ describe MetricConfigurationsController, :type =&gt; :controller do
24 describe 'new' do 24 describe 'new' do
25 let!(:metric_collector) { FactoryGirl.build(:metric_collector) } 25 let!(:metric_collector) { FactoryGirl.build(:metric_collector) }
26 let!(:native_metric) { FactoryGirl.build(:loc) } 26 let!(:native_metric) { FactoryGirl.build(:loc) }
  27 + let!(:reading_groups) { [FactoryGirl.build(:reading_group)] }
  28 + let!(:user) { FactoryGirl.create(:user) }
  29 +
27 before :each do 30 before :each do
28 - sign_in FactoryGirl.create(:user) 31 + sign_in user
29 end 32 end
30 33
31 context 'when the current user owns the kalibro configuration' do 34 context 'when the current user owns the kalibro configuration' do
32 before :each do 35 before :each do
  36 + ReadingGroup.expects(:public_or_owned_by_user).with(user).returns(reading_groups)
33 subject.expects(:kalibro_configuration_owner?).returns true 37 subject.expects(:kalibro_configuration_owner?).returns true
34 KalibroClient::Entities::Processor::MetricCollectorDetails.expects(:find_by_name).with(metric_collector.name).returns(metric_collector) 38 KalibroClient::Entities::Processor::MetricCollectorDetails.expects(:find_by_name).with(metric_collector.name).returns(metric_collector)
35 metric_collector.expects(:find_metric_by_code).with(native_metric.code).returns(native_metric) 39 metric_collector.expects(:find_metric_by_code).with(native_metric.code).returns(native_metric)
@@ -92,7 +96,7 @@ describe MetricConfigurationsController, :type =&gt; :controller do @@ -92,7 +96,7 @@ describe MetricConfigurationsController, :type =&gt; :controller do
92 96
93 describe 'show' do 97 describe 'show' do
94 let(:metric_configuration) { FactoryGirl.build(:metric_configuration_with_id) } 98 let(:metric_configuration) { FactoryGirl.build(:metric_configuration_with_id) }
95 - let(:reading_group) { FactoryGirl.build(:reading_group_with_id) } 99 + let(:reading_group) { FactoryGirl.build(:reading_group, :with_id) }
96 let(:kalibro_range) { FactoryGirl.build(:kalibro_range) } 100 let(:kalibro_range) { FactoryGirl.build(:kalibro_range) }
97 101
98 before :each do 102 before :each do
@@ -108,14 +112,17 @@ describe MetricConfigurationsController, :type =&gt; :controller do @@ -108,14 +112,17 @@ describe MetricConfigurationsController, :type =&gt; :controller do
108 112
109 describe 'edit' do 113 describe 'edit' do
110 let(:metric_configuration) { FactoryGirl.build(:metric_configuration_with_id) } 114 let(:metric_configuration) { FactoryGirl.build(:metric_configuration_with_id) }
  115 + let!(:reading_groups) { [FactoryGirl.build(:reading_group)] }
  116 + let!(:user) { FactoryGirl.create(:user) }
111 117
112 context 'with a User logged in' do 118 context 'with a User logged in' do
113 before do 119 before do
114 - sign_in FactoryGirl.create(:user) 120 + sign_in user
115 end 121 end
116 122
117 context 'when the user owns the metric configuration' do 123 context 'when the user owns the metric configuration' do
118 before :each do 124 before :each do
  125 + ReadingGroup.expects(:public_or_owned_by_user).with(user).returns(reading_groups)
119 subject.expects(:metric_configuration_owner?).returns(true) 126 subject.expects(:metric_configuration_owner?).returns(true)
120 MetricConfiguration.expects(:find).with(metric_configuration.id).returns(metric_configuration) 127 MetricConfiguration.expects(:find).with(metric_configuration.id).returns(metric_configuration)
121 get :edit, id: metric_configuration.id, kalibro_configuration_id: metric_configuration.kalibro_configuration_id.to_s 128 get :edit, id: metric_configuration.id, kalibro_configuration_id: metric_configuration.kalibro_configuration_id.to_s
spec/controllers/projects_controller_spec.rb
@@ -97,14 +97,9 @@ describe ProjectsController, :type =&gt; :controller do @@ -97,14 +97,9 @@ describe ProjectsController, :type =&gt; :controller do
97 97
98 context 'when the user owns the project' do 98 context 'when the user owns the project' do
99 before :each do 99 before :each do
100 - @project_attributes.expects(:destroy)  
101 @subject.expects(:destroy) 100 @subject.expects(:destroy)
102 101
103 - #Those two mocks looks the same but they are necessary since params[:id] is a String and @project.id is an Integer :(  
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)  
106 -  
107 - User.any_instance.expects(:project_attributes).at_least_once.returns(@attributes) 102 + subject.expects(:project_owner?)
108 103
109 Project.expects(:find).with(@subject.id).returns(@subject) 104 Project.expects(:find).with(@subject.id).returns(@subject)
110 delete :destroy, :id => @subject.id 105 delete :destroy, :id => @subject.id
spec/controllers/reading_groups_controller_spec.rb
@@ -12,12 +12,13 @@ describe ReadingGroupsController, :type =&gt; :controller do @@ -12,12 +12,13 @@ describe ReadingGroupsController, :type =&gt; :controller do
12 end 12 end
13 13
14 describe 'create' do 14 describe 'create' do
  15 + let(:attributes) { {public: "1"} }
15 before do 16 before do
16 sign_in FactoryGirl.create(:user) 17 sign_in FactoryGirl.create(:user)
17 end 18 end
18 19
19 context 'with valid fields' do 20 context 'with valid fields' do
20 - let(:reading_group) { FactoryGirl.build(:reading_group_with_id) } 21 + let(:reading_group) { FactoryGirl.build(:reading_group, :with_id) }
21 let(:subject_params) { reading_group.to_hash } 22 let(:subject_params) { reading_group.to_hash }
22 23
23 before :each do 24 before :each do
@@ -26,7 +27,8 @@ describe ReadingGroupsController, :type =&gt; :controller do @@ -26,7 +27,8 @@ describe ReadingGroupsController, :type =&gt; :controller do
26 27
27 context 'rendering the show' do 28 context 'rendering the show' do
28 before :each do 29 before :each do
29 - post :create, :reading_group => subject_params 30 + post :create, :reading_group => subject_params, :attributes => attributes
  31 +
30 end 32 end
31 33
32 it 'should redirect to the show view' do 34 it 'should redirect to the show view' do
@@ -36,7 +38,8 @@ describe ReadingGroupsController, :type =&gt; :controller do @@ -36,7 +38,8 @@ describe ReadingGroupsController, :type =&gt; :controller do
36 38
37 context 'without rendering the show view' do 39 context 'without rendering the show view' do
38 before :each do 40 before :each do
39 - post :create, :reading_group => subject_params 41 + post :create, :reading_group => subject_params, :attributes => attributes
  42 +
40 end 43 end
41 44
42 it { is_expected.to respond_with(:redirect) } 45 it { is_expected.to respond_with(:redirect) }
@@ -45,13 +48,14 @@ describe ReadingGroupsController, :type =&gt; :controller do @@ -45,13 +48,14 @@ describe ReadingGroupsController, :type =&gt; :controller do
45 48
46 context 'with an invalid field' do 49 context 'with an invalid field' do
47 before :each do 50 before :each do
48 - @subject = FactoryGirl.build(:reading_group_with_id) 51 + @subject = FactoryGirl.build(:reading_group, :with_id)
49 @subject_params = @subject.to_hash 52 @subject_params = @subject.to_hash
50 53
51 ReadingGroup.expects(:new).at_least_once.with(@subject_params).returns(@subject) 54 ReadingGroup.expects(:new).at_least_once.with(@subject_params).returns(@subject)
52 ReadingGroup.any_instance.expects(:save).returns(false) 55 ReadingGroup.any_instance.expects(:save).returns(false)
53 56
54 - post :create, :reading_group => @subject_params 57 + post :create, :reading_group => @subject_params, :attributes => attributes
  58 +
55 end 59 end
56 60
57 it { is_expected.to render_template(:new) } 61 it { is_expected.to render_template(:new) }
@@ -59,7 +63,7 @@ describe ReadingGroupsController, :type =&gt; :controller do @@ -59,7 +63,7 @@ describe ReadingGroupsController, :type =&gt; :controller do
59 end 63 end
60 64
61 describe 'show' do 65 describe 'show' do
62 - let!(:reading_group) { FactoryGirl.build(:reading_group_with_id) } 66 + let!(:reading_group) { FactoryGirl.build(:reading_group, :with_id) }
63 let(:reading) { FactoryGirl.build(:reading_with_id) } 67 let(:reading) { FactoryGirl.build(:reading_with_id) }
64 before :each do 68 before :each do
65 ReadingGroup.expects(:find).with(reading_group.id).returns(reading_group) 69 ReadingGroup.expects(:find).with(reading_group.id).returns(reading_group)
@@ -71,27 +75,20 @@ describe ReadingGroupsController, :type =&gt; :controller do @@ -71,27 +75,20 @@ describe ReadingGroupsController, :type =&gt; :controller do
71 75
72 describe 'destroy' do 76 describe 'destroy' do
73 before do 77 before do
74 - @subject = FactoryGirl.build(:reading_group_with_id) 78 + @subject = FactoryGirl.build(:reading_group, :with_id)
75 end 79 end
76 80
77 context 'with a User logged in' do 81 context 'with a User logged in' do
78 before do 82 before do
79 sign_in FactoryGirl.create(:user) 83 sign_in FactoryGirl.create(:user)
80 - @ownership = FactoryGirl.build(:reading_group_ownership) 84 + @ownership = FactoryGirl.build(:reading_group_attributes)
81 @ownerships = [] 85 @ownerships = []
82 -  
83 end 86 end
84 87
85 context 'when the user owns the reading group' do 88 context 'when the user owns the reading group' do
86 before :each do 89 before :each do
87 - @ownership.expects(:destroy)  
88 @subject.expects(:destroy) 90 @subject.expects(:destroy)
89 -  
90 - #Those two mocks looks the same but they are necessary since params[:id] is a String and @ReadingGroup.id is an Integer :(  
91 - @ownerships.expects(:find_by_reading_group_id).with("#{@subject.id}").returns(@ownership)  
92 - @ownerships.expects(:find_by_reading_group_id!).with(@subject.id).returns(@ownership)  
93 -  
94 - User.any_instance.expects(:reading_group_ownerships).at_least_once.returns(@ownerships) 91 + subject.expects(:reading_group_owner?)
95 92
96 ReadingGroup.expects(:find).with(@subject.id).returns(@subject) 93 ReadingGroup.expects(:find).with(@subject.id).returns(@subject)
97 delete :destroy, :id => @subject.id 94 delete :destroy, :id => @subject.id
@@ -107,7 +104,7 @@ describe ReadingGroupsController, :type =&gt; :controller do @@ -107,7 +104,7 @@ describe ReadingGroupsController, :type =&gt; :controller do
107 context "when the user doesn't own the reading group" do 104 context "when the user doesn't own the reading group" do
108 before :each do 105 before :each do
109 @ownerships.expects(:find_by_reading_group_id).with("#{@subject.id}").returns(nil) 106 @ownerships.expects(:find_by_reading_group_id).with("#{@subject.id}").returns(nil)
110 - User.any_instance.expects(:reading_group_ownerships).at_least_once.returns(@ownerships) 107 + User.any_instance.expects(:reading_group_attributes).at_least_once.returns(@ownerships)
111 108
112 delete :destroy, :id => @subject.id 109 delete :destroy, :id => @subject.id
113 end 110 end
@@ -126,9 +123,11 @@ describe ReadingGroupsController, :type =&gt; :controller do @@ -126,9 +123,11 @@ describe ReadingGroupsController, :type =&gt; :controller do
126 end 123 end
127 124
128 describe 'index' do 125 describe 'index' do
  126 + let!(:reading_group_attribute) { FactoryGirl.build(:reading_group_attributes) }
129 before :each do 127 before :each do
130 - @subject = FactoryGirl.build(:reading_group_with_id)  
131 - ReadingGroup.expects(:all).returns([@subject]) 128 + @subject = FactoryGirl.build(:reading_group, :with_id)
  129 + ReadingGroupAttributes.expects(:where).with(public: true).returns([reading_group_attribute])
  130 + ReadingGroup.expects(:find).with(reading_group_attribute.reading_group_id).returns([@subject])
132 get :index 131 get :index
133 end 132 end
134 133
@@ -137,16 +136,16 @@ describe ReadingGroupsController, :type =&gt; :controller do @@ -137,16 +136,16 @@ describe ReadingGroupsController, :type =&gt; :controller do
137 136
138 describe 'edit' do 137 describe 'edit' do
139 before do 138 before do
140 - @subject = FactoryGirl.build(:reading_group_with_id) 139 + @subject = FactoryGirl.build(:reading_group, :with_id)
141 end 140 end
142 141
143 context 'with a User logged in' do 142 context 'with a User logged in' do
144 before do 143 before do
145 @user = FactoryGirl.create(:user) 144 @user = FactoryGirl.create(:user)
146 - @ownership = FactoryGirl.build(:reading_group_ownership) 145 + @ownership = FactoryGirl.build(:reading_group_attributes)
147 @ownerships = [] 146 @ownerships = []
148 147
149 - User.any_instance.expects(:reading_group_ownerships).at_least_once.returns(@ownerships) 148 + User.any_instance.expects(:reading_group_attributes).at_least_once.returns(@ownerships)
150 149
151 sign_in @user 150 sign_in @user
152 end 151 end
@@ -168,7 +167,7 @@ describe ReadingGroupsController, :type =&gt; :controller do @@ -168,7 +167,7 @@ describe ReadingGroupsController, :type =&gt; :controller do
168 167
169 context 'when the user does not own the reading group' do 168 context 'when the user does not own the reading group' do
170 before do 169 before do
171 - @subject = FactoryGirl.build(:another_reading_group_with_id) 170 + @subject = FactoryGirl.build(:another_reading_group, :with_id)
172 @ownerships.expects(:find_by_reading_group_id).with("#{@subject.id}").returns(nil) 171 @ownerships.expects(:find_by_reading_group_id).with("#{@subject.id}").returns(nil)
173 172
174 get :edit, :id => @subject.id 173 get :edit, :id => @subject.id
@@ -190,8 +189,9 @@ describe ReadingGroupsController, :type =&gt; :controller do @@ -190,8 +189,9 @@ describe ReadingGroupsController, :type =&gt; :controller do
190 189
191 describe 'update' do 190 describe 'update' do
192 before do 191 before do
193 - @subject = FactoryGirl.build(:reading_group_with_id) 192 + @subject = FactoryGirl.build(:reading_group, :with_id)
194 @subject_params = @subject.to_hash 193 @subject_params = @subject.to_hash
  194 + @ownership = FactoryGirl.build(:reading_group_attributes, reading_group_id: @subject.id)
195 end 195 end
196 196
197 context 'when the user is logged in' do 197 context 'when the user is logged in' do
@@ -201,11 +201,10 @@ describe ReadingGroupsController, :type =&gt; :controller do @@ -201,11 +201,10 @@ describe ReadingGroupsController, :type =&gt; :controller do
201 201
202 context 'when user owns the reading group' do 202 context 'when user owns the reading group' do
203 before do 203 before do
204 - @ownership = FactoryGirl.build(:reading_group_ownership)  
205 @ownerships = [] 204 @ownerships = []
206 205
207 @ownerships.expects(:find_by_reading_group_id).with("#{@subject.id}").returns(@ownership) 206 @ownerships.expects(:find_by_reading_group_id).with("#{@subject.id}").returns(@ownership)
208 - User.any_instance.expects(:reading_group_ownerships).at_least_once.returns(@ownerships) 207 + User.any_instance.expects(:reading_group_attributes).at_least_once.returns(@ownerships)
209 end 208 end
210 209
211 context 'with valid fields' do 210 context 'with valid fields' do
@@ -216,7 +215,7 @@ describe ReadingGroupsController, :type =&gt; :controller do @@ -216,7 +215,7 @@ describe ReadingGroupsController, :type =&gt; :controller do
216 215
217 context 'rendering the show' do 216 context 'rendering the show' do
218 before :each do 217 before :each do
219 - post :update, :id => @subject.id, :reading_group => @subject_params 218 + post :update, :id => @subject.id, :reading_group => @subject_params, :attributes => {public: @ownership.public}
220 end 219 end
221 220
222 it 'should redirect to the show view' do 221 it 'should redirect to the show view' do
@@ -226,7 +225,8 @@ describe ReadingGroupsController, :type =&gt; :controller do @@ -226,7 +225,8 @@ describe ReadingGroupsController, :type =&gt; :controller do
226 225
227 context 'without rendering the show view' do 226 context 'without rendering the show view' do
228 before :each do 227 before :each do
229 - post :update, :id => @subject.id, :reading_group => @subject_params 228 + post :update, :id => @subject.id, :reading_group => @subject_params, :attributes => {public: @ownership.public}
  229 +
230 end 230 end
231 231
232 it { is_expected.to respond_with(:redirect) } 232 it { is_expected.to respond_with(:redirect) }
@@ -238,7 +238,8 @@ describe ReadingGroupsController, :type =&gt; :controller do @@ -238,7 +238,8 @@ describe ReadingGroupsController, :type =&gt; :controller do
238 ReadingGroup.expects(:find).with(@subject.id).returns(@subject) 238 ReadingGroup.expects(:find).with(@subject.id).returns(@subject)
239 ReadingGroup.any_instance.expects(:update).with(@subject_params).returns(false) 239 ReadingGroup.any_instance.expects(:update).with(@subject_params).returns(false)
240 240
241 - post :update, :id => @subject.id, :reading_group => @subject_params 241 + post :update, :id => @subject.id, :reading_group => @subject_params, :attributes => {public: @ownership.public}
  242 +
242 end 243 end
243 244
244 it { is_expected.to render_template(:edit) } 245 it { is_expected.to render_template(:edit) }
@@ -247,7 +248,8 @@ describe ReadingGroupsController, :type =&gt; :controller do @@ -247,7 +248,8 @@ describe ReadingGroupsController, :type =&gt; :controller do
247 248
248 context 'when the user does not own the reading group' do 249 context 'when the user does not own the reading group' do
249 before :each do 250 before :each do
250 - post :update, :id => @subject.id, :reading_group => @subject_params 251 + post :update, :id => @subject.id, :reading_group => @subject_params, :attributes => {public: @ownership.public}
  252 +
251 end 253 end
252 254
253 it { is_expected.to redirect_to reading_group_path } 255 it { is_expected.to redirect_to reading_group_path }
@@ -256,7 +258,8 @@ describe ReadingGroupsController, :type =&gt; :controller do @@ -256,7 +258,8 @@ describe ReadingGroupsController, :type =&gt; :controller do
256 258
257 context 'with no user logged in' do 259 context 'with no user logged in' do
258 before :each do 260 before :each do
259 - post :update, :id => @subject.id, :reading_group => @subject_params 261 + post :update, :id => @subject.id, :reading_group => @subject_params, :attributes => {public: @ownership.public}
  262 +
260 end 263 end
261 264
262 it { is_expected.to redirect_to new_user_session_path } 265 it { is_expected.to redirect_to new_user_session_path }
spec/controllers/readings_controller_spec.rb
1 require 'rails_helper' 1 require 'rails_helper'
2 2
3 describe ReadingsController, :type => :controller do 3 describe ReadingsController, :type => :controller do
4 - let(:reading_group) { FactoryGirl.build(:reading_group_with_id) } 4 + let(:reading_group) { FactoryGirl.build(:reading_group, :with_id) }
5 5
6 describe 'new' do 6 describe 'new' do
7 before :each do 7 before :each do
spec/controllers/repositories_controller_spec.rb
@@ -5,12 +5,15 @@ describe RepositoriesController, :type =&gt; :controller do @@ -5,12 +5,15 @@ describe RepositoriesController, :type =&gt; :controller do
5 5
6 describe 'new' do 6 describe 'new' do
7 context 'with a User logged in' do 7 context 'with a User logged in' do
  8 + let!(:user) { FactoryGirl.create(:user) }
  9 + let!(:kalibro_configurations) { [FactoryGirl.build(:kalibro_configuration)] }
8 before :each do 10 before :each do
9 - sign_in FactoryGirl.create(:user) 11 + sign_in user
10 end 12 end
11 13
12 context 'when the current user owns the project' do 14 context 'when the current user owns the project' do
13 before :each do 15 before :each do
  16 + KalibroConfiguration.expects(:public_or_owned_by_user).with(user).returns(kalibro_configurations)
14 Repository.expects(:repository_types).returns([]) 17 Repository.expects(:repository_types).returns([])
15 subject.expects(:project_owner?).returns true 18 subject.expects(:project_owner?).returns true
16 19
@@ -41,11 +44,13 @@ describe RepositoriesController, :type =&gt; :controller do @@ -41,11 +44,13 @@ describe RepositoriesController, :type =&gt; :controller do
41 end 44 end
42 45
43 describe 'create' do 46 describe 'create' do
  47 + let!(:kalibro_configurations) { [FactoryGirl.build(:kalibro_configuration)] }
  48 + let!(:user) { FactoryGirl.create(:user) }
44 let (:repository) { FactoryGirl.build(:repository, project_id: project.id) } 49 let (:repository) { FactoryGirl.build(:repository, project_id: project.id) }
45 let(:repository_params) { FactoryGirl.build(:repository).to_hash } 50 let(:repository_params) { FactoryGirl.build(:repository).to_hash }
46 51
47 before do 52 before do
48 - sign_in FactoryGirl.create(:user) 53 + sign_in user
49 end 54 end
50 55
51 context 'when the current user owns the project' do 56 context 'when the current user owns the project' do
@@ -66,6 +71,7 @@ describe RepositoriesController, :type =&gt; :controller do @@ -66,6 +71,7 @@ describe RepositoriesController, :type =&gt; :controller do
66 71
67 context 'with an invalid field' do 72 context 'with an invalid field' do
68 before :each do 73 before :each do
  74 + KalibroConfiguration.expects(:public_or_owned_by_user).with(user).returns(kalibro_configurations)
69 Repository.any_instance.expects(:save).returns(false) 75 Repository.any_instance.expects(:save).returns(false)
70 Repository.expects(:repository_types).returns([]) 76 Repository.expects(:repository_types).returns([])
71 77
@@ -91,9 +97,7 @@ describe RepositoriesController, :type =&gt; :controller do @@ -91,9 +97,7 @@ describe RepositoriesController, :type =&gt; :controller do
91 97
92 context 'without a specific module_result' do 98 context 'without a specific module_result' do
93 before :each do 99 before :each do
94 - processing = FactoryGirl.build(:processing)  
95 -  
96 - KalibroConfiguration.expects(:find).with(repository.id).returns(FactoryGirl.build(:kalibro_configuration_with_id)) 100 + KalibroConfiguration.expects(:find).with(repository.id).returns(FactoryGirl.build(:kalibro_configuration, :with_id))
97 Repository.expects(:find).with(repository.id).returns(repository) 101 Repository.expects(:find).with(repository.id).returns(repository)
98 102
99 get :show, id: repository.id.to_s, project_id: project.id.to_s 103 get :show, id: repository.id.to_s, project_id: project.id.to_s
@@ -105,9 +109,7 @@ describe RepositoriesController, :type =&gt; :controller do @@ -105,9 +109,7 @@ describe RepositoriesController, :type =&gt; :controller do
105 context 'for an specific module_result' do 109 context 'for an specific module_result' do
106 110
107 before :each do 111 before :each do
108 - processing = FactoryGirl.build(:processing)  
109 -  
110 - KalibroConfiguration.expects(:find).with(repository.id).returns(FactoryGirl.build(:kalibro_configuration_with_id)) 112 + KalibroConfiguration.expects(:find).with(repository.id).returns(FactoryGirl.build(:kalibro_configuration, :with_id))
111 Repository.expects(:find).with(repository.id).returns(repository) 113 Repository.expects(:find).with(repository.id).returns(repository)
112 114
113 get :show, id: repository.id.to_s, project_id: project.id.to_s 115 get :show, id: repository.id.to_s, project_id: project.id.to_s
@@ -161,12 +163,15 @@ describe RepositoriesController, :type =&gt; :controller do @@ -161,12 +163,15 @@ describe RepositoriesController, :type =&gt; :controller do
161 let(:repository) { FactoryGirl.build(:repository) } 163 let(:repository) { FactoryGirl.build(:repository) }
162 164
163 context 'with an User logged in' do 165 context 'with an User logged in' do
  166 + let!(:user) { FactoryGirl.create(:user) }
  167 + let!(:kalibro_configurations) { [FactoryGirl.build(:kalibro_configuration)] }
164 before do 168 before do
165 - sign_in FactoryGirl.create(:user) 169 + sign_in user
166 end 170 end
167 171
168 context 'when the user owns the repository' do 172 context 'when the user owns the repository' do
169 before :each do 173 before :each do
  174 + KalibroConfiguration.expects(:public_or_owned_by_user).with(user).returns(kalibro_configurations)
170 subject.expects(:repository_owner?).returns true 175 subject.expects(:repository_owner?).returns true
171 Repository.expects(:find).at_least_once.with(repository.id).returns(repository) 176 Repository.expects(:find).at_least_once.with(repository.id).returns(repository)
172 Repository.expects(:repository_types).returns(["SUBVERSION"]) 177 Repository.expects(:repository_types).returns(["SUBVERSION"])
@@ -197,12 +202,15 @@ describe RepositoriesController, :type =&gt; :controller do @@ -197,12 +202,15 @@ describe RepositoriesController, :type =&gt; :controller do
197 end 202 end
198 203
199 describe 'update' do 204 describe 'update' do
  205 + let(:kalibro_configurations) { [FactoryGirl.build(:kalibro_configuration)] }
200 let(:repository) { FactoryGirl.build(:repository) } 206 let(:repository) { FactoryGirl.build(:repository) }
201 let(:repository_params) { Hash[FactoryGirl.attributes_for(:repository).map { |k,v| [k.to_s, v.to_s] }] } #FIXME: Mocha is creating the expectations with strings, but FactoryGirl returns everything with sybols and integers 207 let(:repository_params) { Hash[FactoryGirl.attributes_for(:repository).map { |k,v| [k.to_s, v.to_s] }] } #FIXME: Mocha is creating the expectations with strings, but FactoryGirl returns everything with sybols and integers
202 208
203 context 'when the user is logged in' do 209 context 'when the user is logged in' do
  210 + let!(:user) { FactoryGirl.create(:user) }
  211 +
204 before do 212 before do
205 - sign_in FactoryGirl.create(:user) 213 + sign_in user
206 end 214 end
207 215
208 context 'when user owns the repository' do 216 context 'when user owns the repository' do
@@ -224,6 +232,7 @@ describe RepositoriesController, :type =&gt; :controller do @@ -224,6 +232,7 @@ describe RepositoriesController, :type =&gt; :controller do
224 232
225 context 'with an invalid field' do 233 context 'with an invalid field' do
226 before :each do 234 before :each do
  235 + KalibroConfiguration.expects(:public_or_owned_by_user).with(user).returns(kalibro_configurations)
227 Repository.expects(:find).at_least_once.with(repository.id).returns(repository) 236 Repository.expects(:find).at_least_once.with(repository.id).returns(repository)
228 Repository.any_instance.expects(:update).with(repository_params).returns(false) 237 Repository.any_instance.expects(:update).with(repository_params).returns(false)
229 Repository.expects(:repository_types).returns([]) 238 Repository.expects(:repository_types).returns([])
@@ -333,7 +342,7 @@ describe RepositoriesController, :type =&gt; :controller do @@ -333,7 +342,7 @@ describe RepositoriesController, :type =&gt; :controller do
333 subject.expects(:repository_owner?).returns true 342 subject.expects(:repository_owner?).returns true
334 repository.expects(:process) 343 repository.expects(:process)
335 Repository.expects(:find).at_least_once.with(repository.id).returns(repository) 344 Repository.expects(:find).at_least_once.with(repository.id).returns(repository)
336 - KalibroConfiguration.expects(:find).with(repository.id).returns(FactoryGirl.build(:kalibro_configuration_with_id)) 345 + KalibroConfiguration.expects(:find).with(repository.id).returns(FactoryGirl.build(:kalibro_configuration, :with_id))
337 get :process_repository, project_id: project.id.to_s, id: repository.id 346 get :process_repository, project_id: project.id.to_s, id: repository.id
338 end 347 end
339 it { is_expected.to redirect_to(project_repository_path(repository.project_id, repository.id)) } 348 it { is_expected.to redirect_to(project_repository_path(repository.project_id, repository.id)) }
spec/factories/kalibro_configuration_attributes.rb 0 → 100644
@@ -0,0 +1,14 @@ @@ -0,0 +1,14 @@
  1 +# Read about factories at https://github.com/thoughtbot/factory_girl
  2 +
  3 +FactoryGirl.define do
  4 + factory :kalibro_configuration_attributes, class: KalibroConfigurationAttributes do
  5 + sequence(:id, 1)
  6 + association :user, strategy: :build
  7 + association :kalibro_configuration, :with_id, strategy: :build
  8 + self.public true
  9 +
  10 + trait :private do
  11 + self.public false
  12 + end
  13 + end
  14 +end
spec/factories/kalibro_configurations.rb
1 - FactoryGirl.define do  
2 - factory :kalibro_configuration, class: KalibroConfiguration do 1 +FactoryGirl.define do
  2 + factory :kalibro_configuration do
3 name "Java" 3 name "Java"
4 description "Code metrics for Java." 4 description "Code metrics for Java."
5 5
@@ -7,17 +7,18 @@ @@ -7,17 +7,18 @@
7 id 1 7 id 1
8 end 8 end
9 9
10 - factory :kalibro_configuration_with_id, traits: [:with_id]  
11 - end  
12 -  
13 - factory :another_kalibro_configuration, class: KalibroConfiguration do  
14 - name "Perl"  
15 - description "Code metrics for Perl." 10 + trait :with_sequential_id do
  11 + sequence(:id, 1)
  12 + end
16 13
17 - trait :with_id do  
18 - id 12 14 + factory :another_kalibro_configuration do
  15 + name "Perl"
  16 + description "Code metrics for Perl."
19 end 17 end
20 18
21 - factory :another_kalibro_configuration_with_id, traits: [:with_id] 19 + factory :public_kalibro_configuration do
  20 + name "Public Kalibro Configuration"
  21 + description "Public Configuration."
  22 + end
22 end 23 end
23 end 24 end
spec/factories/mezuro_configuration_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 :kalibro_configuration_ownership do  
5 - user_id 1  
6 - kalibro_configuration_id 1  
7 - end  
8 -end  
spec/factories/reading_group_attributes.rb 0 → 100644
@@ -0,0 +1,14 @@ @@ -0,0 +1,14 @@
  1 +# Read about factories at https://github.com/thoughtbot/factory_girl
  2 +
  3 +FactoryGirl.define do
  4 + factory :reading_group_attributes, class: ReadingGroupAttributes do
  5 + sequence(:id, 1)
  6 + association :user, strategy: :build
  7 + association :reading_group, :with_id, strategy: :build
  8 + self.public true
  9 +
  10 + trait :private do
  11 + self.public false
  12 + end
  13 + end
  14 +end
spec/factories/reading_group_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 :reading_group_ownership do  
5 - user_id 1  
6 - reading_group_id 1  
7 - end  
8 -end  
spec/factories/reading_groups.rb
@@ -4,20 +4,17 @@ FactoryGirl.define do @@ -4,20 +4,17 @@ FactoryGirl.define do
4 description "Cacildis!" 4 description "Cacildis!"
5 5
6 trait :with_id do 6 trait :with_id do
7 - id 1 7 + sequence(:id, 1)
8 end 8 end
9 9
10 - factory :reading_group_with_id, traits: [:with_id]  
11 - end  
12 -  
13 - factory :another_reading_group, class: ReadingGroup do  
14 - name "My Reading Group"  
15 - description "The best one"  
16 -  
17 - trait :with_id do  
18 - id 2 10 + factory :another_reading_group do
  11 + name "My Reading Group"
  12 + description "The best one"
19 end 13 end
20 14
21 - factory :another_reading_group_with_id, traits: [:with_id] 15 + factory :public_reading_group do
  16 + name "Public Reading Group"
  17 + description "Public"
  18 + end
22 end 19 end
23 end 20 end
spec/factories/users.rb
@@ -6,5 +6,12 @@ FactoryGirl.define do @@ -6,5 +6,12 @@ FactoryGirl.define do
6 name "Diego Martinez" 6 name "Diego Martinez"
7 email "diego@email.com" 7 email "diego@email.com"
8 password "password" 8 password "password"
  9 +
  10 + factory :another_user do
  11 + id 2
  12 + name "Heitor Reis"
  13 + email "hr@email.com"
  14 + password "password"
  15 + end
9 end 16 end
10 end 17 end
spec/helpers/kalibro_configurations_helper_spec.rb
@@ -3,7 +3,7 @@ require &#39;rails_helper&#39; @@ -3,7 +3,7 @@ require &#39;rails_helper&#39;
3 describe KalibroConfigurationsHelper, :type => :helper do 3 describe KalibroConfigurationsHelper, :type => :helper do
4 describe 'kalibro_configuration_owner?' do 4 describe 'kalibro_configuration_owner?' do
5 before :each do 5 before :each do
6 - @subject = FactoryGirl.build(:kalibro_configuration_with_id) 6 + @subject = FactoryGirl.build(:kalibro_configuration, :with_id)
7 end 7 end
8 8
9 context 'returns false if not logged in' do 9 context 'returns false if not logged in' do
@@ -18,10 +18,10 @@ describe KalibroConfigurationsHelper, :type =&gt; :helper do @@ -18,10 +18,10 @@ describe KalibroConfigurationsHelper, :type =&gt; :helper do
18 helper.expects(:user_signed_in?).returns(true) 18 helper.expects(:user_signed_in?).returns(true)
19 helper.expects(:current_user).returns(FactoryGirl.build(:user)) 19 helper.expects(:current_user).returns(FactoryGirl.build(:user))
20 20
21 - @ownerships = []  
22 - @ownerships.expects(:find_by_kalibro_configuration_id).with(@subject.id).returns(nil) 21 + @attributes = []
  22 + @attributes.expects(:find_by_kalibro_configuration_id).with(@subject.id).returns(nil)
23 23
24 - User.any_instance.expects(:kalibro_configuration_ownerships).returns(@ownerships) 24 + User.any_instance.expects(:kalibro_configuration_attributes).returns(@attributes)
25 end 25 end
26 26
27 it { expect(helper.kalibro_configuration_owner?(@subject.id)).to be_falsey } 27 it { expect(helper.kalibro_configuration_owner?(@subject.id)).to be_falsey }
@@ -32,10 +32,10 @@ describe KalibroConfigurationsHelper, :type =&gt; :helper do @@ -32,10 +32,10 @@ describe KalibroConfigurationsHelper, :type =&gt; :helper do
32 helper.expects(:user_signed_in?).returns(true) 32 helper.expects(:user_signed_in?).returns(true)
33 helper.expects(:current_user).returns(FactoryGirl.build(:user)) 33 helper.expects(:current_user).returns(FactoryGirl.build(:user))
34 34
35 - @ownership = FactoryGirl.build(:kalibro_configuration_ownership)  
36 - @ownerships = []  
37 - @ownerships.expects(:find_by_kalibro_configuration_id).with(@subject.id).returns(@ownership)  
38 - User.any_instance.expects(:kalibro_configuration_ownerships).returns(@ownerships) 35 + @ownership = FactoryGirl.build(:kalibro_configuration_attributes)
  36 + @attributes = []
  37 + @attributes.expects(:find_by_kalibro_configuration_id).with(@subject.id).returns(@ownership)
  38 + User.any_instance.expects(:kalibro_configuration_attributes).returns(@attributes)
39 end 39 end
40 40
41 it { expect(helper.kalibro_configuration_owner?(@subject.id)).to be_truthy } 41 it { expect(helper.kalibro_configuration_owner?(@subject.id)).to be_truthy }
spec/helpers/metric_configurations_helper_spec.rb
@@ -9,7 +9,7 @@ describe MetricConfigurationsHelper, :type =&gt; :helper do @@ -9,7 +9,7 @@ describe MetricConfigurationsHelper, :type =&gt; :helper do
9 end 9 end
10 10
11 describe 'reading_group_options' do 11 describe 'reading_group_options' do
12 - let! (:reading_group) { FactoryGirl.build(:reading_group_with_id) } 12 + let! (:reading_group) { FactoryGirl.build(:reading_group, :with_id) }
13 13
14 before :each do 14 before :each do
15 ReadingGroup.expects(:all).returns([reading_group]) 15 ReadingGroup.expects(:all).returns([reading_group])
spec/helpers/reading_groups_helper_spec.rb
@@ -4,7 +4,7 @@ describe ReadingGroupsHelper, :type =&gt; :helper do @@ -4,7 +4,7 @@ describe ReadingGroupsHelper, :type =&gt; :helper do
4 4
5 describe 'reading_group_owner?' do 5 describe 'reading_group_owner?' do
6 before :each do 6 before :each do
7 - @subject = FactoryGirl.build(:reading_group_with_id) 7 + @subject = FactoryGirl.build(:reading_group, :with_id)
8 end 8 end
9 9
10 context 'returns false if not logged in' do 10 context 'returns false if not logged in' do
@@ -22,7 +22,7 @@ describe ReadingGroupsHelper, :type =&gt; :helper do @@ -22,7 +22,7 @@ describe ReadingGroupsHelper, :type =&gt; :helper do
22 @ownerships = [] 22 @ownerships = []
23 @ownerships.expects(:find_by_reading_group_id).with(@subject.id).returns(nil) 23 @ownerships.expects(:find_by_reading_group_id).with(@subject.id).returns(nil)
24 24
25 - User.any_instance.expects(:reading_group_ownerships).returns(@ownerships) 25 + User.any_instance.expects(:reading_group_attributes).returns(@ownerships)
26 end 26 end
27 27
28 it { expect(helper.reading_groups_owner?(@subject.id)).to be_falsey } 28 it { expect(helper.reading_groups_owner?(@subject.id)).to be_falsey }
@@ -33,10 +33,10 @@ describe ReadingGroupsHelper, :type =&gt; :helper do @@ -33,10 +33,10 @@ describe ReadingGroupsHelper, :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(:reading_group_ownership) 36 + @ownership = FactoryGirl.build(:reading_group_attributes)
37 @ownerships = [] 37 @ownerships = []
38 @ownerships.expects(:find_by_reading_group_id).with(@subject.id).returns(@ownership) 38 @ownerships.expects(:find_by_reading_group_id).with(@subject.id).returns(@ownership)
39 - User.any_instance.expects(:reading_group_ownerships).returns(@ownerships) 39 + User.any_instance.expects(:reading_group_attributes).returns(@ownerships)
40 end 40 end
41 41
42 it { expect(helper.reading_groups_owner?(@subject.id)).to be_truthy } 42 it { expect(helper.reading_groups_owner?(@subject.id)).to be_truthy }
spec/models/kalibro_configuration_attributes_spec.rb 0 → 100644
@@ -0,0 +1,7 @@ @@ -0,0 +1,7 @@
  1 +require 'rails_helper'
  2 +
  3 +describe KalibroConfigurationAttributes, :type => :model do
  4 + describe 'associations' do
  5 + it { is_expected.to belong_to(:user) }
  6 + end
  7 +end
spec/models/kalibro_configuration_spec.rb 0 → 100644
@@ -0,0 +1,102 @@ @@ -0,0 +1,102 @@
  1 +require 'rails_helper'
  2 +
  3 +describe KalibroConfiguration, :type => :model do
  4 + describe 'methods' do
  5 + describe 'class methods' do
  6 + describe 'public_or_owned_by_user' do
  7 + def build_attrs(kc_iter, *traits, **params)
  8 + kalibro_configuration = kc_iter.next
  9 + attr = FactoryGirl.build(:kalibro_configuration_attributes, *traits, params.merge(kalibro_configuration: kalibro_configuration))
  10 + kalibro_configuration.stubs(:attributes).returns(attr)
  11 + attr
  12 + end
  13 +
  14 + let!(:kalibro_configurations) { FactoryGirl.build_list(:kalibro_configuration, 4, :with_sequential_id) }
  15 + let!(:kc_iter) { kalibro_configurations.each }
  16 +
  17 + let!(:one_user) { FactoryGirl.build(:user) }
  18 + let!(:other_user) { FactoryGirl.build(:another_user) }
  19 +
  20 + let!(:ones_private_attrs) { build_attrs(kc_iter, :private, user: one_user) }
  21 + let!(:others_private_attrs) { build_attrs(kc_iter, :private, user: other_user) }
  22 + let!(:ones_public_attrs) { build_attrs(kc_iter, user: one_user) }
  23 + let!(:others_public_attrs) { build_attrs(kc_iter, user: other_user) }
  24 +
  25 + let!(:public_attrs) { [ones_public_attrs, others_public_attrs] }
  26 + let(:public_kalibro_configurations) { public_attrs.map(&:kalibro_configuration) }
  27 +
  28 + let(:ones_or_public_attrs) { public_attrs + [ones_private_attrs] }
  29 + let(:ones_or_public_kalibro_configurations) { ones_or_public_attrs.map(&:kalibro_configuration) }
  30 +
  31 + context 'when the kalibro configuration exists' do
  32 + before :each do
  33 + # Map the kalibro_configuration attributes to the corresponding Kalibro Configuration
  34 + kalibro_configurations.each do |kc|
  35 + KalibroConfiguration.stubs(:find).with(kc.id).returns(kc)
  36 + end
  37 +
  38 + KalibroConfigurationAttributes.expects(:where).with(public: true).returns(public_attrs)
  39 + end
  40 +
  41 + context 'when user is not provided' do
  42 + it 'should find all public reading groups' do
  43 + expect(KalibroConfiguration.public).to eq(public_kalibro_configurations)
  44 + end
  45 + end
  46 +
  47 + context 'when user is provided' do
  48 + before do
  49 + KalibroConfigurationAttributes.expects(:where).with(user_id: one_user.id, public: false).returns([ones_private_attrs])
  50 + end
  51 +
  52 + it 'should find all public and owned reading groups' do
  53 + expect(KalibroConfiguration.public_or_owned_by_user(one_user)).to eq(ones_or_public_kalibro_configurations)
  54 + end
  55 + end
  56 + end
  57 +
  58 + context 'when the kalibro configuration does not' do
  59 + before :each do
  60 + # Map the kalibro_configuration attributes to the corresponding Kalibro Configuration
  61 + kalibro_configurations.each do |kc|
  62 + KalibroConfiguration.stubs(:find).with(kc.id).raises(KalibroClient::Errors::RecordNotFound)
  63 + end
  64 +
  65 + KalibroConfigurationAttributes.expects(:where).with(public: true).returns(public_attrs)
  66 + end
  67 +
  68 + it 'is expected to be empty' do
  69 + expect(KalibroConfiguration.public).to be_empty
  70 + end
  71 + end
  72 + end
  73 + end
  74 +
  75 + describe 'destroy' do
  76 + context 'when attributes exist' do
  77 + let!(:kalibro_configuration_attributes) { FactoryGirl.build(:kalibro_configuration_attributes) }
  78 + let!(:kalibro_configuration) { kalibro_configuration_attributes.kalibro_configuration }
  79 +
  80 + it 'should be destroyed' do
  81 + kalibro_configuration.expects(:attributes).twice.returns(kalibro_configuration_attributes)
  82 + kalibro_configuration_attributes.expects(:destroy)
  83 + KalibroClient::Entities::Configurations::KalibroConfiguration.any_instance.expects(:destroy).returns(kalibro_configuration)
  84 + kalibro_configuration.destroy
  85 + end
  86 +
  87 + it 'is expected to clean the attributes memoization' do
  88 + # Call attributes once so it memoizes
  89 + KalibroConfigurationAttributes.expects(:find_by).with(kalibro_configuration_id: kalibro_configuration.id).returns(kalibro_configuration_attributes)
  90 + expect(kalibro_configuration.attributes).to eq(kalibro_configuration_attributes)
  91 +
  92 + # Destroying
  93 + kalibro_configuration.destroy
  94 +
  95 + # The expectation call will try to find the attributes on the database which should be nil since it was destroyed
  96 + KalibroConfigurationAttributes.expects(:find_by).with(kalibro_configuration_id: kalibro_configuration.id).returns(nil)
  97 + expect(kalibro_configuration.attributes).to be_nil
  98 + end
  99 + end
  100 + end
  101 + end
  102 +end
spec/models/mezuro_configuration_ownership_spec.rb
@@ -1,7 +0,0 @@ @@ -1,7 +0,0 @@
1 -require 'rails_helper'  
2 -  
3 -describe KalibroConfigurationOwnership, :type => :model do  
4 - describe 'associations' do  
5 - it { is_expected.to belong_to(:user) }  
6 - end  
7 -end  
spec/models/project_spec.rb
@@ -38,5 +38,32 @@ describe Project, :type =&gt; :model do @@ -38,5 +38,32 @@ describe Project, :type =&gt; :model do
38 expect(subject.attributes).to eq(project_attributes) 38 expect(subject.attributes).to eq(project_attributes)
39 end 39 end
40 end 40 end
  41 +
  42 + describe 'destroy' do
  43 + context 'when attributes exist' do
  44 + let!(:project) { FactoryGirl.build(:project) }
  45 + let!(:project_attributes) { FactoryGirl.build(:project_attributes, project_id: project.id) }
  46 +
  47 + it 'should be destroyed' do
  48 + project.expects(:attributes).twice.returns(project_attributes)
  49 + project_attributes.expects(:destroy)
  50 + KalibroClient::Entities::Processor::Project.any_instance.expects(:destroy).returns(project)
  51 + project.destroy
  52 + end
  53 +
  54 + it 'is expected to clean the attributes memoization' do
  55 + # Call attributes once so it memoizes
  56 + ProjectAttributes.expects(:find_by).with(project_id: project.id).returns(project_attributes)
  57 + expect(project.attributes).to eq(project_attributes)
  58 +
  59 + # Destroying
  60 + project.destroy
  61 +
  62 + # The expectation call will try to find the attributes on the database which should be nil since it was destroyed
  63 + ProjectAttributes.expects(:find_by).with(project_id: project.id).returns(nil)
  64 + expect(project.attributes).to_not eq(project_attributes)
  65 + end
  66 + end
  67 + end
41 end 68 end
42 end 69 end
spec/models/reading_group_attributes_spec.rb 0 → 100644
@@ -0,0 +1,7 @@ @@ -0,0 +1,7 @@
  1 +require 'rails_helper'
  2 +
  3 +describe ReadingGroupAttributes, :type => :model do
  4 + describe 'associations' do
  5 + it { is_expected.to belong_to(:user) }
  6 + end
  7 +end
spec/models/reading_group_ownership_spec.rb
@@ -1,7 +0,0 @@ @@ -1,7 +0,0 @@
1 -require 'rails_helper'  
2 -  
3 -describe ReadingGroupOwnership, :type => :model do  
4 - describe 'associations' do  
5 - it { is_expected.to belong_to(:user) }  
6 - end  
7 -end  
spec/models/reading_group_spec.rb
@@ -4,13 +4,13 @@ describe ReadingGroup, :type =&gt; :model do @@ -4,13 +4,13 @@ describe ReadingGroup, :type =&gt; :model do
4 describe 'methods' do 4 describe 'methods' do
5 describe 'persisted?' do 5 describe 'persisted?' do
6 before :each do 6 before :each do
7 - @subject = FactoryGirl.build(:reading_group_with_id) 7 + @subject = FactoryGirl.build(:reading_group, :with_id)
8 end 8 end
9 end 9 end
10 10
11 describe 'update' do 11 describe 'update' do
12 before :each do 12 before :each do
13 - @qt = FactoryGirl.build(:reading_group_with_id) 13 + @qt = FactoryGirl.build(:reading_group, :with_id)
14 @qt_params = @qt.to_hash 14 @qt_params = @qt.to_hash
15 end 15 end
16 16
@@ -36,7 +36,7 @@ describe ReadingGroup, :type =&gt; :model do @@ -36,7 +36,7 @@ describe ReadingGroup, :type =&gt; :model do
36 end 36 end
37 37
38 describe 'readings' do 38 describe 'readings' do
39 - subject { FactoryGirl.build(:reading_group_with_id) } 39 + subject { FactoryGirl.build(:reading_group, :with_id) }
40 let(:reading) { FactoryGirl.build(:reading_with_id) } 40 let(:reading) { FactoryGirl.build(:reading_with_id) }
41 41
42 it 'should call readings_of on the Reading model' do 42 it 'should call readings_of on the Reading model' do
@@ -45,5 +45,93 @@ describe ReadingGroup, :type =&gt; :model do @@ -45,5 +45,93 @@ describe ReadingGroup, :type =&gt; :model do
45 expect(subject.readings).to include(reading) 45 expect(subject.readings).to include(reading)
46 end 46 end
47 end 47 end
  48 +
  49 + describe 'destroy' do
  50 + context 'when attributes exist' do
  51 + let!(:reading_group_attributes) { FactoryGirl.build(:reading_group_attributes) }
  52 + let!(:reading_group) { reading_group_attributes.reading_group }
  53 +
  54 + it 'should be destroyed' do
  55 + reading_group.expects(:attributes).twice.returns(reading_group_attributes)
  56 + reading_group_attributes.expects(:destroy)
  57 + KalibroClient::Entities::Configurations::ReadingGroup.any_instance.expects(:destroy).returns(reading_group)
  58 + reading_group.destroy
  59 + end
  60 +
  61 + it 'is expected to clean the attributes memoization' do
  62 + # Call attributes once so it memoizes
  63 + ReadingGroupAttributes.expects(:find_by).with(reading_group_id: reading_group.id).returns(reading_group_attributes)
  64 + expect(reading_group.attributes).to eq(reading_group_attributes)
  65 +
  66 + # Destroying
  67 + reading_group.destroy
  68 +
  69 + # The expectation call will try to find the attributes on the database which should be nil since it was destroyed
  70 + ReadingGroupAttributes.expects(:find_by).with(reading_group_id: reading_group.id).returns(nil)
  71 + expect(reading_group.attributes).to be_nil
  72 + end
  73 + end
  74 + end
  75 + end
  76 +
  77 + describe 'class methods' do
  78 + describe 'public_or_owned_by_user' do
  79 + let!(:user) { FactoryGirl.build(:user) }
  80 +
  81 + let!(:owned_private_attrs) { FactoryGirl.build(:reading_group_attributes, :private, user_id: user.id) }
  82 + let!(:owned_public_attrs) { FactoryGirl.build(:reading_group_attributes, user_id: user.id) }
  83 + let!(:not_owned_private_attrs) { FactoryGirl.build(:reading_group_attributes, :private, user_id: user.id+1) }
  84 + let!(:not_owned_public_attrs) { FactoryGirl.build(:reading_group_attributes, user_id: user.id+1) }
  85 +
  86 + let!(:public_attrs) { [owned_public_attrs, not_owned_public_attrs] }
  87 + let(:public_reading_groups) { public_attrs.map(&:reading_group) }
  88 +
  89 + let!(:owned_or_public_attrs) { public_attrs + [owned_private_attrs] }
  90 + let!(:owned_or_public_reading_groups) { owned_or_public_attrs.map(&:reading_group) }
  91 +
  92 + let(:all_reading_groups) { owned_or_public_reading_groups + [not_owned_private_attrs.reading_group] }
  93 +
  94 + context 'when reading groups exist' do
  95 + before :each do
  96 + # Make sure the reading groups are found when looked up by the Attributes by their id
  97 + all_reading_groups.each do |reading_group|
  98 + ReadingGroup.stubs(:find).with(reading_group.id).returns(reading_group)
  99 + end
  100 +
  101 + ReadingGroupAttributes.expects(:where).with(public: true).returns(public_attrs)
  102 + end
  103 +
  104 + context 'when user is not provided' do
  105 + it 'should find all public reading groups' do
  106 + expect(ReadingGroup.public).to eq(public_reading_groups)
  107 + end
  108 + end
  109 +
  110 + context 'when user is provided' do
  111 + before do
  112 + ReadingGroupAttributes.expects(:where).with(user_id: user.id, public: false).returns([owned_private_attrs])
  113 + end
  114 +
  115 + it 'should find all public and owned reading groups' do
  116 + expect(ReadingGroup.public_or_owned_by_user(user)).to eq(owned_or_public_reading_groups)
  117 + end
  118 + end
  119 + end
  120 +
  121 + context 'when no reading groups exist' do
  122 + before :each do
  123 + # Map the reading group attributes to the corresponding Reading Group
  124 + all_reading_groups.each do |reading_group|
  125 + ReadingGroup.stubs(:find).with(reading_group.id).raises(KalibroClient::Errors::RecordNotFound)
  126 + end
  127 +
  128 + ReadingGroupAttributes.expects(:where).with(public: true).returns(public_attrs)
  129 + end
  130 +
  131 + it 'is expected to be empty' do
  132 + expect(ReadingGroup.public).to be_empty
  133 + end
  134 + end
  135 + end
48 end 136 end
49 end 137 end
spec/models/user_spec.rb
@@ -11,8 +11,8 @@ describe User, :type =&gt; :model do @@ -11,8 +11,8 @@ describe User, :type =&gt; :model do
11 11
12 describe 'associations' do 12 describe 'associations' do
13 it { is_expected.to have_many(:project_attributes) } 13 it { is_expected.to have_many(:project_attributes) }
14 - it { is_expected.to have_many(:reading_group_ownerships) }  
15 - it { is_expected.to have_many(:kalibro_configuration_ownerships) } 14 + it { is_expected.to have_many(:reading_group_attributes) }
  15 + it { is_expected.to have_many(:kalibro_configuration_attributes) }
16 end 16 end
17 17
18 describe 'methods' do 18 describe 'methods' do