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

Add madmin initial implementation #295

Merged
merged 2 commits into from
May 10, 2023
Merged
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
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -111,3 +111,5 @@ gem "tzinfo-data", platforms: [:mingw, :mswin, :x64_mingw, :jruby]
gem "coffee-script"

gem "pundit", "~> 2.2"

gem "madmin", "~> 1.2"
6 changes: 6 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,9 @@ GEM
loofah (2.19.1)
crass (~> 1.0.2)
nokogiri (>= 1.5.9)
madmin (1.2.7)
pagy (>= 3.5, < 6.0)
rails (>= 6.0.3)
mail (2.8.1)
mini_mime (>= 0.1.1)
net-imap
Expand Down Expand Up @@ -220,6 +223,8 @@ GEM
actionpack (>= 4.2)
omniauth (~> 2.0)
orm_adapter (0.5.0)
pagy (5.10.1)
activesupport
parallel (1.22.1)
parser (3.2.1.1)
ast (~> 2.4.1)
Expand Down Expand Up @@ -409,6 +414,7 @@ DEPENDENCIES
jquery-rails
jquery-ui-rails (~> 5.0, >= 5.0.5)
listen (~> 3.7)
madmin (~> 1.2)
matrix
mimemagic (~> 0.3.8)
newrelic_rpm
Expand Down
9 changes: 9 additions & 0 deletions app/controllers/madmin/application_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module Madmin
class ApplicationController < Madmin::BaseController
before_action :ensure_admin

def ensure_admin
redirect_to "/" if current_user.nil? || !current_user.admin
end
end
end
4 changes: 4 additions & 0 deletions app/controllers/madmin/estimates_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module Madmin
class EstimatesController < Madmin::ResourceController
end
end
4 changes: 4 additions & 0 deletions app/controllers/madmin/projects_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module Madmin
class ProjectsController < Madmin::ResourceController
end
end
4 changes: 4 additions & 0 deletions app/controllers/madmin/stories_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module Madmin
class StoriesController < Madmin::ResourceController
end
end
4 changes: 4 additions & 0 deletions app/controllers/madmin/users_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module Madmin
class UsersController < Madmin::ResourceController
end
end
24 changes: 24 additions & 0 deletions app/madmin/resources/estimate_resource.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
class EstimateResource < Madmin::Resource
# Attributes
attribute :id, form: false
attribute :best_case_points
attribute :worst_case_points

# Associations
attribute :story
attribute :user

# Uncomment this to customize the display name of records in the admin area.
# def self.display_name(record)
# record.name
# end

# Uncomment this to customize the default sort column and direction.
# def self.default_sort_column
# "created_at"
# end
#
# def self.default_sort_direction
# "desc"
# end
end
26 changes: 26 additions & 0 deletions app/madmin/resources/project_resource.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
class ProjectResource < Madmin::Resource
# Attributes
attribute :id, form: false
attribute :title
attribute :status

# Associations
attribute :stories
attribute :estimates
attribute :users
attribute :parent
attribute :projects

def self.display_name(record)
record.title.truncate(20)
end

# Uncomment this to customize the default sort column and direction.
# def self.default_sort_column
# "created_at"
# end
#
# def self.default_sort_direction
# "desc"
# end
end
26 changes: 26 additions & 0 deletions app/madmin/resources/story_resource.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
class StoryResource < Madmin::Resource
# Attributes
attribute :id, form: false
attribute :title
attribute :description
attribute :real_score
attribute :extra_info

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is leaving out position intentional?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes! I don't think it makes a difference for the purpose of this admin panel.

# Associations
attribute :project
attribute :estimates
attribute :users

def self.display_name(record)
record.title.truncate(30)
end

# Uncomment this to customize the default sort column and direction.
# def self.default_sort_column
# "created_at"
# end
#
# def self.default_sort_direction
# "desc"
# end
end
23 changes: 23 additions & 0 deletions app/madmin/resources/user_resource.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
class UserResource < Madmin::Resource
# Attributes
attribute :id, form: false
attribute :email
attribute :name
attribute :admin

# Associations
attribute :estimates

def self.display_name(record)
record.name.truncate(12)
end

# Uncomment this to customize the default sort column and direction.
# def self.default_sort_column
# "created_at"
# end
#
# def self.default_sort_direction
# "desc"
# end
end
26 changes: 26 additions & 0 deletions app/views/layouts/madmin/application.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<!DOCTYPE html>
<html lang="<%= I18n.locale %>">
<head>
<meta charset="utf-8">
<meta name="ROBOTS" content="NOODP">
<meta name="viewport" content="initial-scale=1">
<title>
Madmin: <%= Rails.application.class %>
</title>

<%= csrf_meta_tags %>

<%= render "javascript" %>
</head>
<body class="min-h-screen">
<div class="md:flex w-full min-h-screen">
<div id="sidebar" class="md:w-64 p-4 flex-shrink-0 border-r">
<%= render "navigation" -%>
</div>
<main class="flex-grow p-4 overflow-x-scroll" role="main">
<%#= render "flashes" -%>
<%= yield %>
</main>
</div>
</body>
</html>
23 changes: 23 additions & 0 deletions app/views/madmin/application/_form.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<%= form_with model: [:madmin, record], url: (record.persisted? ? resource.show_path(record) : resource.index_path), local: true do |form| %>
<% if form.object.errors.any? %>
<div class="mb-4 rounded-md text-sm text-red-700 bg-red-100 p-4">
<div class="mb-2 font-medium leading-5 text-red-800">There was <%= pluralize form.object.errors.full_messages.count, "error" %> with your submission</div>

<% form.object.errors.full_messages.each do |message| %>
<div class="ml-4"><%= message %></div>
<% end %>
</div>
<% end %>

<% resource.attributes.values.each do |attribute| %>
<% next if attribute.field.nil? %>
<% next unless attribute.field.visible?(action_name) %>
<% next unless attribute.field.visible?(:form) %>

<div class="mb-4 md:flex">
<%= render partial: attribute.field.to_partial_path("form"), locals: { field: attribute.field, record: record, form: form, resource: resource } %>
</div>
<% end %>

<%= form.submit class: "bg-white hover:bg-gray-100 text-gray-800 font-semibold py-2 px-4 border border-gray-400 rounded shadow" %>
<% end %>
107 changes: 107 additions & 0 deletions app/views/madmin/application/_javascript.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
<script src="https://cdn.tailwindcss.com?plugins=forms,typography,aspect-ratio,line-clamp"></script>
<%= stylesheet_link_tag "https://unpkg.com/flatpickr/dist/flatpickr.min.css", "data-turbo-track": "reload" %>
<%= stylesheet_link_tag "https://unpkg.com/trix/dist/trix.css", "data-turbo-track": "reload" %>
<%= stylesheet_link_tag "https://unpkg.com/tom-select/dist/css/tom-select.min.css", "data-turbo-track": "reload" %>

<script type="importmap" data-turbo-track="reload">
{
"imports": {
"@hotwired/stimulus": "https://unpkg.com/@hotwired/stimulus/dist/stimulus.js",
"@hotwired/turbo": "https://unpkg.com/@hotwired/turbo",
"@hotwired/turbo-rails": "https://unpkg.com/@hotwired/turbo-rails",
"@rails/actiontext": "https://unpkg.com/@rails/actiontext@<%= npm_rails_version %>/app/assets/javascripts/actiontext.js",
"@rails/activestorage": "https://unpkg.com/@rails/activestorage@<%= npm_rails_version %>/app/assets/javascripts/activestorage.esm.js",
"flatpickr": "https://unpkg.com/flatpickr/dist/esm/index.js",
"stimulus-flatpickr": "https://unpkg.com/stimulus-flatpickr@3.0.0-0/dist/index.m.js",
"tailwindcss-stimulus-components": "https://unpkg.com/tailwindcss-stimulus-components/dist/tailwindcss-stimulus-components.modern.js",
"tom-select": "https://unpkg.com/tom-select/dist/esm/tom-select.complete.js",
"trix": "https://unpkg.com/trix"
}
}
</script>
<script async src="https://unpkg.com/es-module-shims/dist/es-module-shims.js"></script>

<script type="module">
import * as Turbo from "@hotwired/turbo-rails"

import * as ActiveStorage from "@rails/activestorage"
ActiveStorage.start()
import "trix"
import "@rails/actiontext"

import { Application, Controller } from '@hotwired/stimulus'
const application = Application.start()

import { Dropdown } from "tailwindcss-stimulus-components"
application.register("dropdown", Dropdown)

import StimulusFlatpickr from "stimulus-flatpickr"
application.register("flatpickr", StimulusFlatpickr)

import TomSelect from "tom-select"

(() => {
application.register('select', class extends Controller {
static values = {
options: Object,
url: String
}

connect() {
this.select = new TomSelect(this.element, {
plugins: ['remove_button'],
valueField: 'id',
labelField: 'name',
searchField: 'name',
load: (search, callback) => {
let url = search ? `${this.urlValue}?q=${search}` : this.urlValue;
fetch(url)
.then(response => response.json())
.then(json => {
callback(json);
}).catch(() => {
callback();
});
}
})
}

disconnect() {
this.select.destroy()
}
})

application.register('nested-form', class extends Controller {
static get targets() {
return [ "links", "template" ]
}

connect() {
this.wrapperClass = this.data.get("wrapperClass") || "nested-fields"
}

add_association(event) {
event.preventDefault()

var content = this.templateTarget.innerHTML.replace(/NEW_RECORD/g, new Date().getTime())
this.linksTarget.insertAdjacentHTML('beforebegin', content)
}

remove_association(event) {
event.preventDefault()

let wrapper = event.target.closest("." + this.wrapperClass)

// New records are simply removed from the page
if (wrapper.dataset.newRecord == "true") {
wrapper.remove()

// Existing records are hidden and flagged for deletion
} else {
wrapper.querySelector("input[name*='_destroy']").value = 1
wrapper.style.display = 'none'
}
}
})
})()
</script>
32 changes: 32 additions & 0 deletions app/views/madmin/application/_navigation.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<div class="flex flex-col h-full text-sm" data-controller="dropdown">
<div class="flex md:block justify-between items-center">
<div class="flex md:block items-center">
<h1 class="mr-2 md:p-2 text-xl font-semibold">Madmin</h1>
<% if main_app.respond_to?(:root_url) %>
<%= link_to main_app.root_url, class: "block p-2 rounded hover:bg-gray-200", data: { turbo: false } do %>
← Back <span class="hidden md:inline">to App</span>
<% end %>
<% end %>
</div>

<div class="-mr-2 flex items-center md:hidden">
<button data-action="click->dropdown#toggle touch->dropdown#toggle click@window->dropdown#hide touch@window#dropdown->hide" type="button" class="bg-white rounded-md p-2 inline-flex items-center justify-center text-gray-400 hover:bg-gray-200 focus:outline-none focus:ring-2 focus-ring-inset focus:ring-white" id="main-menu" aria-haspopup="true">
<span class="sr-only">Open main menu</span>
<!-- Heroicon name: outline/menu -->
<svg class="h-6 w-6" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke="currentColor" aria-hidden="true">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M4 6h16M4 12h16M4 18h16"></path>
</svg>
</button>
</div>
</div>

<div class="hidden md:flex flex-col flex-grow justify-between" data-dropdown-target="menu">
<% Madmin.resources.each do |resource| %>
<%= nav_link_to resource.friendly_name.pluralize, resource.index_path, class: "block p-2 rounded hover:bg-gray-100", starts_with: resource.index_path, active_class: "font-bold text-black" %>
<% end %>

<div class="mt-auto">
<%= link_to "View Madmin on GitHub", "https://github.com/excid3/madmin", target: :_blank, class: "block p-2 rounded text-gray-500 hover:bg-gray-100" %>
</div>
</div>
</div>
7 changes: 7 additions & 0 deletions app/views/madmin/application/edit.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<h1 class="text-xl mb-4">
<%= link_to resource.friendly_name.pluralize, resource.index_path, class: "text-indigo-500" %>
/
<strong>Edit <%= link_to resource.display_name(@record), resource.show_path(@record), class: "text-indigo-500" %></strong>
</h1>

<%= render partial: "form", locals: { record: @record, resource: resource } %>
Loading