Skip to content

Commit

Permalink
Avo (#25)
Browse files Browse the repository at this point in the history
* Initial admin/avo setup

* Initial admin resources
  • Loading branch information
Shpigford authored May 14, 2024
1 parent 062782d commit 78a70bd
Show file tree
Hide file tree
Showing 25 changed files with 370 additions and 3 deletions.
3 changes: 1 addition & 2 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,4 @@ CONVERTKIT_API_KEY=
CONVERTKIT_API_SECRET=
CONVERTKIT_FORM_ID=
CONVERTKIT_TAG_ID=
ADMIN_USERNAME=
ADMIN_PASSWORD=
AUTH_SECRET=
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,4 @@ group :test do
gem "capybara"
gem "selenium-webdriver"
end
gem "avo", ">= 3.2"
42 changes: 42 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,28 @@ GIT
GEM
remote: https://rubygems.org/
specs:
active_link_to (1.0.5)
actionpack
addressable
addressable (2.8.6)
public_suffix (>= 2.0.2, < 6.0)
ast (2.4.2)
avo (3.7.4)
actionview (>= 6.1)
active_link_to
activerecord (>= 6.1)
activesupport (>= 6.1)
addressable
docile
dry-initializer
httparty
inline_svg
meta-tags
pagy (>= 7.0.0)
turbo-rails (>= 2.0.0)
turbo_power (>= 0.6.0)
view_component (>= 3.7.0)
zeitwerk (>= 2.6.12)
base64 (0.2.0)
better_html (2.1.1)
actionview (>= 6.0)
Expand Down Expand Up @@ -134,15 +153,18 @@ GEM
concurrent-ruby (1.2.3)
connection_pool (2.4.1)
crass (1.0.6)
csv (3.3.0)
date (3.3.4)
debug (1.9.2)
irb (~> 1.10)
reline (>= 0.3.8)
docile (1.4.0)
dotenv (3.1.0)
dotenv-rails (3.1.0)
dotenv (= 3.1.0)
railties (>= 6.1)
drb (2.2.1)
dry-initializer (3.1.1)
erb_lint (0.5.0)
activesupport
better_html (>= 2.0.1)
Expand All @@ -162,12 +184,19 @@ GEM
actioncable (>= 6.0.0)
listen (>= 3.0.0)
railties (>= 6.0.0)
httparty (0.22.0)
csv
mini_mime (>= 1.0.0)
multi_xml (>= 0.5.2)
i18n (1.14.4)
concurrent-ruby (~> 1.0)
importmap-rails (2.0.1)
actionpack (>= 6.0.0)
activesupport (>= 6.0.0)
railties (>= 6.0.0)
inline_svg (1.9.0)
activesupport (>= 3.0)
nokogiri (>= 1.6)
io-console (0.7.2)
irb (1.13.0)
rdoc (>= 4.0.0)
Expand All @@ -190,9 +219,14 @@ GEM
net-smtp
marcel (1.0.4)
matrix (0.4.2)
meta-tags (2.21.0)
actionpack (>= 6.0.0, < 7.2)
method_source (1.1.0)
mini_mime (1.1.5)
minitest (5.21.2)
msgpack (1.7.2)
multi_xml (0.7.1)
bigdecimal (~> 3.1)
net-http (0.4.1)
uri
net-imap (0.4.10)
Expand All @@ -217,6 +251,7 @@ GEM
racc (~> 1.4)
nokogiri (1.16.4-x86_64-linux)
racc (~> 1.4)
pagy (8.4.0)
parallel (1.24.0)
parser (3.3.0.5)
ast (~> 2.4.1)
Expand Down Expand Up @@ -331,11 +366,17 @@ 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)
uri (0.13.0)
useragent (0.16.10)
view_component (3.12.1)
activesupport (>= 5.2.0, < 8.0)
concurrent-ruby (~> 1.0)
method_source (~> 1.0)
web-console (4.2.1)
actionview (>= 6.0.0)
activemodel (>= 6.0.0)
Expand All @@ -359,6 +400,7 @@ PLATFORMS
x86_64-linux

DEPENDENCIES
avo (>= 3.2)
bootsnap
brakeman
capybara
Expand Down
27 changes: 27 additions & 0 deletions app/avo/resources/article.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
class Avo::Resources::Article < Avo::BaseResource
self.find_record_method = -> {
# When using friendly_id, we need to check if the id is a slug or an id.
# If it's a slug, we need to use the find_by_slug method.
# If it's an id, we need to use the find method.
# If the id is an array, we need to use the where method in order to return a collection.
if id.is_a?(Array)
id.first.to_i == 0 ? query.where(slug: id) : query.where(id: id)
else
id.to_i == 0 ? query.find_by_slug(id) : query.find(id)
end
}

self.includes = []
# self.search = {
# query: -> { query.ransack(id_eq: params[:q], m: "or").result(distinct: false) }
# }

def fields
field :id, as: :id
field :title, as: :text
field :slug, as: :text
field :content, as: :trix
field :publish_at, as: :date_time
field :author_name, as: :text
end
end
32 changes: 32 additions & 0 deletions app/avo/resources/term.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
class Avo::Resources::Term < Avo::BaseResource
self.find_record_method = -> {
# When using friendly_id, we need to check if the id is a slug or an id.
# If it's a slug, we need to use the find_by_slug method.
# If it's an id, we need to use the find method.
# If the id is an array, we need to use the where method in order to return a collection.
if id.is_a?(Array)
id.first.to_i == 0 ? query.where(slug: id) : query.where(id: id)
else
id.to_i == 0 ? query.find_by_slug(id) : query.find(id)
end
}

self.includes = []
# self.search = {
# query: -> { query.ransack(id_eq: params[:q], m: "or").result(distinct: false) }
# }

def fields
field :id, as: :id
field :name, as: :text
field :title, as: :text
field :content, as: :trix
field :slug, as: :text
field :video_id, as: :text
field :video_title, as: :text
field :video_description, as: :textarea
field :video_thumbnail_url, as: :text
field :video_upload_date, as: :date
field :video_duration, as: :text
end
end
11 changes: 11 additions & 0 deletions app/avo/resources/user.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class Avo::Resources::User < 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 :username, as: :text
end
end
26 changes: 26 additions & 0 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,30 @@
class ApplicationController < ActionController::Base
# Only allow modern browsers supporting webp images, web push, badges, import maps, CSS nesting, and CSS :has.
# allow_browser versions: :modern

helper_method :current_user
helper_method :user_signed_in?

private

def authenticate
@current_user = nil
authenticate_with_http_digest(ENV["AUTH_SECRET"]) do |username|
@current_user = User.find_by(username: username)
@current_user&.password_digest || false
end
!@current_user.nil?
end

def authenticate!
authenticate || request_http_digest_authentication(ENV["AUTH_SECRET"])
end

def current_user
Current.user ||= @current_user
end

def user_signed_in?
Current.user.present?
end
end
4 changes: 4 additions & 0 deletions app/controllers/avo/articles_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/3.0/controllers.html
class Avo::ArticlesController < Avo::ResourcesController
end
4 changes: 4 additions & 0 deletions app/controllers/avo/terms_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/3.0/controllers.html
class Avo::TermsController < 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/3.0/controllers.html
class Avo::UsersController < Avo::ResourcesController
end
6 changes: 6 additions & 0 deletions app/controllers/pages_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
require "json"

class PagesController < ApplicationController
before_action :authenticate!, only: :login

def index
@stars = Rails.cache.fetch("stargazers_count", expires_in: 24.hours) do
fetch_stars_count
Expand All @@ -19,6 +21,10 @@ def sitemap
@articles = Article.all.order(publish_at: :desc)
end

def login
redirect_to avo_path
end

private

def fetch_stars_count
Expand Down
3 changes: 3 additions & 0 deletions app/models/current.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class Current < ActiveSupport::CurrentAttributes
attribute :user
end
2 changes: 2 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
class User < ApplicationRecord
end
Empty file added app/views/pages/login.html.erb
Empty file.
Loading

0 comments on commit 78a70bd

Please sign in to comment.