diff --git a/.env.development.template b/.env.development.template index 843d0d9..bf311e0 100644 --- a/.env.development.template +++ b/.env.development.template @@ -2,9 +2,11 @@ POSTGRES_USER=postgres POSTGRES_PASSWORD= -# Default User +# Users DEFAULT_USER_EMAIL=user@example.com DEFAULT_USER_PASSWORD=Password123! +DEFAULT_ADMIN_EMAIL=admin@example.com +DEFAULT_ADMIN_PASSWORD=AdminPassword123! # Redis REDIS_URL=redis://localhost:6379/0 diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 50d2862..29842f3 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -1,7 +1,13 @@ # This configuration was generated by # `rubocop --auto-gen-config` -# on 2024-10-21 12:41:10 UTC using RuboCop version 1.67.0. +# on 2024-10-22 20:34:39 UTC using RuboCop version 1.67.0. # The point is for the user to remove these configuration records # one by one as the offenses are removed from the code base. # Note that changes in the inspected code, or installation of new # versions of RuboCop, may require this file to be generated again. + +# Offense count: 1 +# Configuration parameters: CountComments, CountAsOne, AllowedMethods, AllowedPatterns, inherit_mode. +# AllowedMethods: refine +Metrics/BlockLength: + Max: 43 diff --git a/Gemfile b/Gemfile index f1a717f..ddb936e 100644 --- a/Gemfile +++ b/Gemfile @@ -23,6 +23,10 @@ gem "tzinfo-data", platforms: %i[windows jruby] # Authentication gem "devise" +# ActiveAdmin +gem "activeadmin" +gem "activeadmin_addons" + # Performance and Error Tracking gem "rollbar" gem "scout_apm" diff --git a/Gemfile.lock b/Gemfile.lock index 1eaba38..e783350 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -45,6 +45,25 @@ GEM erubi (~> 1.11) rails-dom-testing (~> 2.2) rails-html-sanitizer (~> 1.6) + active_material (2.1.2) + activeadmin (3.2.5) + arbre (~> 1.2, >= 1.2.1) + csv + formtastic (>= 3.1) + formtastic_i18n (>= 0.4) + inherited_resources (~> 1.7) + jquery-rails (>= 4.2) + kaminari (>= 1.2.1) + railties (>= 6.1) + ransack (>= 4.0) + activeadmin_addons (1.10.1) + active_material + railties + redcarpet + require_all + sassc + sassc-rails + xdan-datetimepicker-rails (~> 2.5.1) activejob (7.2.1.1) activesupport (= 7.2.1.1) globalid (>= 0.3.6) @@ -77,6 +96,9 @@ GEM tzinfo (~> 2.0, >= 2.0.5) addressable (2.8.7) public_suffix (>= 2.0.2, < 7.0) + arbre (1.7.0) + activesupport (>= 3.0.0) + ruby2_keywords (>= 0.0.2) ast (2.4.2) autoprefixer-rails (10.4.19.0) execjs (~> 2) @@ -111,6 +133,7 @@ GEM concurrent-ruby (1.3.4) connection_pool (2.4.1) crass (1.0.6) + csv (3.3.0) date (3.3.4) debug (1.9.2) irb (~> 1.10) @@ -152,14 +175,25 @@ GEM ffi (1.17.0-x86_64-linux-gnu) font-awesome-sass (6.5.2) sassc (~> 2.0) + formtastic (5.0.0) + actionpack (>= 6.0.0) + formtastic_i18n (0.7.0) globalid (1.2.1) activesupport (>= 6.1) + has_scope (0.8.2) + actionpack (>= 5.2) + activesupport (>= 5.2) i18n (1.14.6) concurrent-ruby (~> 1.0) importmap-rails (2.0.3) actionpack (>= 6.0.0) activesupport (>= 6.0.0) railties (>= 6.0.0) + inherited_resources (1.14.0) + actionpack (>= 6.0) + has_scope (>= 0.6) + railties (>= 6.0) + responders (>= 2) io-console (0.7.2) irb (1.14.1) rdoc (>= 4.0.0) @@ -167,7 +201,23 @@ GEM jbuilder (2.13.0) actionview (>= 5.0.0) activesupport (>= 5.0.0) + jquery-rails (4.6.0) + rails-dom-testing (>= 1, < 3) + railties (>= 4.2.0) + thor (>= 0.14, < 2.0) json (2.7.2) + kaminari (1.2.2) + activesupport (>= 4.1.0) + kaminari-actionview (= 1.2.2) + kaminari-activerecord (= 1.2.2) + kaminari-core (= 1.2.2) + kaminari-actionview (1.2.2) + actionview + kaminari-core (= 1.2.2) + kaminari-activerecord (1.2.2) + activerecord + kaminari-core (= 1.2.2) + kaminari-core (1.2.2) language_server-protocol (3.17.0.3) logger (1.6.1) loofah (2.22.0) @@ -259,8 +309,13 @@ GEM zeitwerk (~> 2.6) rainbow (3.1.1) rake (13.2.1) + ransack (4.2.1) + activerecord (>= 6.1.5) + activesupport (>= 6.1.5) + i18n rdoc (6.7.0) psych (>= 4.0.0) + redcarpet (3.6.0) redis (5.3.0) redis-client (>= 0.22.0) redis-client (0.22.2) @@ -270,6 +325,7 @@ GEM io-console (~> 0.5) request_store (1.7.0) rack (>= 1.4) + require_all (3.0.0) responders (3.1.1) actionpack (>= 5.2) railties (>= 5.2) @@ -311,9 +367,9 @@ GEM parser (>= 3.3.1.0) rubocop-factory_bot (2.26.1) rubocop (~> 1.61) - rubocop-faker (1.1.0) + rubocop-faker (1.2.0) faker (>= 2.12.0) - rubocop (>= 0.82.0) + rubocop (>= 1.13.0) rubocop-performance (1.22.1) rubocop (>= 1.48.1, < 2.0) rubocop-ast (>= 1.31.1, < 2.0) @@ -401,6 +457,9 @@ GEM websocket-driver (0.7.6) websocket-extensions (>= 0.1.0) websocket-extensions (0.1.5) + xdan-datetimepicker-rails (2.5.4) + jquery-rails + rails (>= 3.2.16) xpath (3.2.0) nokogiri (~> 1.8) zeitwerk (2.7.1) @@ -414,6 +473,8 @@ PLATFORMS x86_64-linux DEPENDENCIES + activeadmin + activeadmin_addons bootsnap bootstrap (~> 5.3.3) brakeman diff --git a/app/admin/users.rb b/app/admin/users.rb new file mode 100644 index 0000000..d62519b --- /dev/null +++ b/app/admin/users.rb @@ -0,0 +1,51 @@ +# frozen_string_literal: true + +ActiveAdmin.register User do + permit_params :first_name, :last_name, :email, :role, :password, :password_confirmation + + filter :first_name_cont, as: :string + filter :last_name_cont, as: :string + filter :email + filter :created_at + + index do + selectable_column + id_column + column :first_name + column :last_name + column :email + column :role + column :created_at + column :updated_at + actions + end + + show do + attributes_table do + row :id + row :first_name + row :last_name + row :email + row :role + row :created_at + row :updated_at + end + end + + form do |f| + f.inputs "User Details" do + f.input :first_name + f.input :last_name + f.input :email + f.input :role, as: :select, collection: User.roles.keys + if f.object.new_record? + f.input :password + f.input :password_confirmation + else + f.input :password, hint: I18n.t("active_admin.hints.password") + f.input :password_confirmation, hint: I18n.t("active_admin.hints.password") + end + end + f.actions + end +end diff --git a/app/assets/config/manifest.js b/app/assets/config/manifest.js index ddd546a..00fdc3b 100644 --- a/app/assets/config/manifest.js +++ b/app/assets/config/manifest.js @@ -2,3 +2,4 @@ //= link_directory ../stylesheets .css //= link_tree ../../javascript .js //= link_tree ../../../vendor/javascript .js +//= link active_material/actions-toggle.js diff --git a/app/assets/javascripts/active_admin.js b/app/assets/javascripts/active_admin.js new file mode 100644 index 0000000..b656ec2 --- /dev/null +++ b/app/assets/javascripts/active_admin.js @@ -0,0 +1,2 @@ +//= require active_admin/base +//= require activeadmin_addons/all diff --git a/app/assets/stylesheets/active_admin.scss b/app/assets/stylesheets/active_admin.scss new file mode 100644 index 0000000..8ef502e --- /dev/null +++ b/app/assets/stylesheets/active_admin.scss @@ -0,0 +1,18 @@ +@import 'activeadmin_addons/all'; +// 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/javascript/application.js b/app/javascript/application.js index 51077c9..28272b4 100644 --- a/app/javascript/application.js +++ b/app/javascript/application.js @@ -3,6 +3,7 @@ import "@hotwired/turbo-rails" import "controllers" import "jquery" +import "@popperjs/core"; import "bootstrap" import "jquery-ui" diff --git a/app/models/user.rb b/app/models/user.rb index e892452..89bfa9b 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -1,10 +1,18 @@ # frozen_string_literal: true class User < ApplicationRecord - # Include default devise modules. Others available are: - # :confirmable, :lockable, :timeoutable, :trackable, and :omniauthable devise :database_authenticatable, :registerable, :recoverable, :rememberable, :validatable + enum :role, { user: 0, admin: 1 } + validates :first_name, :last_name, :email, presence: true + + def self.ransackable_attributes(_auth_object = nil) + %w[first_name last_name email role] + end + + def password_required? + new_record? || password.present? || password_confirmation.present? + end end diff --git a/app/views/layouts/partials/_header.html.erb b/app/views/layouts/partials/_header.html.erb index f483a36..f053d04 100644 --- a/app/views/layouts/partials/_header.html.erb +++ b/app/views/layouts/partials/_header.html.erb @@ -8,8 +8,7 @@ data-bs-target="#navbarHeader" aria-controls="navbarHeader" aria-expanded="false" - aria-label="Toggle navigation" - > + aria-label="Toggle navigation"> @@ -22,12 +21,28 @@ <% if user_signed_in? %>
<% end %> - \ No newline at end of file + diff --git a/config/initializers/active_admin.rb b/config/initializers/active_admin.rb new file mode 100644 index 0000000..951c63f --- /dev/null +++ b/config/initializers/active_admin.rb @@ -0,0 +1,355 @@ +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 = "Default Template" + + # 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" + + # == Load Paths + # + # By default Active Admin files go inside app/admin/. + # You can change this directory. + # + # eg: + # config.load_paths = [File.join(Rails.root, 'app', 'ui')] + # + # Or, you can also load more directories. + # Useful when setting namespaces with users that are not your main AdminUser entity. + # + # eg: + # config.load_paths = [ + # File.join(Rails.root, 'app', 'admin'), + # File.join(Rails.root, 'app', 'cashier') + # ] + + # == 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_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_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_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 = :delete + + # == Root + # + # Set the action to call for the root path. You can set different + # roots for each namespace. + # + # Default: + config.root_to = 'users#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 + redirect_to root_path, alert: I18n.t('active_admin.flash.unauthorized') unless current_user&.admin? + end + + # == 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 + meta_tags_options = { viewport: 'width=device-width, initial-scale=1' } + + # Add additional meta tags to the head element of active admin pages. + # + # Add tags to all pages logged in users see: + config.meta_tags = meta_tags_options + + # 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 = meta_tags_options + + # == 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 = '_start' + + # == 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 = 'Default Template' + + # == 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 + + # == Webpacker + # + # By default, Active Admin uses Sprocket's asset pipeline. + # You can switch to using Webpacker here. + # + # config.use_webpacker = true +end diff --git a/config/initializers/activeadmin_addons.rb b/config/initializers/activeadmin_addons.rb new file mode 100644 index 0000000..6472259 --- /dev/null +++ b/config/initializers/activeadmin_addons.rb @@ -0,0 +1,12 @@ +ActiveadminAddons.setup do |config| + # Change to "default" if you want to use ActiveAdmin's default select control. + config.default_select = "select2" + + # Set default options for DateTimePickerInput. The options you can provide are the same as in + # xdan's datetimepicker library (https://github.com/xdan/datetimepicker/tree/2.5.4). Yo need to + # pass a ruby hash, avoid camelCase keys. For example: use min_date instead of minDate key. + # config.datetime_picker_default_options = {} + + # Set DateTimePickerInput input format. This if for backend (Ruby) + # config.datetime_picker_input_format = "%Y-%m-%d %H:%M" +end diff --git a/config/locales/active_admin.en.yml b/config/locales/active_admin.en.yml new file mode 100644 index 0000000..966cb22 --- /dev/null +++ b/config/locales/active_admin.en.yml @@ -0,0 +1,6 @@ +en: + active_admin: + hints: + password: "Leave blank if you don't want to change it" + flash: + unauthorized: "You are not authorized to access this page." diff --git a/config/routes.rb b/config/routes.rb index 33ec353..0a4f53c 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,11 +1,11 @@ require 'sidekiq/web' Rails.application.routes.draw do - # Devise routes for user authentication + ActiveAdmin.routes(self) devise_for :users - # Mount Sidekiq web UI at /sidekiq for background job monitoring - authenticate :user do + # Mount Sidekiq + authenticate :user, ->(user) { user.admin? } do mount Sidekiq::Web => "/sidekiq" end diff --git a/db/migrate/20240809101614_devise_create_users.rb b/db/migrate/20240809101614_devise_create_users.rb index 17091d9..bfb3030 100644 --- a/db/migrate/20240809101614_devise_create_users.rb +++ b/db/migrate/20240809101614_devise_create_users.rb @@ -3,8 +3,8 @@ class DeviseCreateUsers < ActiveRecord::Migration[7.1] def change create_table :users do |t| - t.string :first_name, null: false - t.string :last_name, null: false + t.string :first_name, null: false, default: "" + t.string :last_name, null: false, default: "" ## Database authenticatable t.string :email, null: false, default: "" diff --git a/db/migrate/20241022131238_add_role_to_users.rb b/db/migrate/20241022131238_add_role_to_users.rb new file mode 100644 index 0000000..66f5404 --- /dev/null +++ b/db/migrate/20241022131238_add_role_to_users.rb @@ -0,0 +1,7 @@ +# frozen_string_literal: true + +class AddRoleToUsers < ActiveRecord::Migration[7.2] + def change + add_column :users, :role, :integer, default: 0 + end +end diff --git a/db/schema.rb b/db/schema.rb index b7ed142..b693836 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[7.2].define(version: 2024_08_09_101614) do +ActiveRecord::Schema[7.2].define(version: 2024_10_22_131238) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -24,6 +24,7 @@ t.datetime "remember_created_at" t.datetime "created_at", null: false t.datetime "updated_at", null: false + t.integer "role", default: 0 t.index ["email"], name: "index_users_on_email", unique: true t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true end diff --git a/db/seeds.rb b/db/seeds.rb index bcea976..055ff1b 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -10,13 +10,18 @@ # MovieGenre.find_or_create_by!(name: genre_name) # end -unless User.exists? - FactoryBot.create( - :user, - first_name: "default", - last_name: "user", - email: ENV.fetch("DEFAULT_USER_EMAIL", nil), - password: ENV.fetch("DEFAULT_USER_PASSWORD", nil), - password_confirmation: ENV.fetch("DEFAULT_USER_PASSWORD", nil) - ) +User.find_or_create_by!(email: ENV.fetch("DEFAULT_USER_EMAIL", "user@example.com")) do |user| + user.first_name = "Normal" + user.last_name = "User" + user.password = ENV.fetch("DEFAULT_USER_PASSWORD", "UserPassword123!") + user.password_confirmation = ENV.fetch("DEFAULT_USER_PASSWORD", "UserPassword123!") + user.role = :user +end + +User.find_or_create_by!(email: ENV.fetch("DEFAULT_ADMIN_EMAIL", "admin@example.com")) do |user| + user.first_name = "Admin" + user.last_name = "User" + user.password = ENV.fetch("DEFAULT_ADMIN_PASSWORD", "AdminPassword123!") + user.password_confirmation = ENV.fetch("DEFAULT_ADMIN_PASSWORD", "AdminPassword123!") + user.role = :admin end diff --git a/spec/factories/users.rb b/spec/factories/users.rb index 433e269..4bfc441 100644 --- a/spec/factories/users.rb +++ b/spec/factories/users.rb @@ -5,7 +5,8 @@ first_name { Faker::Name.first_name } last_name { Faker::Name.last_name } email { Faker::Internet.email } - password { "password123" } - password_confirmation { "password123" } + password { "Password123!" } + password_confirmation { "Password123!" } + role { :user } end end diff --git a/spec/helpers/application_helper_spec.rb b/spec/helpers/application_helper_spec.rb new file mode 100644 index 0000000..2c99997 --- /dev/null +++ b/spec/helpers/application_helper_spec.rb @@ -0,0 +1,27 @@ +# frozen_string_literal: true + +require 'rails_helper' + +RSpec.describe ApplicationHelper, type: :helper do + describe '#bootstrap_class_for' do + it 'returns the correct Bootstrap class for success' do + expect(helper.bootstrap_class_for(:success)).to eq('success') + end + + it 'returns the correct Bootstrap class for error' do + expect(helper.bootstrap_class_for(:error)).to eq('danger') + end + + it 'returns the correct Bootstrap class for alert' do + expect(helper.bootstrap_class_for(:alert)).to eq('warning') + end + + it 'returns the correct Bootstrap class for notice' do + expect(helper.bootstrap_class_for(:notice)).to eq('info') + end + + it 'returns the flash type as a string if it is not mapped' do + expect(helper.bootstrap_class_for(:unknown)).to eq('unknown') + end + end +end diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index 1424881..6b86089 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -26,4 +26,47 @@ expect(user.devise_modules).to include(*devise_modules) end end + + describe "Enums" do + it "defines roles with correct values" do + expect(described_class.roles).to eq("user" => 0, "admin" => 1) + end + end + + describe "#ransackable_attributes" do + it "returns the correct ransackable attributes" do + expect(described_class.ransackable_attributes).to match_array(%w[first_name last_name email role]) + end + end + + describe "#password_required?" do + context "when the user is a new record" do + it "returns true" do + new_user = build(:user) + expect(new_user.password_required?).to be(true) + end + end + + context "when the password is present" do + it "returns true" do + user.password = "newpassword" + expect(user.password_required?).to be(true) + end + end + + context "when the password confirmation is present" do + it "returns true" do + user.password_confirmation = "newpassword" + expect(user.password_required?).to be(true) + end + end + + context "when password and confirmation are not present for an existing user" do + it "returns false" do + user.password = nil + user.password_confirmation = nil + expect(user.password_required?).to be(false) + end + end + end end diff --git a/spec/support/simplecov_config.rb b/spec/support/simplecov_config.rb index f7477a0..0354779 100644 --- a/spec/support/simplecov_config.rb +++ b/spec/support/simplecov_config.rb @@ -7,4 +7,5 @@ add_filter "/db/" add_filter "/spec/" add_filter "/config/" + add_filter "app/admin" end