demo-rails-administrate
Demonstration of Ruby on Rails with the Administrate administration gem by Thoughtbot
https://github.com/joelparkerhenderson/demo-rails-administrate
Science Score: 44.0%
This score indicates how likely this project is to be science-related based on various indicators:
-
✓CITATION.cff file
Found CITATION.cff file -
✓codemeta.json file
Found codemeta.json file -
✓.zenodo.json file
Found .zenodo.json file -
○DOI references
-
○Academic publication links
-
○Committers with academic emails
-
○Institutional organization owner
-
○JOSS paper metadata
-
○Scientific vocabulary similarity
Low similarity (5.4%) to scientific vocabulary
Repository
Demonstration of Ruby on Rails with the Administrate administration gem by Thoughtbot
Basic Info
Statistics
- Stars: 2
- Watchers: 2
- Forks: 0
- Open Issues: 0
- Releases: 0
Metadata Files
README.md
Demo Rails Administrate
Demonstration of:
Ruby on Rails web framework
Administrate gem by ThoughtBot
Prepare
Ruby 3 or greater:
sh
ruby --version
Rails 7 or greater:
sh
gem list rails
SQLite 3 or greater:
sh
sqlite3 --version
Create
Create demo:
sh
rails new demo
cd demo
asdf local ruby latest
bin/rails db:prepare
bin/rails server
Scaffold
Edit file config/routes:
ruby
root "users#index"
Generate scaffolds:
sh
bin/rails generate scaffold User name:string description:string
bin/rails generate scaffold Group name:string description:string
bin/rails generate migration CreateJoinTableGroupUser group user
bin/rails db:migrate
Edit file app/models/user.rb
ruby
class User < ApplicationRecord
has_and_belongs_to_many :groups
end
Edit file app/models/group.rb
ruby
class Group < ApplicationRecord
has_and_belongs_to_many :users
end
Devise
Add to Gemfile:
```ruby
Devise authentication
gem "devise" ```
Setup:
sh
bundle install
rails generate devise:install
rails generate devise User
rails db:migrate
Root
Ensure you have defined root_url to something in your file config/routes.rb.
Example:
ruby
root to: "home#index"
Flash
Ensure you have flash messages in file app/views/layouts/application.html.erb.
Example:
ruby
<p class="notice"><%= notice %></p>
<p class="alert"><%= alert %></p>
Mailer
Ensure you have defined default_url_options in your file config/environments/development.rb.
Example:
ruby
config.action_mailer.default_url_options = { host: 'localhost', port: 3000 }
Seeds
Edit file db/seeds.rb
```rb
Create some user xamples
usera = User.create(name: "Alice Adams", description: "Lorem ipsum dolor sit amet", email: "alice@example.com") userb = User.create(name: "Bob Brown", description: "Vitae suscipit tellus mauris", email: "bob@example.com") userc = User.create(name: "Carol Clark", description: "Viverra justo nec ultrices", email: "carol@example.com") userd = User.create(name: "Dave Davis", description: "Quis eleifend quam adipiscing", email: "dave@example.com") usere = User.create(name: "Eve Evans", description: "Lacus suspendisse faucibus interdum", email: "eve@example.com") userf = User.create(name: "Frank Franklin", description: "Etiam ut feugiat nibh", email: "frank@example.com")
Create some group examples
groupa = Group.create(name: "Archery", description: "Fusce ullamcorper sem curabitur") groupb = Group.create(name: "Basketball", description: "Orci varius natoque penatibus") groupc = Group.create(name: "Cricket", description: "Vivamus id nisi efficitur") groupd = Group.create(name: "Dodgeball", description: "Cras vulputate laoreet est") groupe = Group.create(name: "Equestrianism", description: "Pellentesque nibh arcu tempus") groupf = Group.create(name: "Fencing", description: "Dignissim fringilla diam ultrices")
Create some user group examples
usera.groups = [groupa, groupb, groupc] userb.groups = [groupb, groupc, groupd] userc.groups = [groupc, groupd, groupe] userd.groups = [groupd, groupe, groupf] usere.groups = [groupe, groupf, groupa] userf.groups = [groupf, groupa, groupb] ```
Run:
sh
bin/rails db:seed
Administrate gem
Add to Gemfile:
ruby
gem 'administrate'
Run:
sh
bundle install
bin/rails generate administrate:install
The installer adds some new routes to your config/routes.rb.
The installer creates a controller at app/controllers/admin/application_controller.rb.
The generator creates routes in the file config/routes.rb:
ruby
Rails.application.routes.draw do
namespace :admin do
resources :groups
resources :users
root to: "groups#index"
end
…
end
The generator creates a Dashboard and a Controller for each of your ActiveRecord resources:
sh
app/controllers/admin/groups_controller.rb
app/controllers/admin/users_controller.rb
app/dashboards/group_dashboard.rb
app/dashboards/user_dashboard.rb
The file app/dashboards/user_dashboard.rb:
```ruby require "administrate/base_dashboard"
class UserDashboard < Administrate::BaseDashboard # ATTRIBUTETYPES # a hash that describes the type of each of the model's fields. # # Each different type represents an Administrate::Field object, # which determines how the attribute is displayed # on pages throughout the dashboard. ATTRIBUTETYPES = { groups: Field::HasMany, id: Field::Number, createdat: Field::DateTime, updatedat: Field::DateTime, name: Field::String, description: Field::String, email: Field::String, encryptedpassword: Field::String, resetpasswordtoken: Field::String, resetpasswordsentat: Field::DateTime, remembercreatedat: Field::DateTime, }.freeze
# COLLECTIONATTRIBUTES # an array of attributes that will be displayed on the model's index page. # # By default, it's limited to four items to reduce clutter on index pages. # Feel free to add, remove, or rearrange items. COLLECTIONATTRIBUTES = %i[ groups id createdat updatedat ].freeze
# SHOWPAGEATTRIBUTES # an array of attributes that will be displayed on the model's show page. SHOWPAGEATTRIBUTES = %i[ groups id createdat updatedat name description email encryptedpassword resetpasswordtoken resetpasswordsentat remembercreatedat ].freeze
# FORMATTRIBUTES
# an array of attributes that will be displayed
# on the model's form (new and edit) pages.
FORMATTRIBUTES = %i[
groups
name
description
email
encryptedpassword
resetpasswordtoken
resetpasswordsentat
remembercreatedat
].freeze
# COLLECTIONFILTERS # a hash that defines filters that can be used while searching via the search # field of the dashboard. # # For example to add an option to search for open resources by typing "open:" # in the search field: # # COLLECTIONFILTERS = { # open: ->(resources) { resources.where(open: true) } # }.freeze COLLECTION_FILTERS = {}.freeze
# Overwrite this method to customize how users are displayed # across all pages of the admin dashboard. # # def display_resource(user) # "User ##{user.id}" # end end ```
Visit http://localhost:3000/admin to see your new dashboard in action.
Adminsitrate customization
The Admin::ApplicationController can be customized to add authentication logic, authorization, pagination, or other controller-level concerns.
The routes can be customized to show or hide different models on the dashboard.
Each dashboard specifies which attributes should be displayed. For example see file app/dashboards/user_dashboard.rb which defines class UserDashboard.
Each admin controller can be overwritten to specify custom behavior. For example see file app/controllers/admin/users_controller.rb and class Admin::UsersController.
The file app/dashboards/user_dashboard.rb defines the index page collection attributes here:
COLLECTION_ATTRIBUTES = %i[
groups
id
created_at
updated_at
].freeze
We change these to be:
ruby
COLLECTION_ATTRIBUTES = %i[
id
name
groups
].freeze
We edit SHOW_PAGE_ATTRIBUTES and FORM_ATTRIBUTES to move groups to the bottom of the array, because we prefer to see the user's information before the user's groups.
The file app/dashboards/gropu_dashboard.rb defines the index page collection attributes here:
COLLECTION_ATTRIBUTES = %i[
users
id
created_at
updated_at
].freeze
We change these to be:
ruby
COLLECTION_ATTRIBUTES = %i[
id
name
users
].freeze
We edit SHOW_PAGE_ATTRIBUTES and FORM_ATTRIBUTES to move users to the bottom of the array, because we prefer to see the groups's information before the groups's users.
Owner
- Name: Joel Parker Henderson
- Login: joelparkerhenderson
- Kind: user
- Location: California
- Website: http://www.joelparkerhenderson.com
- Repositories: 319
- Profile: https://github.com/joelparkerhenderson
Software developer. Technology consultant. Creator of GitAlias.com, NumCommand.com, SixArm.com, and many open source projects.
Citation (CITATION.cff)
cff-version: 1.2.0
title: Demo Rails Administrate
message: >-
If you use this work and you want to cite it,
then you can use the metadata from this file.
type: software
authors:
- given-names: Joel Parker
family-names: Henderson
email: joel@joelparkerhenderson.com
affiliation: joelparkerhenderson.com
orcid: 'https://orcid.org/0009-0000-4681-282X'
identifiers:
- type: url
value: ''
description: Demo Rails Administrate
repository-code: ''
abstract: >-
Demo Rails Administrate
license: See license file
GitHub Events
Total
- Push event: 1
Last Year
- Push event: 1
Committers
Last synced: 11 months ago
Top Committers
| Name | Commits | |
|---|---|---|
| Joel Parker Henderson | j****l@j****m | 2 |