Class: Pageflow::FileType

Inherits:
Object
  • Object
show all
Defined in:
lib/pageflow/file_type.rb

Overview

Describes a type of file that can be managed in the editor and used in revisions.

Defined Under Namespace

Classes: NotFoundError

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ FileType

Create file type to be returned in PageType#file_types.

Examples:


Pageflow::FileType.new(model: 'Pageflow::Rainbow::File',
                       editor_partial: 'pageflow/rainbow/editor/files/file')

Parameters:

  • options (Hash)

Options Hash (options):

  • :model (ActiveRecord::Base, String)

    Required. Name of model representing the file. Model reference still works, but is deprecated

  • :partial (String)

    Optional. Path of a partial to include in json representations of the file both inside the editor and published entries.

  • :editor_partial (String)

    Optional. Path of a partial to include in json representations of the file inside the editor.

  • :collection_name (String)

    Optional. String to be used in routes. Defaults to ‘“pageflow_rainbow_file”` for model `Pageflow::Rainbow::File`.

  • :nested_file_types (Array<FileType>)

    Optional. Array of FileTypes allowed for nested files. Defaults to [].

  • :css_background_image_urls (#call)
  • :url_templates (#call)

    Optional. Callable returning a hash of url template strings indexed by their names.

  • :custom_attributes (Array<Symbol>)

    Optional. Array of strings containing attribute names that are custom to this file type



87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/pageflow/file_type.rb', line 87

def initialize(options)
  @model_string_or_reference = options.fetch(:model)
  @partial = options[:partial]
  @editor_partial = options[:editor_partial]
  @collection_name_or_blank = options[:collection_name]
  @nested_file_types = options.fetch(:nested_file_types, [])
  @top_level_type = options.fetch(:top_level_type, false)
  @css_background_image_urls = options[:css_background_image_urls]
  @css_background_image_class_prefix = options[:css_background_image_class_prefix]
  @url_templates = options.fetch(:url_templates, -> { {} })
  @custom_attributes = convert_custom_attributes_option(options[:custom_attributes])
end

Instance Attribute Details

#css_background_image_urls#call (readonly)

Callable that receives a file record and returns a hash of one of the following forms:

{
  poster: "url/of/image"
}

where ‘poster` is an arbitrary css class infix. Use `default` to skip the infix in the generated css class name. Or

{
  poster: {
    desktop: "desktop/url/of/image",
    mobile: "mobile/url/of/image"
  }
}

to provide different urls for the two media breakpoints.

Returns:

  • (#call)


47
48
49
# File 'lib/pageflow/file_type.rb', line 47

def css_background_image_urls
  @css_background_image_urls
end

#custom_attributesObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



55
56
57
# File 'lib/pageflow/file_type.rb', line 55

def custom_attributes
  @custom_attributes
end

#editor_partialString (readonly)

Path of a partial rendered in the json representation of the file inside the editor.

Returns:

  • (String)


16
17
18
# File 'lib/pageflow/file_type.rb', line 16

def editor_partial
  @editor_partial
end

#nested_file_typesArray<FileType> (readonly)

File types that are allowed to be nested inside the file

Returns:



20
21
22
# File 'lib/pageflow/file_type.rb', line 20

def nested_file_types
  @nested_file_types
end

#partialString (readonly)

Path of a partial rendered in the json representation of the file in both the editor and published entries

Returns:

  • (String)


11
12
13
# File 'lib/pageflow/file_type.rb', line 11

def partial
  @partial
end

#top_level_typeBoolean (readonly)

Is this file type used in situations where it is not nested into another file type?

Returns:

  • (Boolean)


25
26
27
# File 'lib/pageflow/file_type.rb', line 25

def top_level_type
  @top_level_type
end

#url_templates#call (readonly)

Callable that returns a hash of url template strings indexed by their names.

Returns:

  • (#call)


52
53
54
# File 'lib/pageflow/file_type.rb', line 52

def url_templates
  @url_templates
end

Instance Method Details

#collection_nameString

Underscored plural name for usage in routes.

Returns:

  • (String)


113
114
115
116
117
118
119
# File 'lib/pageflow/file_type.rb', line 113

def collection_name
  if @collection_name_or_blank.blank?
    model.model_name.plural
  else
    @collection_name_or_blank
  end
end

#css_background_image_class_prefixObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



122
123
124
# File 'lib/pageflow/file_type.rb', line 122

def css_background_image_class_prefix
  @css_background_image_class_prefix || model.model_name.singular
end

#css_background_image_urls_for(file, **options) ⇒ Object



126
127
128
129
130
131
132
# File 'lib/pageflow/file_type.rb', line 126

def css_background_image_urls_for(file, **options)
  if call_arity(css_background_image_urls) == 1
    css_background_image_urls.call(file)
  else
    css_background_image_urls.call(file, **options)
  end
end

#i18n_keyObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



148
149
150
# File 'lib/pageflow/file_type.rb', line 148

def i18n_key
  model.model_name.i18n_key
end

#modelActiveRecord::Model

ActiveRecord model that represents the files of this type.

Returns:

  • (ActiveRecord::Model)


102
103
104
105
106
107
108
109
# File 'lib/pageflow/file_type.rb', line 102

def model
  @model ||=
    if @model_string_or_reference.is_a?(String)
      @model_string_or_reference.constantize
    else
      @model_string_or_reference
    end
end

#param_keyObject Also known as: short_name

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



135
136
137
# File 'lib/pageflow/file_type.rb', line 135

def param_key
  model.model_name.param_key.to_sym
end

#type_nameObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



143
144
145
# File 'lib/pageflow/file_type.rb', line 143

def type_name
  model.name
end