Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: testing new installation #1

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,7 @@ gem "factory_bot_rails", "~> 6.2"
gem "faker", "~> 3.1", ">= 3.1.1"
gem "annotate", "~> 3.2"
gem "pretender", "~> 0.5.0"

gem "pundit"
gem "avo-audit_logging", path: "/Users/adrian/work/avocado/gems/avo-audit_logging"
gem "paper_trail", "~> 15.2"
21 changes: 21 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
PATH
remote: /Users/adrian/work/avocado/gems/avo-audit_logging
specs:
avo-audit_logging (3.2.0)
avo (>= 3.2.1)
avo-diff_field (>= 0.0.4)
zeitwerk (>= 2.6.12)

GEM
remote: https://packager.dev/avo-hq/
specs:
Expand Down Expand Up @@ -123,6 +131,8 @@ GEM
turbo_power (>= 0.6.0)
view_component (>= 3.7.0)
zeitwerk (>= 2.6.12)
avo-diff_field (0.0.4)
diffy
base64 (0.2.0)
bcrypt (3.1.20)
bigdecimal (3.1.8)
Expand Down Expand Up @@ -154,6 +164,7 @@ GEM
railties (>= 4.1.0)
responders
warden (~> 1.2.3)
diffy (3.4.3)
docile (1.4.1)
drb (2.2.1)
erubi (1.13.0)
Expand Down Expand Up @@ -225,6 +236,9 @@ GEM
racc (~> 1.4)
orm_adapter (0.5.0)
pagy (9.1.1)
paper_trail (15.2.0)
activerecord (>= 6.1)
request_store (~> 1.4)
parallel (1.26.3)
parser (3.3.5.0)
ast (~> 2.4.1)
Expand All @@ -239,6 +253,8 @@ GEM
public_suffix (6.0.1)
puma (6.4.3)
nio4r (~> 2.0)
pundit (2.4.0)
activesupport (>= 3.0.0)
racc (1.8.1)
rack (3.1.8)
rack-session (2.0.0)
Expand Down Expand Up @@ -288,6 +304,8 @@ GEM
regexp_parser (2.9.2)
reline (0.5.10)
io-console (~> 0.5)
request_store (1.7.0)
rack (>= 1.4)
responders (3.1.1)
actionpack (>= 5.2)
railties (>= 5.2)
Expand Down Expand Up @@ -409,6 +427,7 @@ DEPENDENCIES
annotate (~> 3.2)
avo (>= 3.2.1)
avo-advanced (>= 3.2.0)!
avo-audit_logging!
bootsnap
brakeman
capybara
Expand All @@ -418,9 +437,11 @@ DEPENDENCIES
faker (~> 3.1, >= 3.1.1)
importmap-rails
jbuilder
paper_trail (~> 15.2)
pretender (~> 0.5.0)
progressbar (~> 1.13)
puma (>= 5.0)
pundit
rails (~> 7.2.1, >= 7.2.1.1)
rubocop-rails-omakase
selenium-webdriver
Expand Down
7 changes: 7 additions & 0 deletions app/avo/actions/undo.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class Avo::Actions::Undo < Avo::BaseAction
self.name = "Undo change"

def handle(query:, **args)
query.first.reify.save!
end
end
4 changes: 4 additions & 0 deletions app/avo/resource_tools/timeline.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class Avo::ResourceTools::Timeline < Avo::BaseResourceTool
self.name = "Timeline"
# self.partial = "avo/resource_tools/city_editor"
end
59 changes: 59 additions & 0 deletions app/avo/resources/avo_activity.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
class Avo::Resources::AvoActivity < Avo::BaseResource
self.index_controls = -> {}
self.visible_on_sidebar = false
self.record_selector = false
self.model_class = "Avo::Activity"
self.authorization_policy = AvoActivityPolicy
self.row_controls = -> {
show_button
delete_button
}

def fields
field :id, as: :id
field :action, as: :text, sortable: true
field :preview, as: :preview
field :activity_class, as: :text, sortable: true, filterable: true
with_options hide_on: :index do
field :author_id, as: :number
field :author_type, as: :text
end
author_link_field
field :payload, as: :code, show_on: :preview, format_using: -> { JSON.pretty_generate(JSON.parse(value)) }
field :created_at, as: :date_time, sortable: true, filterable: true
field :avo_activity_pivots, as: :has_many
end

def filters
dynamic_filter :action,
type: :select,
options: Avo::AuditLogging::AUDIT_LOGGING_DEFAULTS[:actions].keys
end

def author_link_field
field :author, as: :text, html: true do
if record.author_type.present? && record.author_id.present?
resource.link_for_type_and_id type: record.author_type, id: record.author_id
end
end
end

def link_for_type_and_id(type:, id:)
# Instatiate the model class
model_class = type.constantize

# Search for and instantiate the resource
resource = Avo.resource_manager.get_resource_by_model_class(model_class).new

# Find the record
record_instance = resource.find_record id

# Hydrate the resource
resource.hydrate(record: record_instance)

# Generate the link
link_to resource.record_title, resource_path(resource: resource, record: record_instance)
rescue
nil
end
end
22 changes: 22 additions & 0 deletions app/avo/resources/avo_activity_pivot.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
class Avo::Resources::AvoActivityPivot < Avo::Resources::AvoActivity
self.model_class = "Avo::ActivityPivot"

def fields
field :record, as: :text, html: true do
if record.record_type.present? && record.record_id.present?
resource.link_for_type_and_id type: record.record_type, id: record.record_id
end
end
field :record_type, as: :text
field :record_id, as: :number
if defined?(PaperTrail) || defined?(Audited)
field :activity_pivot, as: :text, html: true do
if record.activity_pivot_type.present? && record.activity_pivot_id.present?
resource.link_for_type_and_id type: record.activity_pivot_type, id: record.activity_pivot_id
end
end
field :activity_pivot_type, as: :text
field :activity_pivot_id, as: :number
end
end
end
42 changes: 42 additions & 0 deletions app/avo/resources/paper_trail.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# frozen_string_literal: true

class Avo::Resources::PaperTrail < Avo::BaseResource
# TODO: translate name
# TODO: fix whodunnit on actions
self.visible_on_sidebar = false
self.model_class = ::PaperTrail::Version

self.index_controls = -> {}
self.row_controls = -> {
action Avo::Actions::Undo, title: "Undo this update", icon: "heroicons/outline/arrow-uturn-left", style: :icon
show_button
delete_button
}

def fields
field :id, as: :id
field :event, as: :text
field :preview, as: :preview
field :changes, as: :text do
record.changeset&.keys&.split(",")&.join(" | ")
end
field :whodunnit,
as: :text,
readonly: true do
if record.whodunnit.present?
user = User.find_by(id: record.whodunnit)
if user.present?
link_to user.name, Avo::Engine.routes.url_helpers.resources_user_path(id: user.id)
else
record.whodunnit
end
end
end
field :created_at, as: :date_time
field :changeset, as: :diff, show_on: :preview
end

def actions
action Avo::Actions::Undo
end
end
28 changes: 20 additions & 8 deletions app/avo/resources/product.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,25 @@
class Avo::Resources::Product < Avo::BaseResource
self.audit_logging = {
activity: true
}

def fields
field :id, as: :id, link_to_record: true
field :name, as: :text, link_to_record: true
field :manufacturer, as: :text
field :price, as: :number, step: 1
field :quantity, as: :number, step: 1
field :description, as: :trix
field :is_featured, as: :boolean
field :category, as: :select, enum: ::Product.categories
main_panel do
field :id, as: :id, link_to_record: true
field :name, as: :text, link_to_record: true
field :manufacturer, as: :text
field :price, as: :number, step: 1
field :quantity, as: :number, step: 1
field :description, as: :trix
field :is_featured, as: :boolean
field :category, as: :select, enum: ::Product.categories
sidebar do
tool Avo::ResourceTools::Timeline
end
end

field :avo_activities, as: :has_many
field :versions, as: :has_many, use_resource: Avo::Resources::PaperTrail
end

def actions
Expand Down
12 changes: 12 additions & 0 deletions app/avo/resources/user.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
class Avo::Resources::User < Avo::BaseResource
self.audit_logging = {
activity: true,
author: true
}

self.row_controls = -> {
action Avo::Actions::Impersonate, label: "Impersonate #{record.name}", style: :primary, color: :blue,
icon: "heroicons/outline/identification"
Expand All @@ -11,6 +16,7 @@ def fields
field :id, as: :id, link_to_record: true
field :email, as: :text, link_to_record: true
field :products, as: :has_many
field :avo_authored, as: :has_many
end

def filters
Expand All @@ -21,3 +27,9 @@ def actions
action Avo::Actions::Impersonate
end
end

# author = User.find_by(email: "[email protected]") # o aflam de la author: true

# current_user.class

# user.avo_authored
4 changes: 4 additions & 0 deletions app/controllers/avo/avo_activities_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::AvoActivitiesController < Avo::ResourcesController
end
4 changes: 4 additions & 0 deletions app/controllers/avo/avo_activity_pivots_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::AvoActivityPivotsController < Avo::ResourcesController
end
4 changes: 4 additions & 0 deletions app/controllers/avo/paper_trails_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::PaperTrailsController < Avo::ResourcesController
end
2 changes: 2 additions & 0 deletions app/models/product.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
# updated_at :datetime not null
#
class Product < ApplicationRecord
has_paper_trail

enum :category, {
"Music players": 0,
"Phones": 1,
Expand Down
53 changes: 53 additions & 0 deletions app/policies/application_policy.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# frozen_string_literal: true

class ApplicationPolicy
attr_reader :user, :record

def initialize(user, record)
@user = user
@record = record
end

def index?
false
end

def show?
false
end

def create?
false
end

def new?
create?
end

def update?
false
end

def edit?
update?
end

def destroy?
false
end

class Scope
def initialize(user, scope)
@user = user
@scope = scope
end

def resolve
raise NoMethodError, "You must define #resolve in #{self.class}"
end

private

attr_reader :user, :scope
end
end
Loading
Loading