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.



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

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.



47
48
49
50
51
52
53
54
55
56
57
58
# File 'app/models/cms/content_type.rb', line 47

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:



28
29
30
31
32
33
34
35
36
# File 'app/models/cms/content_type.rb', line 28

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



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

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

.create!Object



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

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.



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

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.



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'app/models/cms/content_type.rb', line 90

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



60
61
62
# File 'app/models/cms/content_type.rb', line 60

def list
  available
end

.named(name) ⇒ Object



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

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

.other_connectablesArray<Cms::ContentType]

Returns all content types besides the default.

Returns:



67
68
69
# File 'app/models/cms/content_type.rb', line 67

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

.user_generated_connectablesObject

Returns only user generated Content Blocks



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

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.



162
163
164
165
166
167
168
169
170
171
# File 'app/models/cms/content_type.rb', line 162

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)


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

def connectable?
  model_class.connectable?
end

#content_block_typeObject

Used in ERB for pathing



174
175
176
177
# File 'app/models/cms/content_type.rb', line 174

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



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

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



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

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



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

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.



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

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

#model_classObject



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

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)


129
130
131
# File 'app/models/cms/content_type.rb', line 129

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.



112
113
114
115
# File 'app/models/cms/content_type.rb', line 112

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



157
158
159
# File 'app/models/cms/content_type.rb', line 157

def param_key
  model_class.model_name.param_key
end

#save!Object

Deprecated.


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

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