Skip to content

Commit

Permalink
admin controller generator
Browse files Browse the repository at this point in the history
  • Loading branch information
michelson committed Jul 13, 2024
1 parent 1eea7db commit 6342286
Show file tree
Hide file tree
Showing 13 changed files with 184 additions and 157 deletions.
9 changes: 8 additions & 1 deletion app/controllers/backstage/terms_and_conditions_controller.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
# lib/generators/backstage/templates/controller.rb
module Backstage
class TermsAndConditionsController < Backstage::BaseController
# Add any resource-specific logic here
def model_class
TermsAndCondition
end

def permitted_params
params.require(:user).permit(:email, :username, :role, :editor)
end

end
end
105 changes: 0 additions & 105 deletions app/models/backstage_config.rb

This file was deleted.

2 changes: 1 addition & 1 deletion backstage/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,6 @@ module Backstage
end
```


## Authentication and Current User

Backstage uses your application's authentication system. By default, it expects a `current_user` method to be available. You can customize the current user method and configure admin authentication in your Backstage configuration:
Expand All @@ -128,6 +127,7 @@ Backstage::Config.configure do

# Resource configurations...
end
```

### Views

Expand Down
6 changes: 5 additions & 1 deletion backstage/app/controllers/backstage/dashboard_controller.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
class Backstage::DashboardController < Backstage::ApplicationController
class Backstage::DashboardController < Backstage::BaseController
layout "backstage/admin"

before_action :authenticate_admin


def index
readme_path = Backstage::Engine.root.join('README.md')
@readme_content = File.exist?(readme_path) ? File.read(readme_path) : "Welcome to the Admin Panel"
Expand Down
16 changes: 16 additions & 0 deletions backstage/app/controllers/backstage/dynamic_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
module Backstage
class DynamicController < BaseController
#class << self
# attr_accessor :resource_config
#end

def model_class
Post
end

def permitted_params
params.require(:user).permit(:email, :username, :role, :editor)
end

end
end
6 changes: 3 additions & 3 deletions backstage/app/views/backstage/shared/_actions.erb
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<% resource.actions.each do |action| %>
<% case action[:name] %>
<% when :view %>
<%= link_to 'View', polymorphic_path([item]), class: "text-blue-600 hover:text-blue-900 mr-2" %>
<%= link_to 'View', backstage.url_for(item), class: "text-blue-600 hover:text-blue-900 mr-2" %>
<% when :edit %>
<%= link_to 'Edit', edit_polymorphic_path([item]), class: "text-green-600 hover:text-green-900 mr-2" %>
<%= link_to 'Edit', "backstage.url_for(item, action: :edit)", class: "text-green-600 hover:text-green-900 mr-2" %>
<% when :delete %>
<%= link_to 'Delete', polymorphic_path([item]), data: {turbo_method: :delete, turbo_confirm: 'Are you sure?' }, class: "text-red-600 hover:text-red-900" %>
<%= link_to 'Delete', backstage.url_for(item), data: {turbo_method: :delete, turbo_confirm: 'Are you sure?' }, class: "text-red-600 hover:text-red-900" %>
<% end %>
<% end %>
5 changes: 4 additions & 1 deletion backstage/config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@

Backstage::Config.resources.each do |resource_name, resource_config|

resources resource_name, controller: resource_config.controller_name.gsub("backstage/", "") do
resources resource_name do # controller: resource_config.controller_name.gsub("backstage/", "") do
collection do
post 'add_filter'
end
resource_config.custom_actions.each do |action|
member do
get action[:name], action: :custom_action, custom_action: action[:name]
Expand Down
16 changes: 15 additions & 1 deletion backstage/lib/backstage.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,19 @@
require "backstage/form_builder"

module Backstage
# Your code goes here...
def self.setup_controllers
Config.resources.each do |resource_name, resource_config|
controller_class_name = "#{resource_name.to_s.classify.pluralize}Controller"

# Ensure DynamicController is loaded
require 'backstage/dynamic_controller'

controller_class = Class.new(Backstage::DynamicController) do
# Define class methods here if needed
end

# controller_class.resource_config = resource_config
Backstage.const_set(controller_class_name, controller_class)
end
end
end
2 changes: 1 addition & 1 deletion backstage/lib/backstage/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def initialize(name, options = {})
@filterable_fields = []
@custom_actions = []
@controller_name = options[:controller] || "backstage/#{name.to_s.pluralize}"
Backstage::Config.ensure_controller_exists(name)
# Backstage::Config.ensure_controller_exists(name)
end

def filterable_field(name, type, options = {})
Expand Down
33 changes: 0 additions & 33 deletions backstage/lib/backstage/controller_lookup_middleware.rb

This file was deleted.

20 changes: 20 additions & 0 deletions backstage/lib/backstage/engine.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,25 @@

module Backstage
class Engine < ::Rails::Engine
isolate_namespace Backstage

initializer "backstage.middleware" do |app|
# app.middleware.use Backstage::ControllerLookupMiddleware
end

config.to_prepare do
# Backstage.setup_controllers
end

initializer "backstage.load_config" do |app|
config.after_initialize do
# Load any additional configuration if needed
end
end

config.after_initialize do |app|
app.reload_routes!
end

end
end
101 changes: 101 additions & 0 deletions config/initializers/backstage_config.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
require "backstage/config"

Backstage::Config.configure do

self.current_user_method = :current_user

# Configure admin authentication
authenticate_admin do
# Your authentication logic here
# You have access to controller methods and the current user
# For example:
redirect_to "/" unless current_backstage_user && current_backstage_user.admin?
end


resource :users do
column :id
column :email
column :username
column :role
column :editor

scope :all
scope :admins, -> { where(role: 'admin') }
scope :artists
scope :recent, -> { where('created_at > ?', 1.week.ago) }

filter :email_cont, :string, label: 'Email contains'
filter :username_cont, :string, label: 'Username contains'
filter :role_cont, :select, collection: -> { User.roles.keys }

filterable_field :username, :string
filterable_field :email, :string
filterable_field :role, :select, collection: -> { [:admin, :artist] }

form_field :email, :email
form_field :username, :string
form_field :role, :select, collection: -> { User.roles.keys }, include_blank: false
form_field :seller, :boolean
form_field :editor, :check_box

action :view
action :edit
action :delete

custom_action :refund, label: 'Refund Invoice' do |invoice|
# Refund logic here
# invoice.refund!
redirect_to backstage.user_path(invoice), notice: 'Invoice refunded successfully'
end

end

resource :categories do
column :id
column :name

filter :name, :string, label: 'Name contains'

form_field :name_cont, :string

action :view
action :edit
action :delete
end

resource :posts do
column :id
column :title
column :author do |post, view|
view.link_to post.user.full_name, view.user_path(post.user)
end

scope :all
scope :published, -> { where(published: true) }
scope :draft, -> { where(published: false) }
scope :recent, -> { where('created_at > ?', 1.week.ago) }

filter :title, :string, label: 'Name contains'

form_field :title_cont, :string

action :view
action :edit
action :delete
end

resource :terms_and_conditions do

column :id
column :title

form_field :title, :string
form_field :category, :string
form_field :content, :custom, ->(view, form) { view.render("shared/simple_editor", form: form, field: :content) }

action :view
action :edit
action :delete
end
end
Loading

0 comments on commit 6342286

Please sign in to comment.