Skip to content

Commit

Permalink
Add Avo as an admin (#1)
Browse files Browse the repository at this point in the history
* add the gem

* add user and session resources

* add authenticated routes

* add admin test and update email domain

* update dropdown style
  • Loading branch information
adrienpoly authored Jan 17, 2024
1 parent f6f48da commit 8f71f83
Show file tree
Hide file tree
Showing 22 changed files with 330 additions and 12 deletions.
4 changes: 4 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -93,3 +93,7 @@ gem "view_component", "~> 3.7"
gem "dry-initializer-rails"

gem "dry-types", "~> 1.7"

# Avo admin
gem "avo", ">= 3.2"
gem "ransack", "~> 4.1"
38 changes: 38 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ GEM
erubi (~> 1.11)
rails-dom-testing (~> 2.2)
rails-html-sanitizer (~> 1.6)
active_link_to (1.0.5)
actionpack
addressable
activejob (7.1.2)
activesupport (= 7.1.2)
globalid (>= 0.3.6)
Expand Down Expand Up @@ -94,6 +97,22 @@ GEM
rake (>= 10.4, < 14.0)
ast (2.4.2)
authentication-zero (3.0.2)
avo (3.3.0)
actionview (>= 6.1)
active_link_to
activerecord (>= 6.1)
activesupport (>= 6.1)
addressable
docile
dry-initializer
httparty
inline_svg
meta-tags
pagy
turbo-rails
turbo_power (>= 0.6.0)
view_component (>= 3.7.0)
zeitwerk (>= 2.6.12)
base64 (0.2.0)
bcrypt (3.1.20)
bcrypt_pbkdf (1.1.0)
Expand Down Expand Up @@ -132,6 +151,7 @@ GEM
irb (~> 1.10)
reline (>= 0.3.8)
debug_inspector (1.2.0)
docile (1.4.0)
dotenv (2.8.1)
drb (2.2.0)
ruby2_keywords
Expand Down Expand Up @@ -174,6 +194,9 @@ GEM
actioncable (>= 6.0.0)
listen (>= 3.0.0)
railties (>= 6.0.0)
httparty (0.21.0)
mini_mime (>= 1.0.0)
multi_xml (>= 0.5.2)
i18n (1.14.1)
concurrent-ruby (~> 1.0)
i18n-tasks (1.0.13)
Expand All @@ -190,6 +213,9 @@ GEM
i18n_generators (2.2.2)
activerecord (>= 3.0.0)
railties (>= 3.0.0)
inline_svg (1.9.0)
activesupport (>= 3.0)
nokogiri (>= 1.6)
io-console (0.7.1)
irb (1.11.1)
rdoc
Expand Down Expand Up @@ -230,10 +256,13 @@ GEM
net-smtp
marcel (1.0.2)
matrix (0.4.2)
meta-tags (2.20.0)
actionpack (>= 6.0.0, < 7.2)
method_source (1.0.0)
mini_mime (1.1.5)
minitest (5.20.0)
msgpack (1.7.2)
multi_xml (0.6.0)
mutex_m (0.2.0)
net-imap (0.4.9.1)
date
Expand Down Expand Up @@ -262,6 +291,7 @@ GEM
racc (~> 1.4)
nokogiri (1.16.0-x86_64-linux)
racc (~> 1.4)
pagy (6.4.1)
parallel (1.24.0)
parser (3.3.0.2)
ast (~> 2.4.1)
Expand Down Expand Up @@ -326,6 +356,10 @@ GEM
zeitwerk (~> 2.6)
rainbow (3.1.1)
rake (13.1.0)
ransack (4.1.1)
activerecord (>= 6.1.5)
activesupport (>= 6.1.5)
i18n
rb-fsevent (0.11.2)
rb-inotify (0.10.1)
ffi (~> 1.0)
Expand Down Expand Up @@ -410,6 +444,8 @@ GEM
actionpack (>= 6.0.0)
activejob (>= 6.0.0)
railties (>= 6.0.0)
turbo_power (0.6.1)
turbo-rails (>= 1.3.0)
tzinfo (2.0.6)
concurrent-ruby (~> 1.0)
unicode-display_width (2.5.0)
Expand Down Expand Up @@ -442,6 +478,7 @@ PLATFORMS
DEPENDENCIES
annotate
authentication-zero (~> 3.0)
avo (>= 3.2)
bcrypt (~> 3.1.7)
bootsnap
byebug
Expand All @@ -463,6 +500,7 @@ DEPENDENCIES
propshaft
puma (>= 5.0)
rails (~> 7.1.1)
ransack (~> 4.1)
redis (>= 4.0.1)
ruby-lsp
ruby-lsp-rails
Expand Down
1 change: 1 addition & 0 deletions app/assets/stylesheets/application.tailwind.css
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
@import "tailwindcss/components";
@import "tailwindcss/utilities";

@import "reset";
@import "components/typography";
22 changes: 22 additions & 0 deletions app/assets/stylesheets/reset.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/* from https://x.com/souporserious/status/1746311121949356469?s=20 */

* {
box-sizing: border-box;
position: relative;
min-width: 0;
}

body {
min-height: 100dvh;
}

h1,
h2,
h3,
h4 {
text-wrap: balance;
}

p {
text-wrap: pretty;
}
14 changes: 14 additions & 0 deletions app/avo/resources/session.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class Avo::Resources::Session < Avo::BaseResource
self.includes = []
# self.search = {
# query: -> { query.ransack(id_eq: params[:q], m: "or").result(distinct: false) }
# }

def fields
field :id, as: :id
field :user, as: :belongs_to
field :user_id, as: :number
field :user_agent, as: :text, hide_on: [:index]
field :ip_address, as: :text, hide_on: [:index]
end
end
17 changes: 17 additions & 0 deletions app/avo/resources/user.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class Avo::Resources::User < Avo::BaseResource
self.title = :admin_title
self.includes = []
self.search = {
query: -> { query.ransack(email_cont: params[:q], id_eq: params[:q], m: "or").result(distinct: true) }
}

def fields
field :id, as: :id, link_to_record: true
field :email, as: :text, link_to_record: true
field :first_name, as: :text
field :last_name, as: :text
field :admin, as: :boolean, link_to_record: true
field :verified, as: :boolean, hide_on: [:index]
field :sessions, as: :has_many
end
end
2 changes: 1 addition & 1 deletion app/components/ui/dropdown_component.html.erb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<%= content_tag :details, class: classes, **attributes do %>
<summary class="m-1 btn btn-ghost">
<summary class="m-1 after:hidden">
<% if toggle_open? && toggle_close? %>
<div class="swap" data-dropdown-target="swap">
<div class="swap-on"><%= toggle_open %></div>
Expand Down
4 changes: 4 additions & 0 deletions app/controllers/avo/sessions_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# This controller has been generated to enable Rails' resource routes.
# More information on https://docs.avohq.io/2.0/controllers.html
class Avo::SessionsController < Avo::ResourcesController
end
4 changes: 4 additions & 0 deletions app/controllers/avo/users_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# This controller has been generated to enable Rails' resource routes.
# More information on https://docs.avohq.io/2.0/controllers.html
class Avo::UsersController < Avo::ResourcesController
end
4 changes: 2 additions & 2 deletions app/views/shared/_authenticated.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
<ul class="menu menu-horizontal px-1">
<li>
<%= ui_dropdown align: :end, content_classes: "mt-3", id: "navbar_toggle" do |c| %>
<% c.with_menu_item_link_to "Mes listes", "#" %>
<% c.with_menu_item_link_to t("admin"), avo_path, display: Current.user&.admin? %>
<% c.with_menu_item_divider %>
<% c.with_menu_item_button_to "Se déconnecter", Current.session, method: :delete, id: :sign_out %>
<% c.with_menu_item_button_to t("sign_out"), Current.session, method: :delete, id: :sign_out %>
<% c.with_toggle_open do %>
<%= heroicon "x-mark", options: {class: "w-6 h-6"} %>
<% end %>
Expand Down
137 changes: 137 additions & 0 deletions config/initializers/avo.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
# For more information regarding these settings check out our docs https://docs.avohq.io
# The values disaplayed here are the default ones. Uncomment and change them to fit your needs.
Avo.configure do |config|
## == Routing ==
config.root_path = "/admin"
# used only when you have custom `map` configuration in your config.ru
# config.prefix_path = "/internal"

# Sometimes you might want to mount Avo's engines yourself.
# https://docs.avohq.io/3.0/routing.html
# config.mount_avo_engines = true

# Where should the user be redirected when visiting the `/avo` url
config.home_path = "/admin/resources/users"

## == Licensing ==
# config.license_key = ENV['AVO_LICENSE_KEY']

## == Set the context ==
config.set_context do
# Return a context object that gets evaluated in Avo::ApplicationController
end

## == Authentication ==
config.current_user_method do
Current.user
end
# config.authenticate_with do
# end

## == Authorization ==
# config.authorization_methods = {
# index: 'index?',
# show: 'show?',
# edit: 'edit?',
# new: 'new?',
# update: 'update?',
# create: 'create?',
# destroy: 'destroy?',
# search: 'search?',
# }
# config.raise_error_on_missing_policy = false
# config.authorization_client = :pundit

## == Localization ==
# config.locale = 'en-US'

## == Resource options ==
# config.resource_controls_placement = :right
# config.model_resource_mapping = {}
# config.default_view_type = :table
# config.per_page = 24
# config.per_page_steps = [12, 24, 48, 72]
# config.via_per_page = 8
# config.id_links_to_resource = false

## == Cache options ==
## Provide a lambda to customize the cache store used by Avo.
## We compute the cache store by default, this is NOT the default, just an example.
# config.cache_store = -> {
# ActiveSupport::Cache.lookup_store(:solid_cache_store)
# }
# config.cache_resources_on_index_view = true
## permanent enable or disable cache_resource_filters, default value is false
# config.cache_resource_filters = false
## provide a lambda to enable or disable cache_resource_filters per user/resource.
# config.cache_resource_filters = -> { current_user.cache_resource_filters? }

## == Logger ==
# config.logger = -> {
# file_logger = ActiveSupport::Logger.new(Rails.root.join("log", "avo.log"))
#
# file_logger.datetime_format = "%Y-%m-%d %H:%M:%S"
# file_logger.formatter = proc do |severity, time, progname, msg|
# "[Avo] #{time}: #{msg}\n".tap do |i|
# puts i
# end
# end
#
# file_logger
# }

## == Customization ==
# config.app_name = 'Avocadelicious'
# config.timezone = 'UTC'
# config.currency = 'USD'
# config.hide_layout_when_printing = false
# config.full_width_container = false
# config.full_width_index_view = false
# config.search_debounce = 300
# config.view_component_path = "app/components"
# config.display_license_request_timeout_error = true
# config.disabled_features = []
# config.buttons_on_form_footers = true
# config.field_wrapper_layout = true
# config.resource_parent_controller = "Avo::ResourcesController"

## == Branding ==
# config.branding = {
# colors: {
# background: "248 246 242",
# 100 => "#CEE7F8",
# 400 => "#399EE5",
# 500 => "#0886DE",
# 600 => "#066BB2",
# },
# chart_colors: ["#0B8AE2", "#34C683", "#2AB1EE", "#34C6A8"],
# logo: "/avo-assets/logo.png",
# logomark: "/avo-assets/logomark.png",
# placeholder: "/avo-assets/placeholder.svg",
# favicon: "/avo-assets/favicon.ico"
# }

## == Breadcrumbs ==
# config.display_breadcrumbs = true
# config.set_initial_breadcrumbs do
# add_breadcrumb "Home", '/avo'
# end

## == Menus ==
# config.main_menu = -> {
# section "Dashboards", icon: "dashboards" do
# all_dashboards
# end

# section "Resources", icon: "resources" do
# all_resources
# end

# section "Tools", icon: "tools" do
# all_tools
# end
# }
# config.profile_menu = -> {
# link "Profile", path: "/avo/profile", icon: "user-circle"
# }
end
2 changes: 2 additions & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,8 @@ en:
sign_up: Sign up
sign_in: Sign in
login: Sign in
admin: Admin
sign_out: Sign out
support:
array:
last_word_connector: ", and "
Expand Down
2 changes: 2 additions & 0 deletions config/locales/fr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,8 @@ fr:
login: Se connecter
sign_in: Se connecter
logout: Se déconnecter
admin: Admin
sign_out: Se déconnecter
number:
currency:
format:
Expand Down
6 changes: 6 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
Rails.application.routes.draw do
extend Authenticator

# authentification
get "sign_in", to: "sessions#new"
post "sign_in", to: "sessions#create"
Expand All @@ -12,6 +14,10 @@
resource :password_reset, only: [:new, :edit, :create, :update]
end

authenticate :admin do
mount Avo::Engine, at: Avo.configuration.root_path
end

root "home#index"
# Define your application routes per the DSL in https://guides.rubyonrails.org/routing.html

Expand Down
Loading

0 comments on commit 8f71f83

Please sign in to comment.