From 485497334de20386d65893e8537a5fe127fc2a06 Mon Sep 17 00:00:00 2001 From: zmagod Date: Wed, 28 Sep 2016 14:18:52 +0200 Subject: [PATCH] generated notification related migrations and model tests --- app/models/notification.rb | 4 +++ app/models/user.rb | 4 +-- app/models/user_notification.rb | 4 +++ .../20160928114119_create_notifications.rb | 12 +++++++++ ...0160928114915_create_user_notifications.rb | 11 ++++++++ db/schema.rb | 25 ++++++++++++++++++- test/fixtures/notifications.yml | 11 ++++++++ test/fixtures/user_notifications.yml | 11 ++++++++ test/models/notification_test.rb | 12 +++++++++ test/models/user_notification_test.rb | 7 ++++++ 10 files changed, 98 insertions(+), 3 deletions(-) create mode 100644 app/models/notification.rb create mode 100644 app/models/user_notification.rb create mode 100644 db/migrate/20160928114119_create_notifications.rb create mode 100644 db/migrate/20160928114915_create_user_notifications.rb create mode 100644 test/fixtures/notifications.yml create mode 100644 test/fixtures/user_notifications.yml create mode 100644 test/models/notification_test.rb create mode 100644 test/models/user_notification_test.rb diff --git a/app/models/notification.rb b/app/models/notification.rb new file mode 100644 index 000000000..189950314 --- /dev/null +++ b/app/models/notification.rb @@ -0,0 +1,4 @@ +class Notification < ActiveRecord::Base + has_many :user_notifications, inverse_of: :notification + has_many :users, through: :user_notifications +end diff --git a/app/models/user.rb b/app/models/user.rb index f6ab98e29..b8ca5edfd 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -84,7 +84,8 @@ class User < ActiveRecord::Base has_many :added_protocols, class_name: 'Protocol', foreign_key: 'added_by_id', inverse_of: :added_by has_many :archived_protocols, class_name: 'Protocol', foreign_key: 'archived_by_id', inverse_of: :archived_by has_many :restored_protocols, class_name: 'Protocol', foreign_key: 'restored_by_id', inverse_of: :restored_by - + has_many :user_notifications, inverse_of: :user + has_many :notifications, through: :user_notifications # If other errors besides parameter "avatar" exist, # they will propagate to "avatar" also, so remove them # and put all other (more specific ones) in it @@ -260,4 +261,3 @@ class User < ActiveRecord::Base end end end - diff --git a/app/models/user_notification.rb b/app/models/user_notification.rb new file mode 100644 index 000000000..481610d3d --- /dev/null +++ b/app/models/user_notification.rb @@ -0,0 +1,4 @@ +class UserNotification < ActiveRecord::Base + belongs_to :user + belongs_to :notification +end diff --git a/db/migrate/20160928114119_create_notifications.rb b/db/migrate/20160928114119_create_notifications.rb new file mode 100644 index 000000000..8afa745fb --- /dev/null +++ b/db/migrate/20160928114119_create_notifications.rb @@ -0,0 +1,12 @@ +class CreateNotifications < ActiveRecord::Migration + def change + create_table :notifications do |t| + t.string :title + t.string :message + t.integer :type_of, null: false + + t.timestamps null: false + end + add_index :notifications, :created_at + end +end diff --git a/db/migrate/20160928114915_create_user_notifications.rb b/db/migrate/20160928114915_create_user_notifications.rb new file mode 100644 index 000000000..c241ef85c --- /dev/null +++ b/db/migrate/20160928114915_create_user_notifications.rb @@ -0,0 +1,11 @@ +class CreateUserNotifications < ActiveRecord::Migration + def change + create_table :user_notifications do |t| + t.belongs_to :user, index: true, foreign_key: true + t.belongs_to :notification, index: true, foreign_key: true + t.boolean :checked + + t.timestamps null: false + end + end +end diff --git a/db/schema.rb b/db/schema.rb index 4bb3282f7..02f84d72a 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20160809074757) do +ActiveRecord::Schema.define(version: 20160928114915) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -226,6 +226,16 @@ ActiveRecord::Schema.define(version: 20160809074757) do add_index "my_modules", ["name"], name: "index_my_modules_on_name", using: :gist add_index "my_modules", ["restored_by_id"], name: "index_my_modules_on_restored_by_id", using: :btree + create_table "notifications", force: :cascade do |t| + t.string "title" + t.string "message" + t.integer "type_of", null: false + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + end + + add_index "notifications", ["created_at"], name: "index_notifications_on_created_at", using: :btree + create_table "organizations", force: :cascade do |t| t.string "name", null: false t.datetime "created_at", null: false @@ -578,6 +588,17 @@ ActiveRecord::Schema.define(version: 20160809074757) do add_index "user_my_modules", ["my_module_id"], name: "index_user_my_modules_on_my_module_id", using: :btree add_index "user_my_modules", ["user_id"], name: "index_user_my_modules_on_user_id", using: :btree + create_table "user_notifications", force: :cascade do |t| + t.integer "user_id" + t.integer "notification_id" + t.boolean "checked" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + end + + add_index "user_notifications", ["notification_id"], name: "index_user_notifications_on_notification_id", using: :btree + add_index "user_notifications", ["user_id"], name: "index_user_notifications_on_user_id", using: :btree + create_table "user_organizations", force: :cascade do |t| t.integer "role", default: 1, null: false t.integer "user_id", null: false @@ -758,6 +779,8 @@ ActiveRecord::Schema.define(version: 20160809074757) do add_foreign_key "user_my_modules", "my_modules" add_foreign_key "user_my_modules", "users" add_foreign_key "user_my_modules", "users", column: "assigned_by_id" + add_foreign_key "user_notifications", "notifications" + add_foreign_key "user_notifications", "users" add_foreign_key "user_organizations", "organizations" add_foreign_key "user_organizations", "users" add_foreign_key "user_organizations", "users", column: "assigned_by_id" diff --git a/test/fixtures/notifications.yml b/test/fixtures/notifications.yml new file mode 100644 index 000000000..90809b963 --- /dev/null +++ b/test/fixtures/notifications.yml @@ -0,0 +1,11 @@ +# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html + +one: + title: MyString + message: MyString + type_of: 1 + +two: + title: MyString + message: MyString + type_of: 1 diff --git a/test/fixtures/user_notifications.yml b/test/fixtures/user_notifications.yml new file mode 100644 index 000000000..9a7bc7e30 --- /dev/null +++ b/test/fixtures/user_notifications.yml @@ -0,0 +1,11 @@ +# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html + +one: + user_id: + notification_id: + checked: false + +two: + user_id: + notification_id: + checked: false diff --git a/test/models/notification_test.rb b/test/models/notification_test.rb new file mode 100644 index 000000000..e8c8a9680 --- /dev/null +++ b/test/models/notification_test.rb @@ -0,0 +1,12 @@ +require 'test_helper' + +class NotificationTest < ActiveSupport::TestCase + should have_db_column(:title).of_type(:string) + should have_db_column(:message).of_type(:text) + should have_db_column(:type_of).of_type(:integer) + should have_db_column(:created_at).of_type(:datetime) + should have_db_column(:updated_at).of_type(:datetime) + + should have_many(:user_notifications) + should have_many(:users) +end diff --git a/test/models/user_notification_test.rb b/test/models/user_notification_test.rb new file mode 100644 index 000000000..3a94905ae --- /dev/null +++ b/test/models/user_notification_test.rb @@ -0,0 +1,7 @@ +require 'test_helper' + +class UserNotificationTest < ActiveSupport::TestCase + should have_db_column(:checked).of_type(:boolean) + should belong_to(:user) + should belong_to(:notification) +end