Class: ContentProviderDecorator
- Inherits:
-
Object
- Object
- ContentProviderDecorator
- Includes:
- ActiveModel::Conversion
- Defined in:
- app/models/content_provider_decorator.rb
Overview
Allows for extending BitCore::ContentProvider with an additional model class and corresponding attributes.
Instance Attribute Summary collapse
-
#content_policy ⇒ Object
readonly
Returns the value of attribute content_policy.
-
#content_provider ⇒ Object
readonly
Returns the value of attribute content_provider.
Class Method Summary collapse
-
.content_provider_classes ⇒ Object
Eagerly load BitCore::ContentProvider descendants, as would normally be required in development and test environments.
- .fetch(provider_or_id) ⇒ Object
-
.model_name ⇒ Object
Play nicely with form_for.
Instance Method Summary collapse
- #content_policy_attributes(attrs) ⇒ Object
- #content_provider_attributes(attrs) ⇒ Object
- #destroy ⇒ Object
- #errors ⇒ Object
-
#initialize(attrs = {}) ⇒ ContentProviderDecorator
constructor
A new instance of ContentProviderDecorator.
-
#persisted? ⇒ Boolean
Play nicely with form_for.
- #pretty_title ⇒ Object
- #save ⇒ Object
-
#to_model ⇒ Object
Play nicely with form_for.
- #update(attrs) ⇒ Object
Constructor Details
#initialize(attrs = {}) ⇒ ContentProviderDecorator
Returns a new instance of ContentProviderDecorator.
46 47 48 49 50 51 52 53 54 55 56 |
# File 'app/models/content_provider_decorator.rb', line 46 def initialize(attrs = {}) if attrs[:content_provider] && attrs[:content_policy] @content_provider = attrs[:content_provider] @content_policy = attrs[:content_policy] else provider_attrs = content_provider_attributes(attrs) @content_provider = BitCore::ContentProvider.new(provider_attrs) policy_attrs = content_policy_attributes(attrs) @content_policy = ContentProviderPolicy.new(policy_attrs) end end |
Instance Attribute Details
#content_policy ⇒ Object (readonly)
Returns the value of attribute content_policy.
16 17 18 |
# File 'app/models/content_provider_decorator.rb', line 16 def content_policy @content_policy end |
#content_provider ⇒ Object (readonly)
Returns the value of attribute content_provider.
16 17 18 |
# File 'app/models/content_provider_decorator.rb', line 16 def content_provider @content_provider end |
Class Method Details
.content_provider_classes ⇒ Object
Eagerly load BitCore::ContentProvider descendants, as would normally be required in development and test environments.
37 38 39 40 41 42 43 44 |
# File 'app/models/content_provider_decorator.rb', line 37 def self.content_provider_classes BitCore::ContentProviders::SlideshowProvider.to_s Dir["#{Rails.root}/app/models/content_providers/*.rb"].each do |file| require_dependency file end BitCore::ContentProvider.descendants end |
.fetch(provider_or_id) ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 |
# File 'app/models/content_provider_decorator.rb', line 18 def self.fetch(provider_or_id) provider = provider_or_id if provider.try(:to_i).class == Fixnum provider = BitCore::ContentProvider.find(provider.to_i) end policy = ContentProviderPolicy.find_or_initialize_by( bit_core_content_provider_id: provider.id ) new(content_provider: provider, content_policy: policy) end |
.model_name ⇒ Object
Play nicely with form_for.
31 32 33 |
# File 'app/models/content_provider_decorator.rb', line 31 def self.model_name ActiveModel::Name.new(BitCore::ContentProvider) end |
Instance Method Details
#content_policy_attributes(attrs) ⇒ Object
117 118 119 |
# File 'app/models/content_provider_decorator.rb', line 117 def content_policy_attributes(attrs) attrs.slice(:is_skippable_after_first_viewing) end |
#content_provider_attributes(attrs) ⇒ Object
110 111 112 113 114 115 |
# File 'app/models/content_provider_decorator.rb', line 110 def content_provider_attributes(attrs) attrs.slice( :bit_core_content_module_id, :type, :source_content_type, :source_content_id, :position, :show_next_nav, :template_path ) end |
#destroy ⇒ Object
98 99 100 101 102 103 104 105 106 107 108 |
# File 'app/models/content_provider_decorator.rb', line 98 def destroy content_provider.transaction do content_policy.destroy! content_provider.destroy! end true rescue false end |
#errors ⇒ Object
68 69 70 71 |
# File 'app/models/content_provider_decorator.rb', line 68 def errors OpenStruct.new(full_messages: content_provider.errors. + content_policy.errors.) end |
#persisted? ⇒ Boolean
Play nicely with form_for.
64 65 66 |
# File 'app/models/content_provider_decorator.rb', line 64 def persisted? true end |
#pretty_title ⇒ Object
73 74 75 |
# File 'app/models/content_provider_decorator.rb', line 73 def pretty_title type.tableize.parameterize.titleize.singularize end |
#save ⇒ Object
77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'app/models/content_provider_decorator.rb', line 77 def save content_provider.transaction do content_provider.save! content_policy.bit_core_content_provider_id = content_provider.id content_policy.save! end true rescue false end |
#to_model ⇒ Object
Play nicely with form_for.
59 60 61 |
# File 'app/models/content_provider_decorator.rb', line 59 def to_model content_provider end |
#update(attrs) ⇒ Object
90 91 92 93 94 95 96 |
# File 'app/models/content_provider_decorator.rb', line 90 def update(attrs) provider_attrs = content_provider_attributes(attrs) policy_attrs = content_policy_attributes(attrs) content_provider.update(provider_attrs) && content_policy.update!(policy_attrs) end |