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.
45 46 47 48 49 50 51 52 53 54 55 |
# File 'app/models/content_provider_decorator.rb', line 45 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.
15 16 17 |
# File 'app/models/content_provider_decorator.rb', line 15 def content_policy @content_policy end |
#content_provider ⇒ Object (readonly)
Returns the value of attribute content_provider.
15 16 17 |
# File 'app/models/content_provider_decorator.rb', line 15 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.
36 37 38 39 40 41 42 43 |
# File 'app/models/content_provider_decorator.rb', line 36 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
17 18 19 20 21 22 23 24 25 26 27 |
# File 'app/models/content_provider_decorator.rb', line 17 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.
30 31 32 |
# File 'app/models/content_provider_decorator.rb', line 30 def self.model_name ActiveModel::Name.new(BitCore::ContentProvider) end |
Instance Method Details
#content_policy_attributes(attrs) ⇒ Object
116 117 118 |
# File 'app/models/content_provider_decorator.rb', line 116 def content_policy_attributes(attrs) attrs.slice(:is_skippable_after_first_viewing) end |
#content_provider_attributes(attrs) ⇒ Object
109 110 111 112 113 114 |
# File 'app/models/content_provider_decorator.rb', line 109 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
97 98 99 100 101 102 103 104 105 106 107 |
# File 'app/models/content_provider_decorator.rb', line 97 def destroy content_provider.transaction do content_policy.destroy! content_provider.destroy! end true rescue false end |
#errors ⇒ Object
67 68 69 70 |
# File 'app/models/content_provider_decorator.rb', line 67 def errors OpenStruct.new(full_messages: content_provider.errors. + content_policy.errors.) end |
#persisted? ⇒ Boolean
Play nicely with form_for.
63 64 65 |
# File 'app/models/content_provider_decorator.rb', line 63 def persisted? true end |
#pretty_title ⇒ Object
72 73 74 |
# File 'app/models/content_provider_decorator.rb', line 72 def pretty_title type.tableize.parameterize.titleize.singularize end |
#save ⇒ Object
76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'app/models/content_provider_decorator.rb', line 76 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.
58 59 60 |
# File 'app/models/content_provider_decorator.rb', line 58 def to_model content_provider end |
#update(attrs) ⇒ Object
89 90 91 92 93 94 95 |
# File 'app/models/content_provider_decorator.rb', line 89 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 |