Class: CckForms::ParameterTypeClass::Album

Inherits:
Object
  • Object
show all
Includes:
NeofilesDenormalize, Base
Defined in:
lib/cck_forms/parameter_type_class/album.rb

Overview

Represents an ordered collection of photos (Image types).

Direct Known Subclasses

WatermarklessAlbum

Constant Summary

Constants included from NeofilesDenormalize

NeofilesDenormalize::NEOFILES_LAZY_ATTRS

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.demongoize_value(value, parameter_type_class = nil) ⇒ Object

Converts input array of attr hashes or IDs to array if Neofiles::Image (possibly lazy loadable)



23
24
25
26
27
28
29
30
# File 'lib/cck_forms/parameter_type_class/album.rb', line 23

def self.demongoize_value(value, parameter_type_class=nil)
  if value.respond_to? :each
    value = value.values if value.respond_to? :values
    value.map { |x| neofiles_mock_or_load(x) }.compact
  else
    []
  end
end

Instance Method Details

#build_form(form_builder, options) ⇒ Object

options:

value     - current value (ID or Neofiles::Album)
with_desc - if true, show the description edit richtext (default false)


36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/cck_forms/parameter_type_class/album.rb', line 36

def build_form(form_builder, options)
  set_value_in_hash options

  options = {

  }.merge options

  the_value = options[:value].is_a?(Array) ? options[:value] : []
  input_name_prefix = form_builder.object_name + '[value][]'
  widget_id_prefix = form_builder_name_to_id form_builder, '_value_'
  file_forms = []

  the_value.each do |image_id|
    image_id = image_id.is_a?(::Neofiles::File) ? image_id.id : image_id
    file_forms << cck_image_type.create_load_form( helper: self,
                                                                        file: image_id,
                                                                        input_name: input_name_prefix,
                                                                        append_create: false,
                                                                        clean_remove: true,
                                                                        widget_id: widget_id_prefix + file_forms.length.to_s,
                                                                        multiple: true,
                                                                        with_desc: options[:with_desc])
  end

  add_file_form = cck_image_type.create_load_form( helper: self,
                                                                        file: nil,
                                                                        input_name: input_name_prefix,
                                                                        append_create: true,
                                                                        clean_remove: true,
                                                                        widget_id: widget_id_prefix + file_forms.length.to_s,
                                                                        multiple: true,
                                                                        with_desc: options[:with_desc])

  id = form_builder_name_to_id form_builder, rand(1...1_000_000).to_s

  <<HTML
    <div class="neofiles-album-compact" id="#{id}">
      #{add_file_form if Rails.application.config.neofiles.album_append_create_side == :left}
      #{file_forms.join}
      #{add_file_form if Rails.application.config.neofiles.album_append_create_side == :right}
    </div>

    <script type="text/javascript">
      $(function() {
          $("##{id}").album();
      });
    </script>
HTML
end

#cck_image_typeObject



86
87
88
# File 'lib/cck_forms/parameter_type_class/album.rb', line 86

def cck_image_type
  CckForms::ParameterTypeClass::Image
end

#mongoizeObject

Converts input array of Neofiles::Image or IDs to array of hashes (denormalized image data) or IDs



8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/cck_forms/parameter_type_class/album.rb', line 8

def mongoize
  the_value = value.is_a?(Hash) ? value['value'] : value

  result = []
  if the_value.respond_to? :each
    the_value.each do |image|
      image = image[1] if the_value.respond_to? :each_value
      result.push self.class.neofiles_attrs_or_id(image, ::Neofiles::Image)
    end
  end

  result.compact
end

#to_diff_value(options = {}) ⇒ Object

Returns a collection of 64x64 IMGs



97
98
99
100
101
102
103
104
105
106
# File 'lib/cck_forms/parameter_type_class/album.rb', line 97

def to_diff_value(options = {})
  view_context = options[:view_context]

  images_html_list = (value || []).map(&:presence).compact.map do |elem|
    id = elem.is_a?(BSON::Document) ? elem['_id'] : elem
    "<img style='width: 64px; height: 64px;' src='#{view_context.neofiles_image_path(id: id, format: '64x64', crop: 1)}'>"
  end

  images_html_list.join.html_safe if images_html_list.any?
end

#to_s(options = nil) ⇒ Object

Returns empty string



92
93
94
# File 'lib/cck_forms/parameter_type_class/album.rb', line 92

def to_s(options = nil)
  ''
end