Access Active Storage models with Active Admin - ruby-on-rails

Is it possible to add the active storage models to active admin? I have tried to add them in the usual active admin way, however, the rails app just crashes.

After a few comments and a quick chat, here's the final solution:
ActiveStorage delivers active_storage_blobs, giving you an extra layer of information: metadata, url, redirects, booleans etc.
These ones are handled by ActiveStorage::Blob.
rails g active_admin:resource active_storage_blobs will throw an error. The reason is because ActiveStorage follows a different setup and ActiveRecord. In this case, get the resource directly from the Active Storage.
For now, the solution is:
rails g active_admin:resource ActiveStorage::Blob
This will display the blob data as an own index inside ActiveAdmin. Everything else should follow the same pattern as normal.
Happy coding!

Related

Active Admin and the Apartment Gem

I am new to Active Admin but from what I have seen so far I think this is quite easy to implement.
I have an app with the apartment gem to add multi-tenancy.
I am adding active admin to my app. Apartment uses PostgreSQL schemas to segregate data. So for example by default your models in Apartment have a 'public' tenant unless you call something like Apartment::Tenant.switch!('abc').
In my app my User and Company model are in the public tenant and everything else is in tenants. So out of the box Active Admin works fine except that the tenant models show no records - as they should.
I did some tinkering and manually added Apartment::Tenant.switch!('abc') one of my AA model files and that worked perfect. Here is an ideal solution:
when the AdminUser logs in the tenant gets set to a default (say the first tenant)
On each tenanted model there could be a select menu that submits a param (?tenant=abc) and then the tenant is changed
The active tenant is persisted in perhaps the AdminUser session store so you can work in the same tenant data until you need to switch.
I think I can do this myself quite easily but I wanted to see if there was any Active Admin specific issues I would need to address like:
Does AA have an equivalent of a application controller? It would be nice to keep the tenant switching logic in there vs the main one.
The alternate AA Devise AdminUser has a separate session variable store available right?
Any suggestions would be appreciated - I will post my final solution / code back to this post once I sort it out.
Does AA have an equivalent of a application controller? It would be nice to keep the tenant switching logic in there vs the main one.
Indeed, there is an ActiveAdmin::BaseController, but the gem authors don't talk about using it for customization, not sure why. Seems like a fine place for the logic you're talking about.
I've never needed to modify it before, but here's a blog article from someone who did.
The alternate AA Devise AdminUser has a separate session variable store available right?
Hmmm. Devise uses Warden for session management, which supports multiple user 'scopes' logged in simultaneously as well as separate session data, and if memory serves from when I've dug around in the code Warden puts the separate session data in a different key in the same cookie. Not sure if this is what you meant, but I verified that it definitely does not use a different cookie for users vs admins in my current ActiveAdmin-using Rails project.
Not a definitive answer, but moving the ball!

ActiveAdmin without ActiveRecord

Is there a way to make ActiveAdmin show information from a resource that isn't a database?
I have a model that's instantiated from JSON received remotely. I would like to get ActiveAdmin to show this information.
If I seed the database, ActiveAdmin will show the same number of records that I have in the seed file. When I comment out the seed, it shows nothing.
Yes, but you will have to change ActiveAdmin code, and will not be trivial.
Active admin depends on rails which depends on active record.
It also extensively use active record features on the code.
The best path to go is to write a db adapter for Rails using your API. There is one question here that addresses how to create a DB adapter.

Track a user who has updated data in Ruby on Rails

I am new to Ruby on Rails. The rails application that I have developed has several models including one for user that stores user name, passwords and other user related information.
Now the problem is that a few columns of a table corresponding to a model has modified erratically. Now I want to know if Ralis has any feature so that I can know the user who has done this or this is because of some other reasons.
You can try installing Userstamp and maybe Paper Trail to track changes to records. If you've implemented the User model yourself (as opposed to a framework like devise), you'll need to read the docs carefully to see what properties are expected of your User models to get the full benefit.
Using devise
It adds other columns(yours) in migrate, before generate views
https://github.com/collectiveidea/audited might provide the auditing you require.

Uploadify + Paperclip + Rails nested association before_save

I need to create a model application form where models can fill it and add pictures to it.
I'm following this example with Uploadify, Paperclip and Rails 3 approach.
https://github.com/websymphony/Rails3-Paperclip-Uploadify
To the pictures, i have an polymorphic Attachment model and i would like to attach those ajax uploaded attachments to the yet unsaved model form and there's is where the tricky part cames.
Users are not logged, so there's no "model_id" until is saved.
Since i'm showing the user a small preview of the images that he uploaded in the form by ajax after each upload i need some way to correlate them.
I was thinking about some middle token until the model is saved but i'm not sure whats the best approach to accomplish this.
Thank you!
We dumped Uploadify when moving to Rails 3 and are now using jQuery-File-Upload.
https://github.com/blueimp/jQuery-File-Upload
Setting up a bunch of middleware to do nothing more than upload files seems like a major hassle.
Only problem with the jquery solution is multiple uploads aren't supported in IE.

Paperclip and failed validation - avoid reupload

I am currently setting up Paperclip for a model with Rails 3. When one of the fields fails validation (title), the user has to upload the file again. This is not very user friendly :/
The recommendation from the Paperclip forum is to move the Paperclip stuff into a related model. My model is very simple with just a few fields, so I would like to avoid having two pages/steps for creating a record.
arts/create (when valid) -> arts_image/create
Any suggestions?
I use the two-step solution with a separate model. Although it's possible to code and hack your way around the default behaviour, you could also validate on the client-side with JS.
Look at this article http://ryantownsend.co.uk/articles/storing-paperclip-file-uploads-when-validation-fails.html
Cached version of the article: http://web.archive.org/web/20100919151143/http://ryantownsend.co.uk/articles/storing-paperclip-file-uploads-when-validation-fails.html
I have taken a different approach by 'serving' the file back to the client and re-accepting it when the form gets resubmitted.
https://stackoverflow.com/a/25853569/7693

Resources