Commit e2ac9d3

mo <mo.khan@gmail.com>
2018-09-05 01:33:24
create clients model
1 parent cc091b7
app/controllers/my/clients_controller.rb
@@ -0,0 +1,22 @@
+# frozen_string_literal: true
+
+module My
+  class ClientsController < ApplicationController
+    def index; end
+
+    def new
+      @client = Client.new
+    end
+
+    def create
+      Client.create!(secure_params)
+      redirect_to my_clients_path, notice: "Client successfully created!"
+    end
+
+    private
+
+    def secure_params
+      params.require(:client).permit(:name, :secret, :redirect_uri)
+    end
+  end
+end
app/models/client.rb
@@ -0,0 +1,14 @@
+# frozen_string_literal: true
+
+class Client < ApplicationRecord
+  has_secure_token :secret
+
+  after_initialize do
+    self.uuid = SecureRandom.uuid unless uuid
+    self.secret = self.class.generate_unique_secure_token unless secret
+  end
+
+  def to_param
+    uuid
+  end
+end
app/views/my/clients/index.html.erb
app/views/my/clients/new.html.erb
@@ -0,0 +1,20 @@
+<div class="container">
+  <div class="row">
+    <div class="col">
+      <h1>Client</h1>
+      <div data-controller="clients--new">
+        <p>Client Id: <%= @client.to_param %></p>
+        <p>Secret: <%= @client.secret %></p>
+
+        <%= form_for @client, url: my_clients_path, method: :post do |form| %>
+          <%= form.label :name %>
+          <%= form.text_field :name, data: { target: 'clients--new.name' } %>
+          <%= form.label :redirect_uri %>
+          <%= form.url_field :redirect_uri, data: { target: 'clients--new.redirect_uri' } %>
+          <%= form.submit t(".enable"), class: 'btn btn-primary', data: { disable_with: 'Saving…' } %>
+          <%= link_to t(".cancel"), my_clients_path, class: 'btn' %>
+        <% end %>
+      </div>
+    </div>
+  </div>
+</div>
config/environments/development.rb
@@ -63,6 +63,4 @@ Rails.application.configure do
   # Use an evented file watcher to asynchronously detect changes in source code,
   # routes, locales, etc. This feature depends on the listen gem.
   config.file_watcher = ActiveSupport::EventedFileUpdateChecker
-
-  config.action_view.raise_on_missing_translations = true
 end
config/locales/en.yml
@@ -5,6 +5,10 @@ en:
       loading: Loading
       login: Login
   my:
+    clients:
+      new:
+        cancel: Cancel
+        enable: Enable
     mfas:
       new:
         cancel: Cancel
config/routes.rb
@@ -11,7 +11,9 @@ Rails.application.routes.draw do
   namespace :my do
     resource :dashboard, only: [:show]
     resource :mfa, only: [:show, :new, :edit, :create, :destroy]
+    resources :clients, only: [:index, :new, :create]
   end
+
   namespace :scim do
     namespace :v2, defaults: { format: :scim } do
       post ".search", to: "search#index"
db/migrate/20180905011437_create_clients.rb
@@ -0,0 +1,13 @@
+# frozen_string_literal: true
+
+class CreateClients < ActiveRecord::Migration[5.2]
+  def change
+    create_table :clients do |t|
+      t.string :uuid, null: false, index: true
+      t.string :name, null: false
+      t.string :secret, null: false
+      t.string :redirect_uri, null: false
+      t.timestamps null: false
+    end
+  end
+end
db/schema.rb
@@ -10,7 +10,17 @@
 #
 # It's strongly recommended that you check this file into your version control system.
 
-ActiveRecord::Schema.define(version: 2018_09_05_005659) do
+ActiveRecord::Schema.define(version: 2018_09_05_011437) do
+
+  create_table "clients", force: :cascade do |t|
+    t.string "uuid", null: false
+    t.string "name", null: false
+    t.string "secret", null: false
+    t.string "redirect_uri", null: false
+    t.datetime "created_at", null: false
+    t.datetime "updated_at", null: false
+    t.index ["uuid"], name: "index_clients_on_uuid"
+  end
 
   create_table "sessions", force: :cascade do |t|
     t.string "session_id", null: false
spec/requests/my/clients_spec.rb
@@ -0,0 +1,34 @@
+require 'rails_helper'
+
+RSpec.describe '/my/clients' do
+  context "when logged in" do
+    let(:current_user) { create(:user) }
+    before { http_login(current_user) }
+
+    describe "GET /my/clients" do
+      before { get '/my/clients' }
+
+      specify { expect(response).to have_http_status(:ok) }
+    end
+
+    describe "GET /my/clients/new" do
+      before { get '/my/clients/new' }
+
+      specify { expect(response).to have_http_status(:ok) }
+      specify { expect(response.body).to include('Client Id') }
+      specify { expect(response.body).to include('Secret') }
+    end
+
+    describe "POST /my/clients" do
+      context "when the request data is valid" do
+        let(:attributes) { attributes_for(:client) }
+
+        before { post '/my/clients', params: { client: attributes } }
+
+        specify { expect(response).to redirect_to(my_clients_path) }
+        specify { expect(flash[:notice]).to include('success') }
+        specify { expect(Client.count).to eql(1) }
+      end
+    end
+  end
+end
spec/factories.rb
@@ -1,4 +1,10 @@
 FactoryBot.define do
+  factory :client do
+    uuid { SecureRandom.uuid }
+    name { FFaker::Name.name }
+    redirect_uri { FFaker::Internet.uri('https') }
+  end
+
   factory :user do
     email { FFaker::Internet.email }
     uuid { SecureRandom.uuid }