Rails 7 - ActiveAdmin scope for ActiveStorage - ruby-on-rails

I use ActiveStorage to store 'photos' and "avatar" for Profile model. My activeadmin resource:
ActiveAdmin.register ActiveStorage::Attachment do
scope :only_attachments_where_name_column_is_photos
end
Question is simple - how to create that scope if I do not have ActiveStorageAttachment model ?

Related

Rails Active Storage and Acts as List

I've been working on a way to add acts_as_list to ActiveStorage::Attachment using a concern. I've been working on this for two days and can't seem to find a way to add the line acts_as_list to the model. I tried to load with an initializer. Any thoughts?
Here's the initializer code:
Rails.configuration.to_prepare do
class ActiveStorage::Attachment
include ActiveStorageActsAsList
end
end
Concern
module ActiveStorageActsAsList
extend ActiveSupport::Concern
included do
acts_as_list
end
end

Rails admin gem showing s3 attachments which is IMAGES/PDF/DOCS/XLSX

I am using rails_admin gem for the rails api application for the backend admin side.
I am using rails 6 with active_storage and storing attachments on the S3.
On the admin side, I need to display the list of attachments which might be images or files anything.
My question is How to show those in index method, do I need to show images then what to show in pdf/docs, do I need to show the only the link of s3?
currently, it looks like these broken images are the images and other one were files
My model
class Attachment < AttachmentBlock::ApplicationRecord
self.table_name = :attachments
include Wisper::Publisher
has_one_attached :attachment
belongs_to :account, class_name: 'AccountBlock::Account'
has_and_belongs_to_many :orders , class_name: 'BxBlockOrdermanagement::Order'
scope :not_expired, -> {where('is_expired = ?',false)}
end
What should I use here to list the attachment that the user upload?
how to check the attachment type and then if its image then shows the image and if it files then show file url from s3?
thanks.
Yours is a two part question:
To add links to the side menu on rails admin you need to define models so if you wanted an index for all the attachments of type 'pdf' you could use rails STI (single table inheritance) or define a custom default_scope like this:
class ImageAttachment < Attachment
def self.default_scope
where('attachments.attachment LIKE ?', '%png') }
end
end
To customize the row of each individual attachment record you need to defined that behavior for the field on the model rails admin config.
For example you can put this block of code inside your ImageAttachment class.
class ImageAttachment < Attachment
def self.default_scope
where('attachments.attachment LIKE ?', '%png') }
end
rails_admin do
configure :attachment do
view = bindings[:view]
attachment = bindings[:object]
if view
view.content_tag(:img, attachment.attachment.url)
else
''
end
end
list do
field :attachment
end
end
end
As you can see inside the attachment field configuration block you have access to the attachment object and of course all its methods, you can determine the type of attachment and do some special html rendering.

Undefined method 'attachment_path' for Rails engine model with ActiveStorage attachment

I have a Rails Engine and Rails app, both running Rails 6.0.0.rc1.
Engine defines model with ActiveStorage attachments, eg.
module Shop
class Product < ApplicationRecord
...
has_one_attached :image
has_many_attached :images
end
end
Using this model in both engine's and application views and trying to call = url_for(product.image) raises an exception undefined method 'attachment_path' for #<#<Class:XXX>:XXX>. product.image.attached? returns true, thus attachment is available.
This code was extracted from Rails application where it worked just fine. Is there a special route helper for Rails Engine model attachments or any setup other than rails active_storage:install needed not mentioned in the documentation?
I got this working using main_app.url_for(product.image)!
Works both in engine views and main Rails app views.
Just as reference for others, you can read more about this in the Rails Guides.

rails scope based on models parent attribute

I have 2 models. line_item and account.
line_item belongs to an account.
account has a column is_active.
I'm looking for a way to write a Rails scope to find all line_items where their account is_active = true
Something like
LineItem.should_display
EDIT
class LineItem < ActiveRecord::Base
scope :should_display, -> { joins(:account).where(accounts: {is_active: true}) }
end
This produces the same result as adding the following class method in your LineItem model.
def self.should_display
joins(:account).where(accounts: {is_active: true})
end
I think you can find more information in the Rails guides for Active Record Querying: http://guides.rubyonrails.org/active_record_querying.html

How to use a find_by_sql query for an ActiveAdmin index page?

I'm using active admin on a project. I have a request to create a new resource and was given a complex SQL query to use - which connects to a different DB. All is good - however, I'm somewhat new to ActiveAdmin and curious how to get the index page to use my custom query vs. the default resource.
I just need a nudge/sample to see how to override this default activity.
You can declare scopes and filters when registering your model with ActiveAdmin.
app/model/your_model.rb
class YourModel < ActiveRecord::Base
scope :my_scope, where('some custom SQL')
scope :my_other_scope, where('some other custom SQL')
end
app/admin/your_models.rb
ActiveAdmin.register YourModel do
scope :my_scope, default: true
scope :my_other_scope
end

Resources