Commit c5c5ae201d8a9c382c9051932872d9b6268e66c4

Authored by Daniel
Committed by Diego Camarinha
1 parent 119b56ec

Prepare model for optional Reading Group/Configuration visibility [no-ci]

app/models/kalibro_configuration.rb
1 1 class KalibroConfiguration < KalibroClient::Entities::Configurations::KalibroConfiguration
2 2 include KalibroRecord
  3 +
  4 + def self.public_or_owned_by_user(user=nil)
  5 + query = if user
  6 + KalibroConfigurationAttributes.where("user_id == ? OR public", user.id)
  7 + else
  8 + KalibroConfigurationAttributes.where(public: true)
  9 + end
  10 +
  11 + query.map { |cfg_attr|
  12 + self.find(cfg_attr.kalibro_configuration_id)
  13 + }.compact
  14 + end
  15 +
  16 + def self.public
  17 + self.public_or_owned_by_user(nil)
  18 + end
  19 +
  20 + def attributes
  21 + KalibroConfigurationAttributes.find_by(kalibro_configuration_id: self.id)
  22 + end
3 23 end
... ...
app/models/kalibro_configuration_attributes.rb 0 → 100644
... ... @@ -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   -class KalibroConfigurationOwnership < ActiveRecord::Base
2   - belongs_to :user
3   - validates :kalibro_configuration_id, presence: true
4   -end
app/models/reading_group.rb
1 1 class ReadingGroup < KalibroClient::Entities::Configurations::ReadingGroup
2 2 include KalibroRecord
  3 +
  4 + def self.public_or_owned_by_user(user=nil)
  5 + query = if user
  6 + ReadingGroupAttributes.where("user_id == ? OR public", user.id)
  7 + else
  8 + ReadingGroupAttributes.where(public: true)
  9 + end
  10 +
  11 + query.map { |cfg_attr|
  12 + self.find(cfg_attr.reading_group_id)
  13 + }.compact
  14 + end
  15 +
  16 + def self.public
  17 + self.public_or_owned_by_user(nil)
  18 + end
  19 +
  20 + def attributes
  21 + ReadingGroupAttributes.find_by(reading_group_id: self.id)
  22 + end
3 23 end
... ...
app/models/reading_group_attributes.rb 0 → 100644
... ... @@ -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   -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 9 validates :email, uniqueness: true
10 10  
11 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_attributess
  13 + has_many :kalibro_configuration_attributes
14 14 # Alert: when adding new parameters to this model, they should also be added to registrations_controller
15 15  
16 16 def projects
... ...
db/migrate/20140124124835_create_mezuro_configuration_ownerships.rb
1   -class CreateKalibroConfigurationOwnerships < ActiveRecord::Migration
  1 +class CreateMezuroConfigurationOwnerships < ActiveRecord::Migration
2 2 def change
3   - create_table :kalibro_configuration_ownerships do |t|
  3 + create_table :mezuro_configuration_ownerships do |t|
4 4 t.integer :user_id
5   - t.integer :kalibro_configuration_id
  5 + t.integer :mezuro_configuration_id
6 6  
7 7 t.timestamps
8 8 end
... ...
db/migrate/20150511180712_rename_kalibro_configuration_ownerships_to_kalibro_confiugration_attributes.rb 0 → 100644
... ... @@ -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 @@
  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/schema.rb
... ... @@ -11,13 +11,17 @@
11 11 #
12 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: 20150511181035) do
15 15  
16   - create_table "kalibro_configuration_ownerships", force: :cascade do |t|
  16 + # These are extensions that must be enabled in order to support this database
  17 + enable_extension "plpgsql"
  18 +
  19 + create_table "kalibro_configuration_attributes", force: :cascade do |t|
17 20 t.integer "user_id"
18 21 t.integer "kalibro_configuration_id"
19 22 t.datetime "created_at"
20 23 t.datetime "updated_at"
  24 + t.boolean "public", default: false
21 25 end
22 26  
23 27 create_table "project_attributes", force: :cascade do |t|
... ... @@ -29,30 +33,31 @@ ActiveRecord::Schema.define(version: 20150225170704) do
29 33 t.datetime "updated_at", null: false
30 34 end
31 35  
32   - create_table "reading_group_ownerships", force: :cascade do |t|
  36 + create_table "reading_group_attributes", force: :cascade do |t|
33 37 t.integer "user_id"
34 38 t.integer "reading_group_id"
35 39 t.datetime "created_at"
36 40 t.datetime "updated_at"
  41 + t.boolean "public", default: false
37 42 end
38 43  
39 44 create_table "users", force: :cascade do |t|
40   - t.string "name", limit: 255, default: "", null: false
41   - t.string "email", limit: 255, default: "", null: false
  45 + t.string "name", default: "", null: false
  46 + t.string "email", default: "", null: false
42 47 t.datetime "created_at"
43 48 t.datetime "updated_at"
44   - t.string "encrypted_password", limit: 255, default: "", null: false
45   - t.string "reset_password_token", limit: 255
  49 + t.string "encrypted_password", default: "", null: false
  50 + t.string "reset_password_token"
46 51 t.datetime "reset_password_sent_at"
47 52 t.datetime "remember_created_at"
48   - t.integer "sign_in_count", default: 0
  53 + t.integer "sign_in_count", default: 0
49 54 t.datetime "current_sign_in_at"
50 55 t.datetime "last_sign_in_at"
51   - t.string "current_sign_in_ip", limit: 255
52   - t.string "last_sign_in_ip", limit: 255
  56 + t.string "current_sign_in_ip"
  57 + t.string "last_sign_in_ip"
53 58 end
54 59  
55   - add_index "users", ["email"], name: "index_users_on_email", unique: true
56   - add_index "users", ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
  60 + add_index "users", ["email"], name: "index_users_on_email", unique: true, using: :btree
  61 + add_index "users", ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true, using: :btree
57 62  
58 63 end
... ...
spec/factories/kalibro_configuration_attributes.rb 0 → 100644
... ... @@ -0,0 +1,13 @@
  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 + association :user, strategy: :build
  6 + association :kalibro_configuration, :with_id, strategy: :build
  7 + self.public false
  8 +
  9 + trait :public do
  10 + self.public true
  11 + end
  12 + end
  13 +end
... ...
spec/factories/reading_group_attributes.rb 0 → 100644
... ... @@ -0,0 +1,13 @@
  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 + association :user, strategy: :build
  6 + association :reading_group, :with_id, strategy: :build
  7 + self.public false
  8 +
  9 + trait :public do
  10 + self.public true
  11 + end
  12 + end
  13 +end
... ...
spec/models/kalibro_configuration_attributes_spec.rb 0 → 100644
... ... @@ -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/reading_group_attributes_spec.rb 0 → 100644
... ... @@ -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
... ...