Class: Cms::ContentType
- Inherits:
-
Object
- Object
- Cms::ContentType
- Defined in:
- app/models/cms/content_type.rb
Direct Known Subclasses
Constant Summary collapse
- DEFAULT_CONTENT_TYPE_NAME =
'Cms::HtmlBlock'
Instance Attribute Summary collapse
-
#name ⇒ Object
Returns the value of attribute name.
-
#path_builder ⇒ Object
Returns the value of attribute path_builder.
Class Method Summary collapse
-
.addressable ⇒ Object
Return content types that can be accessed as pages.
-
.available ⇒ Array<Cms::ContentType] An alphabetical list of content types.
Returns a list of all ContentTypes in the system.
-
.available_by_module ⇒ Hash<Symbol, Cms::ContentType]
Return all content types, grouped by module.
- .connectable ⇒ Object
- .create! ⇒ Object
-
.default ⇒ Object
Returns the default content type that is most frequently added to pages.
-
.find_by_key(key) ⇒ Object
Given a 'key' like 'html_blocks' or 'portlet'.
- .list ⇒ Object
- .named(name) ⇒ Object
-
.other_connectables ⇒ Array<Cms::ContentType]
Returns all content types besides the default.
-
.user_generated_connectables ⇒ Object
Returns only user generated Content Blocks.
Instance Method Summary collapse
-
#columns_for_index ⇒ Object
Allows models to show additional columns when being shown in a list.
-
#connectable? ⇒ Boolean
Determines if the content can be connected to other pages.
-
#content_block_type ⇒ Object
Used in ERB for pathing.
-
#content_block_type_for_list ⇒ Object
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.
- #display_name ⇒ Object
- #display_name_plural ⇒ Object
-
#form ⇒ Object
Returns the partial used to render the form fields for a given block.
-
#initialize(options) ⇒ ContentType
constructor
A new instance of ContentType.
- #menu_name ⇒ Object
- #model_class ⇒ Object
-
#module_name ⇒ Symbol
Return the name of the module this content type should be grouped in.
-
#orderable_attributes ⇒ Object
Returns a list of column names and values for this content type which are allowed be orderable.
-
#param_key ⇒ Object
Cms::HtmlBlock -> html_block ThingBlock -> thing_block.
- #readonly? ⇒ Boolean
- #save! ⇒ Object deprecated Deprecated.
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() self.name = [:name] @path_builder = EngineAwarePathBuilder.new(model_class) end |
Instance Attribute Details
#name ⇒ Object
Returns the value of attribute name.
4 5 6 |
# File 'app/models/cms/content_type.rb', line 4 def name @name end |
#path_builder ⇒ Object
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
.addressable ⇒ Object
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 |
.available ⇒ Array<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.
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_module ⇒ Hash<Symbol, Cms::ContentType]
Return all content types, grouped by module.
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 |
.connectable ⇒ Object
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 |
.default ⇒ Object
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 |
.list ⇒ Object
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_connectables ⇒ Array<Cms::ContentType]
Returns all content types besides the default.
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_connectables ⇒ Object
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_index ⇒ Object
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.
159 160 161 |
# File 'app/models/cms/content_type.rb', line 159 def connectable? model_class.connectable? end |
#content_block_type ⇒ Object
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_list ⇒ Object
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_name ⇒ Object
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_plural ⇒ Object
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 |
#form ⇒ Object
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 |
#menu_name ⇒ Object
142 143 144 |
# File 'app/models/cms/content_type.rb', line 142 def model_class.respond_to?(:menu_name) ? model_class. : display_name end |
#model_class ⇒ Object
154 155 156 |
# File 'app/models/cms/content_type.rb', line 154 def model_class name.constantize end |
#module_name ⇒ Symbol
Return the name of the module this content type should be grouped in. In most cases, content blocks will be configured to specify this.
133 134 135 |
# File 'app/models/cms/content_type.rb', line 133 def module_name model_class.content_module end |
#orderable_attributes ⇒ Object
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_key ⇒ Object
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
16 17 18 |
# File 'app/models/cms/content_type.rb', line 16 def readonly? model_class.readonly? end |
#save! ⇒ Object
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 |