From f4e8350db1e74da3c279a964120f6a9ce99c0633 Mon Sep 17 00:00:00 2001 From: Stephanie Xu <51296329+Aphexis@users.noreply.github.com> Date: Mon, 30 Mar 2020 20:43:07 -0400 Subject: [PATCH] Admin Dashboard (#54) * add flowchart_node_id to seed data * add active admin * add icons to activeadmin * add flowchart node form * add icon_helper resource, dashboard * modified flowchart create vs. edit form * add link to icons in node view * linting and test fixes * linting fixes --- Gemfile | 1 + Gemfile.lock | 57 +++ app/admin/admin_users.rb | 29 ++ app/admin/dashboard.rb | 33 ++ app/admin/flowchart_icon_helpers.rb | 18 + app/admin/flowchart_icons.rb | 5 + app/admin/flowchart_nodes.rb | 20 ++ app/admin/flowcharts.rb | 50 +++ app/assets/javascripts/active_admin.js | 1 + app/assets/stylesheets/active_admin.scss | 17 + app/controllers/application_controller.rb | 4 +- app/models/admin_user.rb | 27 ++ app/models/flowchart.rb | 5 +- app/models/flowchart_icon.rb | 4 + app/models/flowchart_node.rb | 7 +- app/models/user.rb | 24 -- config/application.rb | 9 +- config/initializers/active_admin.rb | 330 ++++++++++++++++++ config/routes.rb | 17 +- db/migrate/20200313010735_drop_users.rb | 7 + ...0200313011036_devise_create_admin_users.rb | 43 +++ ...0313011046_create_active_admin_comments.rb | 18 + db/schema.rb | 36 +- db/seeds.rb | 11 +- spec/factories/admin_users.rb | 25 ++ spec/models/admin_user_spec.rb | 26 ++ spec/models/user_spec.rb | 42 --- 27 files changed, 770 insertions(+), 96 deletions(-) create mode 100644 app/admin/admin_users.rb create mode 100644 app/admin/dashboard.rb create mode 100644 app/admin/flowchart_icon_helpers.rb create mode 100644 app/admin/flowchart_icons.rb create mode 100644 app/admin/flowchart_nodes.rb create mode 100644 app/admin/flowcharts.rb create mode 100644 app/assets/javascripts/active_admin.js create mode 100644 app/assets/stylesheets/active_admin.scss create mode 100644 app/models/admin_user.rb delete mode 100644 app/models/user.rb create mode 100644 config/initializers/active_admin.rb create mode 100644 db/migrate/20200313010735_drop_users.rb create mode 100644 db/migrate/20200313011036_devise_create_admin_users.rb create mode 100644 db/migrate/20200313011046_create_active_admin_comments.rb create mode 100644 spec/factories/admin_users.rb create mode 100644 spec/models/admin_user_spec.rb delete mode 100644 spec/models/user_spec.rb diff --git a/Gemfile b/Gemfile index 87b4aff..d9305ea 100644 --- a/Gemfile +++ b/Gemfile @@ -47,6 +47,7 @@ end # Windows does not include zoneinfo files, so bundle the tzinfo-data gem gem 'tzinfo-data', platforms: %i[mingw mswin x64_mingw jruby] +gem 'activeadmin' gem 'devise' gem 'devise-jwt', '~> 0.5.9' gem 'pg', '~> 1.1' diff --git a/Gemfile.lock b/Gemfile.lock index ed1b7c0..0b6ed55 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -37,6 +37,17 @@ GEM erubi (~> 1.4) rails-dom-testing (~> 2.0) rails-html-sanitizer (~> 1.1, >= 1.2.0) + activeadmin (2.6.1) + arbre (~> 1.2, >= 1.2.1) + formtastic (~> 3.1) + formtastic_i18n (~> 0.4) + inherited_resources (~> 1.7) + jquery-rails (~> 4.2) + kaminari (~> 1.0, >= 1.0.1) + railties (>= 5.2, < 6.1) + ransack (~> 2.1, >= 2.1.1) + sassc-rails (~> 2.1) + sprockets (>= 3.0, < 4.1) activejob (6.0.0) activesupport (= 6.0.0) globalid (>= 0.3.6) @@ -59,6 +70,8 @@ GEM annotate (3.0.3) activerecord (>= 3.2, < 7.0) rake (>= 10.4, < 14.0) + arbre (1.2.1) + activesupport (>= 3.0.0) ast (2.4.0) bcrypt (3.1.13) bootsnap (1.4.5) @@ -100,12 +113,39 @@ GEM railties (>= 4.2.0) ffi (1.11.1) ffi (1.11.1-x64-mingw32) + formtastic (3.1.5) + actionpack (>= 3.2.13) + formtastic_i18n (0.6.0) globalid (0.4.2) activesupport (>= 4.2.0) + has_scope (0.7.2) + actionpack (>= 4.1) + activesupport (>= 4.1) i18n (1.6.0) concurrent-ruby (~> 1.0) + inherited_resources (1.11.0) + actionpack (>= 5.0, < 6.1) + has_scope (~> 0.6) + railties (>= 5.0, < 6.1) + responders (>= 2, < 4) jaro_winkler (1.5.3) + jquery-rails (4.3.5) + rails-dom-testing (>= 1, < 3) + railties (>= 4.2.0) + thor (>= 0.14, < 2.0) jwt (2.2.1) + kaminari (1.2.0) + activesupport (>= 4.1.0) + kaminari-actionview (= 1.2.0) + kaminari-activerecord (= 1.2.0) + kaminari-core (= 1.2.0) + kaminari-actionview (1.2.0) + actionview + kaminari-core (= 1.2.0) + kaminari-activerecord (1.2.0) + activerecord + kaminari-core (= 1.2.0) + kaminari-core (1.2.0) listen (3.1.5) rb-fsevent (~> 0.9, >= 0.9.4) rb-inotify (~> 0.9, >= 0.9.7) @@ -135,6 +175,8 @@ GEM ast (~> 2.4.0) pg (1.1.4) pg (1.1.4-x64-mingw32) + polyamorous (2.3.2) + activerecord (>= 5.2.1) puma (3.12.1) rack (2.0.7) rack-cors (1.0.5) @@ -169,6 +211,11 @@ GEM thor (>= 0.20.3, < 2.0) rainbow (3.0.0) rake (13.0.0) + ransack (2.3.2) + activerecord (>= 5.2.1) + activesupport (>= 5.2.1) + i18n + polyamorous (= 2.3.2) rb-fsevent (0.10.3) rb-inotify (0.10.0) ffi (~> 1.0) @@ -201,6 +248,14 @@ GEM unicode-display_width (>= 1.4.0, < 1.7) ruby-progressbar (1.10.1) ruby_dep (1.5.0) + sassc (2.2.1-x64-mingw32) + ffi (~> 1.9) + sassc-rails (2.1.2) + railties (>= 4.0.0) + sassc (>= 2.0) + sprockets (> 3.0) + sprockets-rails + tilt spring (2.1.0) spring-watcher-listen (2.0.1) listen (>= 2.7, < 4.0) @@ -214,6 +269,7 @@ GEM sprockets (>= 3.0.0) thor (0.20.3) thread_safe (0.3.6) + tilt (2.0.10) tzinfo (1.2.5) thread_safe (~> 0.1) tzinfo-data (1.2019.3) @@ -236,6 +292,7 @@ PLATFORMS x64-mingw32 DEPENDENCIES + activeadmin annotate bootsnap (>= 1.4.2) byebug diff --git a/app/admin/admin_users.rb b/app/admin/admin_users.rb new file mode 100644 index 0000000..78e454e --- /dev/null +++ b/app/admin/admin_users.rb @@ -0,0 +1,29 @@ +# frozen_string_literal: true + +ActiveAdmin.register AdminUser do + permit_params :email, :password, :password_confirmation + + index do + selectable_column + id_column + column :email + column :current_sign_in_at + column :sign_in_count + column :created_at + actions + end + + filter :email + filter :current_sign_in_at + filter :sign_in_count + filter :created_at + + form do |f| + f.inputs do + f.input :email + f.input :password + f.input :password_confirmation + end + f.actions + end +end diff --git a/app/admin/dashboard.rb b/app/admin/dashboard.rb new file mode 100644 index 0000000..0ca8c86 --- /dev/null +++ b/app/admin/dashboard.rb @@ -0,0 +1,33 @@ +# frozen_string_literal: true + +ActiveAdmin.register_page 'Dashboard' do + menu priority: 1, label: proc { I18n.t('active_admin.dashboard') } + + content title: proc { I18n.t('active_admin.dashboard') } do + columns do + column do + panel 'Recent Flowcharts' do + table_for Flowchart.order('id').limit(10) do + column('Title', &:title) + column('Description', &:description) + end + end + end + column do + panel 'Recent Icons' do + table_for FlowchartIcon.order('id').limit(10) do + column('ID', &:id) + column('URL', &:url) + end + end + end + end + columns do + column do + panel 'Info' do + para 'Welcome to the admin dashboard for SDC flowcharts!' + end + end + end + end +end diff --git a/app/admin/flowchart_icon_helpers.rb b/app/admin/flowchart_icon_helpers.rb new file mode 100644 index 0000000..bb8605b --- /dev/null +++ b/app/admin/flowchart_icon_helpers.rb @@ -0,0 +1,18 @@ +# frozen_string_literal: true + +ActiveAdmin.register FlowchartIconHelper, as: 'Icon Connectors' do + # See permitted parameters documentation: + # https://github.com/activeadmin/activeadmin/blob/master/docs/2-resource-customization.md#setting-up-strong-parameters + # + # Uncomment all parameters which should be permitted for assignment + # + permit_params :flowchart_node_id, :flowchart_icon_id + # + # or + # + # permit_params do + # permitted = [:flowchart_node_id, :flowchart_icon_id] + # permitted << :other if params[:action] == 'create' && current_user.admin? + # permitted + # end +end diff --git a/app/admin/flowchart_icons.rb b/app/admin/flowchart_icons.rb new file mode 100644 index 0000000..432e2a5 --- /dev/null +++ b/app/admin/flowchart_icons.rb @@ -0,0 +1,5 @@ +# frozen_string_literal: true + +ActiveAdmin.register FlowchartIcon, as: 'Icon' do + permit_params :url +end diff --git a/app/admin/flowchart_nodes.rb b/app/admin/flowchart_nodes.rb new file mode 100644 index 0000000..58aea78 --- /dev/null +++ b/app/admin/flowchart_nodes.rb @@ -0,0 +1,20 @@ +# frozen_string_literal: true + +ActiveAdmin.register FlowchartNode do + sidebar 'Flowchart Details', only: %i[show edit] do + ul do + li link_to 'Node Icons', admin_icon_connectors_path('q[flowchart_node_id_eq]' => flowchart_node.id) + end + end + + belongs_to :flowchart + permit_params :id, :text, :header, :next_question, :is_root, :flowchart_id, :deleted, :flowchart_node_id, :flowchart_id, :is_leaf, :breadcrumb_title, + flowchart_icon_helpers_attributes: %i[id flowchart_icon_id flowchart_node_id _destroy] + + controller do + def permitted_params + params.permit :authenticity_token, :commit, :_method, :flowchart_id, :id, flowchart_node: [:id, :text, :header, :breadcrumb_title, :next_question, :is_root, :flowchart_id, :flowchart_node_id, :is_leaf, :deleted, :flowchart_id, + flowchart_icon_helpers_attributes: %i[id flowchart_icon_id flowchart_node_id _destroy]] + end + end +end diff --git a/app/admin/flowcharts.rb b/app/admin/flowcharts.rb new file mode 100644 index 0000000..2586852 --- /dev/null +++ b/app/admin/flowcharts.rb @@ -0,0 +1,50 @@ +# frozen_string_literal: true + +ActiveAdmin.register Flowchart do + sidebar 'Flowchart Details', only: %i[show edit] do + ul do + li link_to 'Flowchart Nodes', admin_flowchart_flowchart_nodes_path(resource) + end + end + + permit_params :id, :title, :description, :root_id, :created_at, :updated_at, :deleted, :height, + flowchart_node_attributes: [:id, :text, :header, :button_text, :next_question, :child_id, :sibling_id, :is_root, :flowchart_id, :flowchart_node_id, :deleted, :_destroy, + flowchart_icon_helpers_attributes: %i[id flowchart_icon_id flowchart_node_id _destroy]] + + controller do + def permitted_params + params.permit :authenticity_token, :commit, flowchart: [:id, :title, :description, :root_id, :created_at, :updated_at, :deleted, :height, + flowchart_nodes_attributes: [:id, :text, :header, :button_text, :next_question, :is_root, :flowchart_id, :flowchart_node_id, :deleted, :_destroy, + flowchart_icon_helpers_attributes: %i[id flowchart_icon_id flowchart_node_id _destroy]]] + end + end + + form do |f| + f.inputs 'Details' do + f.input :title, label: 'Flowchart title' + f.input :description, label: 'Description' + f.hidden_field :height, value: 1 + unless f.object.new_record? + f.input :root_id, label: 'Root Node ID' + end + end + unless f.object.new_record? + f.inputs 'Flowchart Nodes' do + f.has_many :flowchart_nodes, new_record: true, allow_destroy: true, heading: false do |n| + n.input :id, label: 'Node ID', input_html: { disabled: true } + n.input :header, label: 'Node Title' + n.input :text, label: 'Node Text' + n.input :next_question, label: 'Node Question' + n.input :breadcrumb_title, label: 'Breadcrumb Title' + n.input :parent, label: 'Parent Node', as: :select, collection: FlowchartNode.select(:header).where(flowchart_id: f.object.id) + n.input :is_root, label: 'Root Node?' + n.input :is_leaf, label: 'Leaf Node?' + n.has_many :flowchart_icon_helpers, new_record: 'Add Icon', allow_destroy: true, heading: 'Node Icons' do |i| + i.input :flowchart_icon, label: 'Icon ID' + end + end + end + end + f.actions + end +end diff --git a/app/assets/javascripts/active_admin.js b/app/assets/javascripts/active_admin.js new file mode 100644 index 0000000..d2b66c5 --- /dev/null +++ b/app/assets/javascripts/active_admin.js @@ -0,0 +1 @@ +//= require active_admin/base diff --git a/app/assets/stylesheets/active_admin.scss b/app/assets/stylesheets/active_admin.scss new file mode 100644 index 0000000..d3c61f3 --- /dev/null +++ b/app/assets/stylesheets/active_admin.scss @@ -0,0 +1,17 @@ +// SASS variable overrides must be declared before loading up Active Admin's styles. +// +// To view the variables that Active Admin provides, take a look at +// `app/assets/stylesheets/active_admin/mixins/_variables.scss` in the +// Active Admin source. +// +// For example, to change the sidebar width: +// $sidebar-width: 242px; + +// Active Admin's got SASS! +@import "active_admin/mixins"; +@import "active_admin/base"; + +// Overriding any non-variable SASS must be done after the fact. +// For example, to change the default status-tag color: +// +// .status_tag { background: #6090DB; } diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 40e0d43..2ba6503 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -1,7 +1,7 @@ # frozen_string_literal: true -class ApplicationController < ActionController::API - respond_to :json +class ApplicationController < ActionController::Base + respond_to :json, :html def render_resource(resource) if resource.errors.empty? render json: resource diff --git a/app/models/admin_user.rb b/app/models/admin_user.rb new file mode 100644 index 0000000..b51341e --- /dev/null +++ b/app/models/admin_user.rb @@ -0,0 +1,27 @@ +# frozen_string_literal: true + +# == Schema Information +# +# Table name: admin_users +# +# id :bigint not null, primary key +# email :string default(""), not null +# encrypted_password :string default(""), not null +# remember_created_at :datetime +# reset_password_sent_at :datetime +# reset_password_token :string +# created_at :datetime not null +# updated_at :datetime not null +# +# Indexes +# +# index_admin_users_on_email (email) UNIQUE +# index_admin_users_on_reset_password_token (reset_password_token) UNIQUE +# + +class AdminUser < ApplicationRecord + # Include default devise modules. Others available are: + # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable + devise :database_authenticatable, + :recoverable, :rememberable, :validatable +end diff --git a/app/models/flowchart.rb b/app/models/flowchart.rb index 807d7e4..c8fe21c 100644 --- a/app/models/flowchart.rb +++ b/app/models/flowchart.rb @@ -23,9 +23,12 @@ # class Flowchart < ApplicationRecord + has_many :flowchart_nodes + # attr_accessible :flowchart_nodes_attributes + accepts_nested_attributes_for :flowchart_nodes, allow_destroy: true validates :title, presence: true validates :description, presence: true - validates :height, numericality: { only_integer: true, greater_than_or_equal_to: 0 }, presence: true + # validates :height, numericality: { only_integer: true, greater_than_or_equal_to: 0 }, presence: true validates :root_id, numericality: { only_integer: true, greater_than_or_equal_to: 0 }, allow_nil: true validates :deleted, inclusion: { in: [true, false] }, allow_nil: true # defaults to false diff --git a/app/models/flowchart_icon.rb b/app/models/flowchart_icon.rb index e208687..8bb5040 100644 --- a/app/models/flowchart_icon.rb +++ b/app/models/flowchart_icon.rb @@ -13,4 +13,8 @@ class FlowchartIcon < ApplicationRecord has_many :flowchart_icon_helpers has_many :flowchart_node, through: :flowchart_icon_helpers + + def display_name + url + end end diff --git a/app/models/flowchart_node.rb b/app/models/flowchart_node.rb index daadc05..05a64e1 100644 --- a/app/models/flowchart_node.rb +++ b/app/models/flowchart_node.rb @@ -37,8 +37,9 @@ class FlowchartNode < ApplicationRecord belongs_to :flowchart - belongs_to :parent, class_name: 'FlowchartNode', optional: true + belongs_to :parent, class_name: 'FlowchartNode', optional: true, foreign_key: 'flowchart_node_id' has_many :flowchart_icon_helpers + accepts_nested_attributes_for :flowchart_icon_helpers, allow_destroy: true has_many :flowchart_icons, through: :flowchart_icon_helpers validates :text, presence: true validates :header, presence: true @@ -106,6 +107,10 @@ def children FlowchartNode.where(flowchart_node_id: id).find_each end + def display_name + header + end + def as_json(_options = {}) super(include: :flowchart_icons) end diff --git a/app/models/user.rb b/app/models/user.rb deleted file mode 100644 index 4518f7c..0000000 --- a/app/models/user.rb +++ /dev/null @@ -1,24 +0,0 @@ -# frozen_string_literal: true - -# == Schema Information -# -# Table name: users -# -# id :bigint not null, primary key -# email :string default(""), not null -# encrypted_password :string default(""), not null -# jti :string not null -# created_at :datetime not null -# updated_at :datetime not null -# -# Indexes -# -# index_users_on_email (email) UNIQUE -# index_users_on_jti (jti) UNIQUE -# - -class User < ApplicationRecord - include Devise::JWT::RevocationStrategies::JTIMatcher - devise :database_authenticatable, :validatable, :rememberable, - :jwt_authenticatable, jwt_revocation_strategy: self -end diff --git a/config/application.rb b/config/application.rb index 4d8f0eb..f534037 100644 --- a/config/application.rb +++ b/config/application.rb @@ -14,7 +14,7 @@ require 'action_text/engine' require 'action_view/railtie' require 'action_cable/engine' -# require "sprockets/railtie" +require 'sprockets/railtie' require 'rails/test_unit/railtie' # Require the gems listed in Gemfile, including any gems @@ -37,6 +37,13 @@ class Application < Rails::Application # Middleware like session, flash, cookies can be added back manually. # Skip views, helpers and assets when generating a new resource. config.api_only = true + + # Middleware for ActiveAdmin + config.middleware.use Rack::MethodOverride + config.middleware.use ActionDispatch::Flash + config.middleware.use ActionDispatch::Cookies + config.middleware.use ActionDispatch::Session::CookieStore + config.middleware.insert_before 0, Rack::Cors do allow do origins '*' diff --git a/config/initializers/active_admin.rb b/config/initializers/active_admin.rb new file mode 100644 index 0000000..65eee8a --- /dev/null +++ b/config/initializers/active_admin.rb @@ -0,0 +1,330 @@ +# frozen_string_literal: true + +ActiveAdmin.setup do |config| + # == Site Title + # + # Set the title that is displayed on the main layout + # for each of the active admin pages. + # + config.site_title = 'SDC API' + + # Set the link url for the title. For example, to take + # users to your main site. Defaults to no link. + # + # config.site_title_link = "/" + + # Set an optional image to be displayed for the header + # instead of a string (overrides :site_title) + # + # Note: Aim for an image that's 21px high so it fits in the header. + # + # config.site_title_image = "logo.png" + + # == Default Namespace + # + # Set the default namespace each administration resource + # will be added to. + # + # eg: + # config.default_namespace = :hello_world + # + # This will create resources in the HelloWorld module and + # will namespace routes to /hello_world/* + # + # To set no namespace by default, use: + # config.default_namespace = false + # + # Default: + # config.default_namespace = :admin + # + # You can customize the settings for each namespace by using + # a namespace block. For example, to change the site title + # within a namespace: + # + # config.namespace :admin do |admin| + # admin.site_title = "Custom Admin Title" + # end + # + # This will ONLY change the title for the admin section. Other + # namespaces will continue to use the main "site_title" configuration. + + # == User Authentication + # + # Active Admin will automatically call an authentication + # method in a before filter of all controller actions to + # ensure that there is a currently logged in admin user. + # + # This setting changes the method which Active Admin calls + # within the application controller. + config.authentication_method = :authenticate_admin_user! + + # == User Authorization + # + # Active Admin will automatically call an authorization + # method in a before filter of all controller actions to + # ensure that there is a user with proper rights. You can use + # CanCanAdapter or make your own. Please refer to documentation. + # config.authorization_adapter = ActiveAdmin::CanCanAdapter + + # In case you prefer Pundit over other solutions you can here pass + # the name of default policy class. This policy will be used in every + # case when Pundit is unable to find suitable policy. + # config.pundit_default_policy = "MyDefaultPunditPolicy" + + # If you wish to maintain a separate set of Pundit policies for admin + # resources, you may set a namespace here that Pundit will search + # within when looking for a resource's policy. + # config.pundit_policy_namespace = :admin + + # You can customize your CanCan Ability class name here. + # config.cancan_ability_class = "Ability" + + # You can specify a method to be called on unauthorized access. + # This is necessary in order to prevent a redirect loop which happens + # because, by default, user gets redirected to Dashboard. If user + # doesn't have access to Dashboard, he'll end up in a redirect loop. + # Method provided here should be defined in application_controller.rb. + # config.on_unauthorized_access = :access_denied + + # == Current User + # + # Active Admin will associate actions with the current + # user performing them. + # + # This setting changes the method which Active Admin calls + # (within the application controller) to return the currently logged in user. + config.current_user_method = :current_admin_user + + # == Logging Out + # + # Active Admin displays a logout link on each screen. These + # settings configure the location and method used for the link. + # + # This setting changes the path where the link points to. If it's + # a string, the strings is used as the path. If it's a Symbol, we + # will call the method to return the path. + # + # Default: + config.logout_link_path = :destroy_admin_user_session_path + + # This setting changes the http method used when rendering the + # link. For example :get, :delete, :put, etc.. + # + # Default: + # config.logout_link_method = :get + + # == Root + # + # Set the action to call for the root path. You can set different + # roots for each namespace. + # + # Default: + # config.root_to = 'dashboard#index' + + # == Admin Comments + # + # This allows your users to comment on any resource registered with Active Admin. + # + # You can completely disable comments: + config.comments = false + # + # You can change the name under which comments are registered: + # config.comments_registration_name = 'AdminComment' + # + # You can change the order for the comments and you can change the column + # to be used for ordering: + # config.comments_order = 'created_at ASC' + # + # You can disable the menu item for the comments index page: + config.comments_menu = false + # + # You can customize the comment menu: + # config.comments_menu = { parent: 'Admin', priority: 1 } + + # == Batch Actions + # + # Enable and disable Batch Actions + # + config.batch_actions = true + + # == Controller Filters + # + # You can add before, after and around filters to all of your + # Active Admin resources and pages from here. + # + # config.before_action :do_something_awesome + + # == Attribute Filters + # + # You can exclude possibly sensitive model attributes from being displayed, + # added to forms, or exported by default by ActiveAdmin + # + config.filter_attributes = %i[encrypted_password password password_confirmation] + + # == Localize Date/Time Format + # + # Set the localize format to display dates and times. + # To understand how to localize your app with I18n, read more at + # https://guides.rubyonrails.org/i18n.html + # + # You can run `bin/rails runner 'puts I18n.t("date.formats")'` to see the + # available formats in your application. + # + config.localize_format = :long + + # == Setting a Favicon + # + # config.favicon = 'favicon.ico' + + # == Meta Tags + # + # Add additional meta tags to the head element of active admin pages. + # + # Add tags to all pages logged in users see: + # config.meta_tags = { author: 'My Company' } + + # By default, sign up/sign in/recover password pages are excluded + # from showing up in search engine results by adding a robots meta + # tag. You can reset the hash of meta tags included in logged out + # pages: + # config.meta_tags_for_logged_out_pages = {} + + # == Removing Breadcrumbs + # + # Breadcrumbs are enabled by default. You can customize them for individual + # resources or you can disable them globally from here. + # + # config.breadcrumb = false + + # == Create Another Checkbox + # + # Create another checkbox is disabled by default. You can customize it for individual + # resources or you can enable them globally from here. + # + # config.create_another = true + + # == Register Stylesheets & Javascripts + # + # We recommend using the built in Active Admin layout and loading + # up your own stylesheets / javascripts to customize the look + # and feel. + # + # To load a stylesheet: + # config.register_stylesheet 'my_stylesheet.css' + # + # You can provide an options hash for more control, which is passed along to stylesheet_link_tag(): + # config.register_stylesheet 'my_print_stylesheet.css', media: :print + # + # To load a javascript file: + # config.register_javascript 'my_javascript.js' + + # == CSV options + # + # Set the CSV builder separator + # config.csv_options = { col_sep: ';' } + # + # Force the use of quotes + # config.csv_options = { force_quotes: true } + + # == Menu System + # + # You can add a navigation menu to be used in your application, or configure a provided menu + # + # To change the default utility navigation to show a link to your website & a logout btn + # + # config.namespace :admin do |admin| + # admin.build_menu :utility_navigation do |menu| + # menu.add label: "My Great Website", url: "http://www.mygreatwebsite.com", html_options: { target: :blank } + # admin.add_logout_button_to_menu menu + # end + # end + # + # If you wanted to add a static menu item to the default menu provided: + # + # config.namespace :admin do |admin| + # admin.build_menu :default do |menu| + # menu.add label: "My Great Website", url: "http://www.mygreatwebsite.com", html_options: { target: :blank } + # end + # end + + # == Download Links + # + # You can disable download links on resource listing pages, + # or customize the formats shown per namespace/globally + # + # To disable/customize for the :admin namespace: + # + # config.namespace :admin do |admin| + # + # # Disable the links entirely + # admin.download_links = false + # + # # Only show XML & PDF options + # admin.download_links = [:xml, :pdf] + # + # # Enable/disable the links based on block + # # (for example, with cancan) + # admin.download_links = proc { can?(:view_download_links) } + # + # end + + # == Pagination + # + # Pagination is enabled by default for all resources. + # You can control the default per page count for all resources here. + # + # config.default_per_page = 30 + # + # You can control the max per page count too. + # + # config.max_per_page = 10_000 + + # == Filters + # + # By default the index screen includes a "Filters" sidebar on the right + # hand side with a filter for each attribute of the registered model. + # You can enable or disable them for all resources here. + # + # config.filters = true + # + # By default the filters include associations in a select, which means + # that every record will be loaded for each association (up + # to the value of config.maximum_association_filter_arity). + # You can enabled or disable the inclusion + # of those filters by default here. + # + # config.include_default_association_filters = true + + # config.maximum_association_filter_arity = 256 # default value of :unlimited will change to 256 in a future version + # config.filter_columns_for_large_association, [ + # :display_name, + # :full_name, + # :name, + # :username, + # :login, + # :title, + # :email, + # ] + # config.filter_method_for_large_association, '_starts_with' + + # == Head + # + # You can add your own content to the site head like analytics. Make sure + # you only pass content you trust. + # + # config.head = ''.html_safe + + # == Footer + # + # By default, the footer shows the current Active Admin version. You can + # override the content of the footer here. + # + # config.footer = 'my custom footer text' + + # == Sorting + # + # By default ActiveAdmin::OrderClause is used for sorting logic + # You can inherit it with own class and inject it for all resources + # + # config.order_clause = MyOrderClause +end diff --git a/config/routes.rb b/config/routes.rb index a74f230..e1f40ae 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,18 +1,15 @@ # frozen_string_literal: true Rails.application.routes.draw do + devise_for :admin_users, ActiveAdmin::Devise.config + ActiveAdmin.routes(self) + namespace :admin do + resources :flowcharts do + resources :flowchart_nodes + end + end get 'flowchart_icon_helper/new' get 'flowchart_icon/new' - devise_for :users, - path: '', - path_names: { - sign_in: 'login', - sign_out: 'logout' - }, - controllers: { - sessions: 'sessions' - }, - defaults: { format: :json } get '/questions', to: 'questions#index' get '/flowchart/:id', to: 'flowchart#serialized_flowchart_by_id' diff --git a/db/migrate/20200313010735_drop_users.rb b/db/migrate/20200313010735_drop_users.rb new file mode 100644 index 0000000..b1666b6 --- /dev/null +++ b/db/migrate/20200313010735_drop_users.rb @@ -0,0 +1,7 @@ +# frozen_string_literal: true + +class DropUsers < ActiveRecord::Migration[6.0] + def change + drop_table :users + end +end diff --git a/db/migrate/20200313011036_devise_create_admin_users.rb b/db/migrate/20200313011036_devise_create_admin_users.rb new file mode 100644 index 0000000..711c188 --- /dev/null +++ b/db/migrate/20200313011036_devise_create_admin_users.rb @@ -0,0 +1,43 @@ +# frozen_string_literal: true + +class DeviseCreateAdminUsers < ActiveRecord::Migration[6.0] + def change + create_table :admin_users do |t| + ## Database authenticatable + t.string :email, null: false, default: '' + t.string :encrypted_password, null: false, default: '' + + ## Recoverable + t.string :reset_password_token + t.datetime :reset_password_sent_at + + ## Rememberable + t.datetime :remember_created_at + + ## Trackable + # t.integer :sign_in_count, default: 0, null: false + # t.datetime :current_sign_in_at + # t.datetime :last_sign_in_at + # t.inet :current_sign_in_ip + # t.inet :last_sign_in_ip + + ## Confirmable + # t.string :confirmation_token + # t.datetime :confirmed_at + # t.datetime :confirmation_sent_at + # t.string :unconfirmed_email # Only if using reconfirmable + + ## Lockable + # t.integer :failed_attempts, default: 0, null: false # Only if lock strategy is :failed_attempts + # t.string :unlock_token # Only if unlock strategy is :email or :both + # t.datetime :locked_at + + t.timestamps null: false + end + + add_index :admin_users, :email, unique: true + add_index :admin_users, :reset_password_token, unique: true + # add_index :admin_users, :confirmation_token, unique: true + # add_index :admin_users, :unlock_token, unique: true + end +end diff --git a/db/migrate/20200313011046_create_active_admin_comments.rb b/db/migrate/20200313011046_create_active_admin_comments.rb new file mode 100644 index 0000000..f854cad --- /dev/null +++ b/db/migrate/20200313011046_create_active_admin_comments.rb @@ -0,0 +1,18 @@ +# frozen_string_literal: true + +class CreateActiveAdminComments < ActiveRecord::Migration[6.0] + def self.up + create_table :active_admin_comments do |t| + t.string :namespace + t.text :body + t.references :resource, polymorphic: true + t.references :author, polymorphic: true + t.timestamps + end + add_index :active_admin_comments, [:namespace] + end + + def self.down + drop_table :active_admin_comments + end +end diff --git a/db/schema.rb b/db/schema.rb index 31443b3..02cb309 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -15,6 +15,32 @@ # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" + create_table "active_admin_comments", force: :cascade do |t| + t.string "namespace" + t.text "body" + t.string "resource_type" + t.bigint "resource_id" + t.string "author_type" + t.bigint "author_id" + t.datetime "created_at", precision: 6, null: false + t.datetime "updated_at", precision: 6, null: false + t.index ["author_type", "author_id"], name: "index_active_admin_comments_on_author_type_and_author_id" + t.index ["namespace"], name: "index_active_admin_comments_on_namespace" + t.index ["resource_type", "resource_id"], name: "index_active_admin_comments_on_resource_type_and_resource_id" + end + + create_table "admin_users", force: :cascade do |t| + t.string "email", default: "", null: false + t.string "encrypted_password", default: "", null: false + t.string "reset_password_token" + t.datetime "reset_password_sent_at" + t.datetime "remember_created_at" + t.datetime "created_at", precision: 6, null: false + t.datetime "updated_at", precision: 6, null: false + t.index ["email"], name: "index_admin_users_on_email", unique: true + t.index ["reset_password_token"], name: "index_admin_users_on_reset_password_token", unique: true + end + create_table "flowchart_icon_helpers", force: :cascade do |t| t.bigint "flowchart_node_id" t.bigint "flowchart_icon_id" @@ -67,16 +93,6 @@ t.datetime "updated_at", precision: 6, null: false end - create_table "users", force: :cascade do |t| - t.string "email", default: "", null: false - t.string "encrypted_password", default: "", null: false - t.datetime "created_at", precision: 6, null: false - t.datetime "updated_at", precision: 6, null: false - t.string "jti", null: false - t.index ["email"], name: "index_users_on_email", unique: true - t.index ["jti"], name: "index_users_on_jti", unique: true - end - add_foreign_key "flowchart_nodes", "flowchart_nodes" add_foreign_key "flowchart_nodes", "flowchart_nodes", column: "child_id" add_foreign_key "flowchart_nodes", "flowchart_nodes", column: "sibling_id" diff --git a/db/seeds.rb b/db/seeds.rb index eded522..680cea2 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -3,11 +3,11 @@ case Rails.env when 'development' # create seed data - User.create!( - email: ENV['SEED_USER_EMAIL'], - password: ENV['SEED_USER_PASSWORD'], - jti: SecureRandom.uuid - ) + # User.create!( + # email: ENV['SEED_USER_EMAIL'], + # password: ENV['SEED_USER_PASSWORD'], + # jti: SecureRandom.uuid + # ) flowchart = Flowchart.create!( title: 'Flow Chart!!!!', description: 'this is an awesome flowchart', @@ -101,3 +101,4 @@ node5.save! node6.save! end +AdminUser.create!(email: 'admin@example.com', password: 'password', password_confirmation: 'password') if Rails.env.development? diff --git a/spec/factories/admin_users.rb b/spec/factories/admin_users.rb new file mode 100644 index 0000000..52fb105 --- /dev/null +++ b/spec/factories/admin_users.rb @@ -0,0 +1,25 @@ +# frozen_string_literal: true + +# == Schema Information +# +# Table name: admin_users +# +# id :bigint not null, primary key +# email :string default(""), not null +# encrypted_password :string default(""), not null +# remember_created_at :datetime +# reset_password_sent_at :datetime +# reset_password_token :string +# created_at :datetime not null +# updated_at :datetime not null +# +# Indexes +# +# index_admin_users_on_email (email) UNIQUE +# index_admin_users_on_reset_password_token (reset_password_token) UNIQUE +# + +FactoryBot.define do + factory :admin_user do + end +end diff --git a/spec/models/admin_user_spec.rb b/spec/models/admin_user_spec.rb new file mode 100644 index 0000000..7f2dd04 --- /dev/null +++ b/spec/models/admin_user_spec.rb @@ -0,0 +1,26 @@ +# frozen_string_literal: true + +# == Schema Information +# +# Table name: admin_users +# +# id :bigint not null, primary key +# email :string default(""), not null +# encrypted_password :string default(""), not null +# remember_created_at :datetime +# reset_password_sent_at :datetime +# reset_password_token :string +# created_at :datetime not null +# updated_at :datetime not null +# +# Indexes +# +# index_admin_users_on_email (email) UNIQUE +# index_admin_users_on_reset_password_token (reset_password_token) UNIQUE +# + +require 'rails_helper' + +RSpec.describe AdminUser, type: :model do + pending "add some examples to (or delete) #{__FILE__}" +end diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb deleted file mode 100644 index dc5fd37..0000000 --- a/spec/models/user_spec.rb +++ /dev/null @@ -1,42 +0,0 @@ -# frozen_string_literal: true - -# == Schema Information -# -# Table name: users -# -# id :bigint not null, primary key -# email :string default(""), not null -# encrypted_password :string default(""), not null -# jti :string not null -# created_at :datetime not null -# updated_at :datetime not null -# -# Indexes -# -# index_users_on_email (email) UNIQUE -# index_users_on_jti (jti) UNIQUE -# - -require 'rails_helper' - -RSpec.describe User, type: :model do - it 'is valid with valid attributes' do - expect(User.new(email: 'test@test.com', password: 'password')).to be_valid - end - it 'is not valid without email' do - user = User.new(email: nil) - expect(user).to_not be_valid - end - it 'is not valid without encrypted password' do - user = User.new(password: nil) - expect(user).to_not be_valid - end - it 'is not valid with invalid email' do - user = User.new(email: 'not_an_email', password: 'testing') - expect(user).to_not be_valid - end - it 'is not valid with password shorter than 6 chars' do - user = User.new(email: 'test@test.com', password: 'p') - expect(user).to_not be_valid - end -end