Module: Controllers::DigitalAssetLinkable

Extended by:
ActiveSupport::Concern
Included in:
ContactsController, CustomersController, OpportunitiesController
Defined in:
app/concerns/controllers/digital_asset_linkable.rb

Overview

Provides link_images and link_videos actions for controllers that manage
digital assets via HABTM relationships (customers, contacts, opportunities).

Including controllers must define a digital_asset_linkable_record method
that returns the record to link digital assets to, and a
digital_asset_linkable_redirect_path method for the redirect target.

Examples:

class CustomersController < CrmController
  include Controllers::DigitalAssetLinkable

  private

  def digital_asset_linkable_record
    @customer
  end

  def digital_asset_linkable_redirect_path
    customer_path(@customer, anchor: 'digital_assets')
  end
end

Instance Method Summary collapse

Instance Method Details

POST /resources/:id/link_images



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'app/concerns/controllers/digital_asset_linkable.rb', line 31

def link_images
  record = digital_asset_linkable_record
  authorize!(:update, record)

  image_ids = Array(params[:image_ids]).map(&:to_i).uniq
  redirect_to digital_asset_linkable_redirect_path, alert: 'No images selected.' and return if image_ids.blank?

  images = Image.where(id: image_ids)
  existing_ids = record.digital_asset_ids & image_ids
  new_images = images.where.not(id: existing_ids)

  record.digital_assets << new_images if new_images.any?

  notice = new_images.any? ? "#{new_images.size} image(s) linked." : 'Selected images were already linked.'
  redirect_to digital_asset_linkable_redirect_path, notice: notice
end

POST /resources/:id/link_videos



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'app/concerns/controllers/digital_asset_linkable.rb', line 49

def link_videos
  record = digital_asset_linkable_record
  authorize!(:update, record)

  video_ids = Array(params[:video_ids]).map(&:to_i).uniq
  redirect_to digital_asset_linkable_redirect_path, alert: 'No videos selected.' and return if video_ids.blank?

  videos = Video.where(id: video_ids)
  existing_ids = record.digital_asset_ids & video_ids
  new_videos = videos.where.not(id: existing_ids)

  record.digital_assets << new_videos if new_videos.any?

  notice = new_videos.any? ? "#{new_videos.size} video(s) linked." : 'Selected videos were already linked.'
  redirect_to digital_asset_linkable_redirect_path, notice: notice
end