RailsAdmin - Overview and Best practices
-
Upload
benoit-benezech -
Category
Technology
-
view
20.706 -
download
2
description
Transcript of RailsAdmin - Overview and Best practices
![Page 1: RailsAdmin - Overview and Best practices](https://reader036.fdocuments.net/reader036/viewer/2022070313/554be19eb4c90556328b471a/html5/thumbnails/1.jpg)
RailsAdminOverview & Best practices
mercredi 7 août 13
![Page 2: RailsAdmin - Overview and Best practices](https://reader036.fdocuments.net/reader036/viewer/2022070313/554be19eb4c90556328b471a/html5/thumbnails/2.jpg)
14 years agoAcceptable in the 90’
mercredi 7 août 13
![Page 3: RailsAdmin - Overview and Best practices](https://reader036.fdocuments.net/reader036/viewer/2022070313/554be19eb4c90556328b471a/html5/thumbnails/3.jpg)
Data Admin, stand-alone
• + Pros
• Reliable
• Powerful
• - Cons
• No business rules whatsoever
mercredi 7 août 13
![Page 4: RailsAdmin - Overview and Best practices](https://reader036.fdocuments.net/reader036/viewer/2022070313/554be19eb4c90556328b471a/html5/thumbnails/4.jpg)
mercredi 7 août 13
![Page 5: RailsAdmin - Overview and Best practices](https://reader036.fdocuments.net/reader036/viewer/2022070313/554be19eb4c90556328b471a/html5/thumbnails/5.jpg)
Data Admin, in the application
• + Pros
• Leverages the whole application or some parts for business rules etc.
• - Cons
• No direct (fast) DB administration
• Your code?
In theory...
mercredi 7 août 13
![Page 6: RailsAdmin - Overview and Best practices](https://reader036.fdocuments.net/reader036/viewer/2022070313/554be19eb4c90556328b471a/html5/thumbnails/6.jpg)
• Admin generator
• Immediate startup
• Steep learning curve
• Extensible
• Powerful when leverages Rails Models and Cancan
• Admin builder
• Slower startup
• Simple DSL
• Usable
• Good for ad-hoc admin building when Models/Cancan rules don’t infer the UI
RailsAdmin ActiveAdmin
Grain of salt, common sense, etc.
mercredi 7 août 13
![Page 7: RailsAdmin - Overview and Best practices](https://reader036.fdocuments.net/reader036/viewer/2022070313/554be19eb4c90556328b471a/html5/thumbnails/7.jpg)
Pitfall
• ActiveAdmin lets you do the work
• => with some help from Formtastic :/
• RailsAdmin shortcuts ActiveModel
• => Complex custom code laying on the ORMs :/
• But hey! it works®*
ActiveModel does not provide anything for associations!
* Unless you screwed up your models associations definitions
mercredi 7 août 13
![Page 8: RailsAdmin - Overview and Best practices](https://reader036.fdocuments.net/reader036/viewer/2022070313/554be19eb4c90556328b471a/html5/thumbnails/8.jpg)
Minimum installation
• rails new new_app
• rails g model blog::post title:string content:text
• rails g model blog::comment content:text post:belongs_to
• gem ‘rails_admin’
• mount RailsAdmin::Engine => ‘’
mercredi 7 août 13
![Page 9: RailsAdmin - Overview and Best practices](https://reader036.fdocuments.net/reader036/viewer/2022070313/554be19eb4c90556328b471a/html5/thumbnails/9.jpg)
Big picture
• Actions
• Map routing to templates and controller
• Sections
• Map field configuration to each action
• Fields
• Handle fields configurations
mercredi 7 août 13
![Page 10: RailsAdmin - Overview and Best practices](https://reader036.fdocuments.net/reader036/viewer/2022070313/554be19eb4c90556328b471a/html5/thumbnails/10.jpg)
Actions-Sections CRUD mapping
• new => new < edit < base
• edit => update < edit < base
• edit/new nested => nested < edit < base
• edit/new modal => modal < edit < base
• index => list < base
• export => export < base
mercredi 7 août 13
![Page 11: RailsAdmin - Overview and Best practices](https://reader036.fdocuments.net/reader036/viewer/2022070313/554be19eb4c90556328b471a/html5/thumbnails/11.jpg)
Example
mercredi 7 août 13
![Page 12: RailsAdmin - Overview and Best practices](https://reader036.fdocuments.net/reader036/viewer/2022070313/554be19eb4c90556328b471a/html5/thumbnails/12.jpg)
The issue...
• Never-ending list of section/fields definitions
mercredi 7 août 13
![Page 13: RailsAdmin - Overview and Best practices](https://reader036.fdocuments.net/reader036/viewer/2022070313/554be19eb4c90556328b471a/html5/thumbnails/13.jpg)
Solutions
• Use configure as much as you can (doesn’t force the field list in opt-in mode)
• Use order
• Move each model configuration to a rails_admin block in class definition
• Hide fields you don’t need instead of listing them (less overhead usually)
mercredi 7 août 13
![Page 14: RailsAdmin - Overview and Best practices](https://reader036.fdocuments.net/reader036/viewer/2022070313/554be19eb4c90556328b471a/html5/thumbnails/14.jpg)
Associations
mercredi 7 août 13
![Page 15: RailsAdmin - Overview and Best practices](https://reader036.fdocuments.net/reader036/viewer/2022070313/554be19eb4c90556328b471a/html5/thumbnails/15.jpg)
The associations
• has_many => nested
• has_one => nested
• belongs_to => modal
• has_many :through => modal
• has_and_belong_to_many => modal
• It makes sense®
mercredi 7 août 13
![Page 16: RailsAdmin - Overview and Best practices](https://reader036.fdocuments.net/reader036/viewer/2022070313/554be19eb4c90556328b471a/html5/thumbnails/16.jpg)
Back to our models...
mercredi 7 août 13
![Page 17: RailsAdmin - Overview and Best practices](https://reader036.fdocuments.net/reader036/viewer/2022070313/554be19eb4c90556328b471a/html5/thumbnails/17.jpg)
You tell your ORM about it
mercredi 7 août 13
![Page 18: RailsAdmin - Overview and Best practices](https://reader036.fdocuments.net/reader036/viewer/2022070313/554be19eb4c90556328b471a/html5/thumbnails/18.jpg)
Stay on the Golden path
• Convention over configuration!
• RailsAdmin gets as much intel as it can from your models:
• Fields
• Associations
• Validations
• Don’t configure RailsAdmin when you can tweak your app to Rails’ best standards!
mercredi 7 août 13
![Page 19: RailsAdmin - Overview and Best practices](https://reader036.fdocuments.net/reader036/viewer/2022070313/554be19eb4c90556328b471a/html5/thumbnails/19.jpg)
But wait! Admin! Passwords and stuff!
• gem ‘devise’
• rails g devise:install
• rails g devise user
• rails g migration add_user_id_to_blog_posts user:belongs_to
• config/rails_admin.rb:
mercredi 7 août 13
![Page 20: RailsAdmin - Overview and Best practices](https://reader036.fdocuments.net/reader036/viewer/2022070313/554be19eb4c90556328b471a/html5/thumbnails/20.jpg)
Who am I?
mercredi 7 août 13
![Page 21: RailsAdmin - Overview and Best practices](https://reader036.fdocuments.net/reader036/viewer/2022070313/554be19eb4c90556328b471a/html5/thumbnails/21.jpg)
This is called authentication.Do you have authorization?• gem ‘cancan’
• rails g migration add_profile_to_users profile:string
• config/rails_admin.rb:
• rails g cancan:ability
mercredi 7 août 13
![Page 22: RailsAdmin - Overview and Best practices](https://reader036.fdocuments.net/reader036/viewer/2022070313/554be19eb4c90556328b471a/html5/thumbnails/22.jpg)
Some other profiles
• Admin can do anything
• Writer can
• access rails_admin
• access the dashboard
• CRUD Blog::Post that he owns
• for new posts, user_id will be forced to his id
mercredi 7 août 13
![Page 23: RailsAdmin - Overview and Best practices](https://reader036.fdocuments.net/reader036/viewer/2022070313/554be19eb4c90556328b471a/html5/thumbnails/23.jpg)
Ok, what do I get for my writers ?
mercredi 7 août 13
![Page 24: RailsAdmin - Overview and Best practices](https://reader036.fdocuments.net/reader036/viewer/2022070313/554be19eb4c90556328b471a/html5/thumbnails/24.jpg)
At last, some RailsAdmin field configuration!
mercredi 7 août 13
![Page 25: RailsAdmin - Overview and Best practices](https://reader036.fdocuments.net/reader036/viewer/2022070313/554be19eb4c90556328b471a/html5/thumbnails/25.jpg)
Deal with serialized fields
• rails g migration add_metadata_to_blog_posts metadata:text
• models/blog/post.rb
mercredi 7 août 13
![Page 26: RailsAdmin - Overview and Best practices](https://reader036.fdocuments.net/reader036/viewer/2022070313/554be19eb4c90556328b471a/html5/thumbnails/26.jpg)
Now what?
• It’s error prone :(
• It’s ugly :(
• It’s not user friendly :(
• It’s dangerous :(
• What should I do?
mercredi 7 août 13
![Page 27: RailsAdmin - Overview and Best practices](https://reader036.fdocuments.net/reader036/viewer/2022070313/554be19eb4c90556328b471a/html5/thumbnails/27.jpg)
Use some ‘virtual’ fields?
• You don’t need your object to respond to ‘block’ !
• You can create RailsAdmin fields out of the blue
• You just need to define value and parse_input methods (in & out the form)
• allowed_methods lets you modify the mass-assignable params on the object (safety)
mercredi 7 août 13
![Page 28: RailsAdmin - Overview and Best practices](https://reader036.fdocuments.net/reader036/viewer/2022070313/554be19eb4c90556328b471a/html5/thumbnails/28.jpg)
Say I need another one like ‘block’?
• Let’s create a metadata RailsAdmin field type, in our application
• lib/rails_admin/metadata.rb
mercredi 7 août 13
![Page 29: RailsAdmin - Overview and Best practices](https://reader036.fdocuments.net/reader036/viewer/2022070313/554be19eb4c90556328b471a/html5/thumbnails/29.jpg)
Time to leverage
No pollution in the model!
mercredi 7 août 13
![Page 30: RailsAdmin - Overview and Best practices](https://reader036.fdocuments.net/reader036/viewer/2022070313/554be19eb4c90556328b471a/html5/thumbnails/30.jpg)
Some actions?
mercredi 7 août 13
![Page 31: RailsAdmin - Overview and Best practices](https://reader036.fdocuments.net/reader036/viewer/2022070313/554be19eb4c90556328b471a/html5/thumbnails/31.jpg)
I need to publish my posts!
Define an action, then use it
mercredi 7 août 13
![Page 32: RailsAdmin - Overview and Best practices](https://reader036.fdocuments.net/reader036/viewer/2022070313/554be19eb4c90556328b471a/html5/thumbnails/32.jpg)
Then the view part, simple stuff
The view goes to rails_admin/main/publish.slim
Then add some translations for proper integration
mercredi 7 août 13
![Page 33: RailsAdmin - Overview and Best practices](https://reader036.fdocuments.net/reader036/viewer/2022070313/554be19eb4c90556328b471a/html5/thumbnails/33.jpg)
I’m in trouble
• Check that your models are ok (rails c)
• bundle open rails_admin
• trace @abstract_model, @object in actions
• trace @abstract_model, bindings[:object] in fields
mercredi 7 août 13
![Page 34: RailsAdmin - Overview and Best practices](https://reader036.fdocuments.net/reader036/viewer/2022070313/554be19eb4c90556328b471a/html5/thumbnails/34.jpg)
Someone said I need tests, I probably need tests, did I say I need tests?
https://github.com/sferik/rails_admin/wiki/Rspec-with-capybara-examples
mercredi 7 août 13
![Page 35: RailsAdmin - Overview and Best practices](https://reader036.fdocuments.net/reader036/viewer/2022070313/554be19eb4c90556328b471a/html5/thumbnails/35.jpg)
Thanks for listening!Full example code
https://github.com/bbenezech/blog_admin
@bbenezech github/Skype/twitter
Freelance
mercredi 7 août 13