Class: Maglev::Section::Store
- Inherits:
-
Object
- Object
- Maglev::Section::Store
- Extended by:
- Forwardable
- Defined in:
- app/models/maglev/section.rb
Instance Attribute Summary collapse
-
#array ⇒ Object
readonly
Returns the value of attribute array.
Instance Method Summary collapse
- #as_json(**_options) ⇒ Object
- #available_for(sections_content) ⇒ Object
- #filter(sections_content, keyword: nil, category_id: nil) ⇒ Object
- #find(type) ⇒ Object
- #find_all_by_type(type) ⇒ Object
- #grouped_by_category ⇒ Object
-
#initialize(array) ⇒ Store
constructor
A new instance of Store.
Constructor Details
#initialize(array) ⇒ Store
Returns a new instance of Store.
119 120 121 |
# File 'app/models/maglev/section.rb', line 119 def initialize(array) @array = array end |
Instance Attribute Details
#array ⇒ Object (readonly)
Returns the value of attribute array.
117 118 119 |
# File 'app/models/maglev/section.rb', line 117 def array @array end |
Instance Method Details
#as_json(**_options) ⇒ Object
157 158 159 |
# File 'app/models/maglev/section.rb', line 157 def as_json(**) @array.as_json end |
#available_for(sections_content) ⇒ Object
135 136 137 138 139 140 141 142 143 |
# File 'app/models/maglev/section.rb', line 135 def available_for(sections_content) # we don't want to add site_scoped sections or singleton sections that are already present on the page new_array = @array.reject do |section| (section.site_scoped? || section.singleton?) && sections_content.any? do |section_content| section_content.type == section.id end end self.class.new(new_array) end |
#filter(sections_content, keyword: nil, category_id: nil) ⇒ Object
145 146 147 148 149 150 151 152 153 154 155 |
# File 'app/models/maglev/section.rb', line 145 def filter(sections_content, keyword: nil, category_id: nil) new_array = if keyword.present? @array.select { |section| section.name.downcase.include?(keyword.downcase) } elsif category_id.present? @array.select { |section| section.category == category_id } else @array end self.class.new(new_array).available_for(sections_content) end |
#find(type) ⇒ Object
123 124 125 |
# File 'app/models/maglev/section.rb', line 123 def find(type) @array.find { |section| section.id == type } end |
#find_all_by_type(type) ⇒ Object
127 128 129 |
# File 'app/models/maglev/section.rb', line 127 def find_all_by_type(type) @array.select { |section| section.id == type } end |
#grouped_by_category ⇒ Object
131 132 133 |
# File 'app/models/maglev/section.rb', line 131 def grouped_by_category @array.group_by(&:category) end |