Skip to content

Commit

Permalink
try046
Browse files Browse the repository at this point in the history
Part 4: Multi db improvements, Basic API for connection switching
rails/rails#34052
  • Loading branch information
suketa committed Jun 29, 2019
1 parent 1e8bc19 commit 532b0c7
Show file tree
Hide file tree
Showing 11 changed files with 91 additions and 8 deletions.
3 changes: 3 additions & 0 deletions app/assets/stylesheets/dashboard.scss
@@ -0,0 +1,3 @@
// Place all the styles related to the Dashboard controller here.
// They will automatically be included in application.css.
// You can use Sass (SCSS) here: http://sass-lang.com/
7 changes: 7 additions & 0 deletions app/controllers/dashboard_controller.rb
@@ -0,0 +1,7 @@
class DashboardController < ApplicationController
def index
@books = Book.all
@users = User.all.to_a
@users_in_library = User.all_in_library
end
end
2 changes: 2 additions & 0 deletions app/helpers/dashboard_helper.rb
@@ -0,0 +1,2 @@
module DashboardHelper
end
3 changes: 3 additions & 0 deletions app/models/book.rb
@@ -0,0 +1,3 @@
class Book < ApplicationRecord
connects_to database: { writing: :library, reading: :library }
end
7 changes: 7 additions & 0 deletions app/models/user.rb
@@ -0,0 +1,7 @@
class User < ApplicationRecord
def self.all_in_library
ActiveRecord::Base.connected_to(database: :library) do
all
end
end
end
40 changes: 40 additions & 0 deletions app/views/dashboard/index.html.erb
@@ -0,0 +1,40 @@
<h1>Dashboard#index</h1>

<table>
<thead>
<tr>
<th>
valiable
</th>
<th>
value
</th>
</tr>
</thead>
<tbody>
<tr>
<td>
@books
</td>
<td>
<%= @books.map(&:title) %>
</td>
</tr>
<tr>
<td>
@users
</td>
<td>
<%= @users.map(&:name) %>
</td>
</tr>
<tr>
<td>
@users_in_library
</td>
<td>
<%= @users_in_library.map(&:name) %>
</td>
</tr>
</tbody>
</table>
1 change: 1 addition & 0 deletions config/routes.rb
@@ -1,3 +1,4 @@
Rails.application.routes.draw do
get 'dashboard/index'
# For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html
end
7 changes: 7 additions & 0 deletions db/library_migrate/20190629000054_create_user.rb
@@ -0,0 +1,7 @@
class CreateUser < ActiveRecord::Migration[6.0]
def change
create_table :users do |t|
t.string :name
end
end
end
6 changes: 5 additions & 1 deletion db/library_schema.rb
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 2019_06_08_225808) do
ActiveRecord::Schema.define(version: 2019_06_29_000054) do

# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
Expand All @@ -19,4 +19,8 @@
t.string "title"
end

create_table "users", force: :cascade do |t|
t.string "name"
end

end
14 changes: 7 additions & 7 deletions db/seeds.rb
@@ -1,7 +1,7 @@
# This file should contain all the record creation needed to seed the database with its default values.
# The data can then be loaded with the rails db:seed command (or created alongside the database with db:setup).
#
# Examples:
#
# movies = Movie.create([{ name: 'Star Wars' }, { name: 'Lord of the Rings' }])
# Character.create(name: 'Luke', movie: movies.first)
User.create(name: 'Taro')

ActiveRecord::Base.connected_to(database: :library) do
User.create(name: 'Hanako')
end

Book.create(title: 'Programming Ruby')
9 changes: 9 additions & 0 deletions test/controllers/dashboard_controller_test.rb
@@ -0,0 +1,9 @@
require 'test_helper'

class DashboardControllerTest < ActionDispatch::IntegrationTest
test "should get index" do
get dashboard_index_url
assert_response :success
end

end

0 comments on commit 532b0c7

Please sign in to comment.