Class: Cms::ContentType

Inherits:
Object
  • Object
show all
Defined in:
app/models/cms/content_type.rb

Direct Known Subclasses

FormEntriesController::FauxContentType

Constant Summary collapse

DEFAULT_CONTENT_TYPE_NAME =
'Cms::HtmlBlock'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ ContentType

Returns a new instance of ContentType.



6
7
8
9
# File 'app/models/cms/content_type.rb', line 6

def initialize(options)
  self.name = options[:name]
  @path_builder = EngineAwarePathBuilder.new(model_class)
end

Instance Attribute Details

#nameObject

Returns the value of attribute name.



4
5
6
# File 'app/models/cms/content_type.rb', line 4

def name
  @name
end

#path_builderObject

Returns the value of attribute path_builder.



11
12
13
# File 'app/models/cms/content_type.rb', line 11

def path_builder
  @path_builder
end

Class Method Details

.addressableObject

Return content types that can be accessed as pages.



86
87
88
# File 'app/models/cms/content_type.rb', line 86

def addressable()
  available.select { |content_type| content_type.model_class.addressable? }
end

.availableArray<Cms::ContentType] An alphabetical list of content types.

Returns a list of all ContentTypes in the system. Content Types can opt out of this list by specifying:

class MyWidget < ActiveRecord::Base
  acts_as_content content_module: false
end

Ignores the database to just look at classes, then returns a ‘new’ ContentType to match.

Returns:

  • (Array<Cms::ContentType] An alphabetical list of content types.)

    Array<Cms::ContentType] An alphabetical list of content types.



51
52
53
54
55
56
57
58
59
60
61
62
# File 'app/models/cms/content_type.rb', line 51

def available
  subclasses = ObjectSpace.each_object(::Class).select do |klass|
    klass < Cms::Concerns::HasContentType::InstanceMethods
  end
  subclasses << Cms::Portlet
  subclasses.uniq! { |k| k.name } # filter duplicate classes
  subclasses.map do |klass|
    unless klass < Cms::Portlet
      Cms::ContentType.new(name: klass.name)
    end
  end.compact.sort { |a, b| a.name <=> b.name }
end

.available_by_moduleHash<Symbol, Cms::ContentType]

Return all content types, grouped by module.

Returns:



32
33
34
35
36
37
38
39
40
# File 'app/models/cms/content_type.rb', line 32

def available_by_module
  modules = {}
  available.each do |content_type|

    modules[content_type.module_name] = [] unless modules[content_type.module_name]
    modules[content_type.module_name] << content_type
  end
  modules
end

.connectableObject



25
26
27
# File 'app/models/cms/content_type.rb', line 25

def connectable
  available.select { |content_type| content_type.connectable? }
end

.create!Object



126
127
128
# File 'app/models/cms/content_type.rb', line 126

def self.create!
  ActiveSupport::Deprecation.warn "Cms::ContentType.create! should no longer be called. Content Types do not need to be registered in the database."
end

.defaultObject

Returns the default content type that is most frequently added to pages.



76
77
78
# File 'app/models/cms/content_type.rb', line 76

def default()
  Cms::ContentType.new(name: DEFAULT_CONTENT_TYPE_NAME)
end

.find_by_key(key) ⇒ Object

Given a ‘key’ like ‘html_blocks’ or ‘portlet’. Looks first for a class in the Cms

namespace, then again without it.

Raises exception if nothing was found.



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'app/models/cms/content_type.rb', line 94

def self.find_by_key(key)
  class_name = key.tableize.classify
  klass = nil
  prefix = "Cms::"
  if !class_name.starts_with? prefix
    klass = "Cms::#{class_name}".safe_constantize
  end
  unless klass
    klass = class_name.safe_constantize
  end
  unless klass
    if class_name.starts_with?(prefix)
      klass = class_name[prefix.length, class_name.length].safe_constantize
    end
  end
  unless klass
    raise "Couldn't find ContentType for '#{key}'. Checked for classes Cms::#{class_name} and #{class_name}."
  end
  klass.content_type
end

.listObject



64
65
66
# File 'app/models/cms/content_type.rb', line 64

def list
  available
end

.named(name) ⇒ Object



21
22
23
# File 'app/models/cms/content_type.rb', line 21

def named(name)
  [Cms::ContentType.new(name: name)]
end

.other_connectablesArray<Cms::ContentType]

Returns all content types besides the default.

Returns:



71
72
73
# File 'app/models/cms/content_type.rb', line 71

def other_connectables()
  available.select { |content_type| content_type.name != DEFAULT_CONTENT_TYPE_NAME }
end

.user_generated_connectablesObject

Returns only user generated Content Blocks



81
82
83
# File 'app/models/cms/content_type.rb', line 81

def user_generated_connectables()
  available.select { |content_type| !content_type.name.starts_with?("Cms::") }
end

Instance Method Details

#columns_for_indexObject

Allows models to show additional columns when being shown in a list.



170
171
172
173
174
175
176
177
178
179
# File 'app/models/cms/content_type.rb', line 170

def columns_for_index
  if model_class.respond_to?(:columns_for_index)
    model_class.columns_for_index.map do |column|
      column.respond_to?(:humanize) ? {:label => column.humanize, :method => column} : column
    end
  else
    [{:label => "Name", :method => :name, :order => "name"},
     {:label => "Updated On", :method => :updated_on_string, :order => "updated_at"}]
  end
end

#connectable?Boolean

Determines if the content can be connected to other pages.

Returns:

  • (Boolean)


159
160
161
# File 'app/models/cms/content_type.rb', line 159

def connectable?
  model_class.connectable?
end

#content_block_typeObject

Used in ERB for pathing



182
183
184
185
# File 'app/models/cms/content_type.rb', line 182

def content_block_type
  n = name.starts_with?("Cms::") ? name.demodulize : name
  n.pluralize.underscore
end

#content_block_type_for_listObject

This is used for situations where you want different to use a type for the list page This is true for portlets, where you don’t want to list all portlets of a given type, You want to list all portlets



190
191
192
193
194
195
196
# File 'app/models/cms/content_type.rb', line 190

def content_block_type_for_list
  if model_class.respond_to?(:content_block_type_for_list)
    model_class.content_block_type_for_list
  else
    content_block_type
  end
end

#display_nameObject



146
147
148
# File 'app/models/cms/content_type.rb', line 146

def display_name
  model_class.respond_to?(:display_name) ? model_class.display_name : Cms::Behaviors::Connecting.default_naming_for(model_class)
end

#display_name_pluralObject



150
151
152
# File 'app/models/cms/content_type.rb', line 150

def display_name_plural
  model_class.respond_to?(:display_name_plural) ? model_class.display_name_plural : display_name.pluralize
end

#formObject

Returns the partial used to render the form fields for a given block.



138
139
140
# File 'app/models/cms/content_type.rb', line 138

def form
  model_class.respond_to?(:form) ? model_class.form : "#{name.underscore.pluralize}/form"
end


142
143
144
# File 'app/models/cms/content_type.rb', line 142

def menu_name
  model_class.respond_to?(:menu_name) ? model_class.menu_name : display_name
end

#model_classObject



154
155
156
# File 'app/models/cms/content_type.rb', line 154

def model_class
  name.constantize
end

#module_nameSymbol

Return the name of the module this content type should be grouped in. In most cases, content blocks will be configured to specify this.

Returns:

  • (Symbol)


133
134
135
# File 'app/models/cms/content_type.rb', line 133

def module_name
  model_class.content_module
end

#orderable_attributesObject

Returns a list of column names and values for this content type which are allowed be orderable.



116
117
118
119
# File 'app/models/cms/content_type.rb', line 116

def orderable_attributes
  attribute_names = model_class.new.attribute_names
  attribute_names -= ["id", "version", "lock_version", "created_by_id", "updated_by_id"]
end

#param_keyObject

Cms::HtmlBlock -> html_block ThingBlock -> thing_block



165
166
167
# File 'app/models/cms/content_type.rb', line 165

def param_key
  model_class.model_name.param_key
end

#readonly?Boolean

Returns:

  • (Boolean)


16
17
18
# File 'app/models/cms/content_type.rb', line 16

def readonly?
  model_class.readonly?
end

#save!Object

Deprecated.


122
123
124
# File 'app/models/cms/content_type.rb', line 122

def save!
  ActiveSupport::Deprecation.warn "Cms::ContentType#save! should no longer be called. Content Types do not need to be registered in the database."
end