Module: NaranyaEcm::Behaviors::MediaMethods

Extended by:
ActiveSupport::Concern
Included in:
Category, Content, ContentVersion
Defined in:
lib/naranya_ecm/behaviors/media_methods.rb

Instance Method Summary collapse

Instance Method Details

#detect_media_by_roles(options) ⇒ Object



71
72
73
# File 'lib/naranya_ecm/behaviors/media_methods.rb', line 71

def detect_media_by_roles(options)
  select_media_by_roles(options).first
end

#detect_media_url_by_roles(options) ⇒ Object



80
81
82
# File 'lib/naranya_ecm/behaviors/media_methods.rb', line 80

def detect_media_url_by_roles(options)
  select_media_urls_by_roles(options).first
end

#find_media_by_role(role_name) ⇒ Object



6
7
8
9
# File 'lib/naranya_ecm/behaviors/media_methods.rb', line 6

def find_media_by_role(role_name)
  mr = self.media_resources.nil? ? [] : self.media_resources
  mr.detect { |mr| mr.roles.include? role_name }
end

#find_media_url_by_role(role_name) ⇒ Object



11
12
13
14
# File 'lib/naranya_ecm/behaviors/media_methods.rb', line 11

def find_media_url_by_role(role_name)
  mr = self.find_media_by_role role_name
  mr.present? ? mr.downloadable_url : nil
end

#select_media_by_role(role_name) ⇒ Object



16
17
18
# File 'lib/naranya_ecm/behaviors/media_methods.rb', line 16

def select_media_by_role(role_name)
  self.media_resources.select { |mr| mr.roles.include? role_name }
end

#select_media_by_roles(options) ⇒ Object

Returns an Array of MediaResource objects which match the given options in their role list.

Attributes

  • options - The given options to select the MediaResource objects

Options

You may which to break out options as a separate item since there maybe multiple items. Note options are prefixed with a colon, denoting them as a

  • :match - An Array of roles that must exist in matching MediaResource objects.

  • :prefer - An Array of values consisting of:

    • A String indicating a desired role

    • A Hash containing the desired role as key, and an Array of fallback roles as value.

Examples

Illustrate the behaviour of the method using examples. Indent examples:

base = Base.new("Example String")
base.method_name("Example", "more")

Raises:

  • (ArgumentError)


48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/naranya_ecm/behaviors/media_methods.rb', line 48

def select_media_by_roles(options)

  options = options.with_indifferent_access if options.is_a?(Hash) && !options.is_a?(ActiveSupport::HashWithIndifferentAccess)

  match = options.is_a?(Hash) ? options[:match] : options
  match = [match.to_s] if match.is_a?(String) || match.is_a?(Symbol)
  
  raise ArgumentError, "No match options given" if match.blank?

  except = options.is_a?(Hash) ? options[:except] : []
  except = [except.to_s] if except.is_a?(String) || except.is_a?(Symbol)
  except = [] if except.blank?

  filtered_media = self.media_resources.select do |mr|
    ((mr.roles & match).count == match.count) && ((mr.roles & except).count < 1)
  end

  prefer = options.is_a?(Hash) ? options[:prefer] : nil
  prefer = [prefer.to_s] if prefer.is_a?(String) || prefer.is_a?(Symbol)
  
  filtered_media
end

#select_media_urls_by_role(role_name) ⇒ Object



20
21
22
23
# File 'lib/naranya_ecm/behaviors/media_methods.rb', line 20

def select_media_urls_by_role(role_name)
  mr = self.select_media_by_role(role_name).map(&:downloadable_url)
  mr.flatten.uniq
end

#select_media_urls_by_roles(options) ⇒ Object



75
76
77
78
# File 'lib/naranya_ecm/behaviors/media_methods.rb', line 75

def select_media_urls_by_roles(options)
  selected_media = select_media_by_roles(options)
  selected_media.present? ? selected_media.map(&:downloadable_url) : nil
end