-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
43ee2ab
commit b873cf6
Showing
12 changed files
with
107 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
module SqlView | ||
VERSION = "0.0.5" | ||
VERSION = "0.0.6" | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,6 @@ | ||
class Account < ApplicationRecord | ||
has_many :users | ||
|
||
has_one :account_stat_view, class_name: AccountStatViewView.model.to_s, foreign_key: :account_id | ||
has_many :active_users, join_table: :active_users_views, class_name: ActiveUserView.model.to_s, foreign_key: :account_id | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
class User < ApplicationRecord | ||
belongs_to :account, optional: true | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
class AccountStatViewView < SQLView::Model | ||
|
||
schema -> { | ||
"SELECT accounts.id as account_id, RANDOM() as factor from accounts" | ||
} | ||
|
||
extend_model_with do | ||
# sample how you can extend it, similar to regular AR model | ||
# | ||
# include SomeConcern | ||
# | ||
# belongs_to :user | ||
# has_many :posts | ||
# | ||
# scope :ordered, -> { order(:created_at) } | ||
# scope :by_role, ->(role) { where(role: role) } | ||
end | ||
end |
5 changes: 5 additions & 0 deletions
5
test/dummy/db/migrate/20230618100146_add_account_id_to_user.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
class AddAccountIdToUser < ActiveRecord::Migration[7.0] | ||
def change | ||
add_column :users, :account_id, :integer | ||
end | ||
end |
9 changes: 9 additions & 0 deletions
9
test/dummy/db/migrate/20230618101748_create_account_stat_view_view.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
class CreateAccountStatViewView < ActiveRecord::Migration[7.0] | ||
def up | ||
AccountStatViewView.sql_view.up | ||
end | ||
|
||
def down | ||
AccountStatViewView.sql_view.down | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
class RefreshActiveUsers < ActiveRecord::Migration[7.0] | ||
def change | ||
ActiveUserView.sql_view.down | ||
ActiveUserView.sql_view.up | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters