-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'refresh' of https://github.com/unepwcmc/ProtectedPlanet …
…into refresh
- Loading branch information
Showing
16 changed files
with
213 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
class Comfy::Cms::LayoutCategory < ApplicationRecord | ||
self.table_name = 'comfy_cms_layout_categories' | ||
|
||
has_many :page_categories | ||
has_many :layouts_categories | ||
has_many :layouts, through: :layouts_categories | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
class Comfy::Cms::LayoutsCategory < ApplicationRecord | ||
self.table_name = 'comfy_cms_layouts_categories' | ||
|
||
belongs_to :layout | ||
belongs_to :layout_category | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
class Comfy::Cms::PageCategory < ApplicationRecord | ||
self.table_name = 'comfy_cms_page_categories' | ||
|
||
belongs_to :layout_category | ||
has_many :pages_categories | ||
has_many :pages, through: :pages_categories | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
class Comfy::Cms::PagesCategory < ApplicationRecord | ||
self.table_name = 'comfy_cms_pages_categories' | ||
|
||
belongs_to :page | ||
belongs_to :page_category | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
Rails.configuration.to_prepare do | ||
Comfy::Cms::Page.class_eval do | ||
has_many :pages_categories, foreign_key: 'page_id' | ||
has_many :page_categories, through: :pages_categories, foreign_key: 'page_id' | ||
|
||
has_many :topics, -> { joins(:layout_category).where("comfy_cms_layout_categories.label = 'topics'") }, | ||
through: :pages_categories, class_name: 'Comfy::Cms::PageCategory', source: :page_category | ||
|
||
has_many :page_types, -> { joins(:layout_category).where("comfy_cms_layout_categories.label = 'types'") }, | ||
through: :pages_categories, class_name: 'Comfy::Cms::PageCategory', source: :page_category | ||
|
||
accepts_nested_attributes_for :pages_categories | ||
end | ||
|
||
Comfy::Cms::Layout.class_eval do | ||
has_many :layouts_categories, foreign_key: 'layout_id' | ||
has_many :layout_categories, through: :layouts_categories, foreign_key: 'layout_id' | ||
|
||
accepts_nested_attributes_for :layouts_categories | ||
|
||
after_save :assign_layout_categories | ||
|
||
def assign_layout_categories | ||
_categories = self.content_tokens.select do |t| | ||
unless t.is_a?(Hash) | ||
false | ||
else | ||
t[:tag_class] == 'categories' | ||
end | ||
end | ||
|
||
delete_orphan_categories && return if _categories.blank? | ||
|
||
_categories.each do |cat| | ||
tag_name = cat[:tag_params].split(',').first | ||
_layout_category = Comfy::Cms::LayoutCategory.find_by(label: tag_name) | ||
Comfy::Cms::LayoutsCategory.find_or_create_by( | ||
layout_id: self.id, | ||
layout_category_id: _layout_category.id | ||
) | ||
end | ||
end | ||
|
||
def delete_orphan_categories | ||
layouts_categories = Comfy::Cms::LayoutsCategory.where(layout_id: self.id) | ||
|
||
self.pages.each do |page| | ||
layouts_categories.each do |lc| | ||
pcs_ids = page.page_categories.where(layout_category_id: lc.id).map(&:id) | ||
Comfy::Cms::PagesCategory.where( | ||
page_id: page.id, | ||
page_category_id: pcs_ids | ||
).destroy_all | ||
end | ||
end | ||
|
||
layout_categories.destroy_all | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
class Categories < ComfortableMexicanSofa::Content::Tag::Fragment | ||
def initialize(context:, params: [], source: nil) | ||
super | ||
@group_name = params.first | ||
@categories = Comfy::Cms::LayoutCategory.find_by(label: @group_name).page_categories | ||
end | ||
|
||
def form_field(object_name, view, index) | ||
input = view.collection_check_boxes(:page, :page_category_ids, @categories, :id, :label) do |b| | ||
view.content_tag(:div, class: "form-check form-check-inline") do | ||
view.concat b.check_box(class: "form-check-input") | ||
view.concat b.label(class: "form-check-label") | ||
end | ||
end | ||
|
||
yield input | ||
end | ||
end | ||
|
||
ComfortableMexicanSofa::Content::Renderer.register_tag( | ||
:categories, Categories | ||
) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
namespace :cms_categories do | ||
desc 'Seed database tables with custom categories for the CMS' | ||
task import: :environment do | ||
groups = I18n.t('search')[:custom_categories] | ||
|
||
groups.each do |group_name, categories| | ||
layout_category = Comfy::Cms::LayoutCategory.find_or_create_by(label: group_name.to_s) | ||
categories_names = categories[:items].keys | ||
|
||
categories_names.each do |cat_name| | ||
Comfy::Cms::PageCategory.find_or_create_by(layout_category_id: layout_category.id, label: cat_name) | ||
end | ||
end | ||
end | ||
end |