Skip to content

Commit

Permalink
Merge pull request #1 from comfy/master
Browse files Browse the repository at this point in the history
Update to current CMS head
  • Loading branch information
kbroderick authored Jun 13, 2019
2 parents b7bdfd3 + 38a3142 commit c267045
Show file tree
Hide file tree
Showing 57 changed files with 435 additions and 178 deletions.
7 changes: 4 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
language: ruby
rvm:
- 2.3.7
- 2.4.4
- 2.5.1
- 2.3.8
- 2.4.5
- 2.5.5
- 2.6.2
- ruby-head
gemfile:
- test/gemfiles/5.2.gemfile
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ If you want to add a Blog functionality to your app take a look at

CMS for Rails 5.1 doesn't have published gem, but you may use [rails 5.1 branch](https://github.com/comfy/comfortable-mexican-sofa/tree/rails5.1) directly.

If you want to use CMS version 1.12 on Rails 5.2 use [1.13 branch](https://github.com/comfy/comfortable-mexican-sofa/tree/1.13) directly.

With Rails 4.2 and 5.0 use gem version [1.12.10](https://rubygems.org/gems/comfortable_mexican_sofa/versions/1.12.10)

With Rails 3.0 use gem version [1.8.5](https://rubygems.org/gems/comfortable_mexican_sofa/versions/1.8.5)
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/comfy/admin/cms/categories_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def create
end

def update
@category.update_attributes!(category_params)
@category.update!(category_params)
rescue ActiveRecord::RecordInvalid
head :ok
end
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/comfy/admin/cms/layouts_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def create
end

def update
@layout.update_attributes!(layout_params)
@layout.update!(layout_params)
flash[:success] = I18n.t("comfy.admin.cms.layouts.updated")
redirect_to action: :edit, id: @layout
rescue ActiveRecord::RecordInvalid
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/comfy/admin/cms/sites_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def create
end

def update
@site.update_attributes!(site_params)
@site.update!(site_params)
flash[:success] = I18n.t("comfy.admin.cms.sites.updated")
redirect_to action: :edit, id: @site
rescue ActiveRecord::RecordInvalid
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/comfy/admin/cms/snippets_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def create
end

def update
@snippet.update_attributes!(snippet_params)
@snippet.update!(snippet_params)
flash[:success] = I18n.t("comfy.admin.cms.snippets.updated")
redirect_to action: :edit, id: @snippet
rescue ActiveRecord::RecordInvalid
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/comfy/admin/cms/translations_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def create
end

def update
@translation.update_attributes!(translation_params)
@translation.update!(translation_params)
flash[:success] = I18n.t("comfy.admin.cms.translations.updated")
redirect_to action: :edit, id: @translation
rescue ActiveRecord::RecordInvalid
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/comfy/cms/assets_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

class Comfy::Cms::AssetsController < Comfy::Cms::BaseController

skip_before_action :verify_authenticity_token
skip_before_action :verify_authenticity_token, raise: false

before_action :load_cms_layout,
:use_null_session
Expand Down
17 changes: 6 additions & 11 deletions app/controllers/comfy/cms/content_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ def show
respond_to do |format|
format.html { render_page }
format.json do
json_page = @cms_page.as_json(except: [:content_cache])
json_page[:content] = render_to_string(
@cms_page.content = render_to_string(
inline: @cms_page.content_cache,
layout: false
)
json_page = @cms_page.as_json(ComfortableMexicanSofa.config.page_to_json_options)
render json: json_page
end
end
Expand All @@ -44,7 +44,7 @@ def render_page(status = :ok)
# it's possible to control mimetype of a page by creating a `mime_type` field
def mime_type
mime_block = @cms_page.fragments.detect { |f| f.identifier == "mime_type" }
mime_block&.content || "text/html"
mime_block&.content&.strip || "text/html"
end

def app_layout
Expand Down Expand Up @@ -73,15 +73,10 @@ def load_cms_page
# Getting page and setting content_cache and fragments data if we need to
# serve translation data
def find_cms_page_by_full_path(full_path)
@cms_page = @cms_site.pages.published.find_by!(full_path: full_path)
@cms_layout = @cms_page.layout
@cms_page = @cms_site.pages.published.find_by!(full_path: full_path)

# There are translations for this page and locale is not the default site
# locale, so we need to grab translation data.
if @cms_page.translations.any? && @cms_site.locale != I18n.locale.to_s
@cms_page.translate!(I18n.locale)
@cms_layout = @cms_page.layout
end
@cms_page.translate!
@cms_layout = @cms_page.layout

@cms_page

Expand Down
15 changes: 8 additions & 7 deletions app/models/comfy/cms/file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,12 @@ class Comfy::Cms::File < ActiveRecord::Base
belongs_to :site

# -- Callbacks ---------------------------------------------------------------
before_validation :assign_label, on: :create
before_create :assign_position
after_save :process_attachment

# -- Validations -------------------------------------------------------------
validates :label, presence: true
validates :file, presence: true, on: :create

# -- Scopes ------------------------------------------------------------------
Expand All @@ -34,20 +36,19 @@ class Comfy::Cms::File < ActiveRecord::Base
where("active_storage_blobs.content_type LIKE 'image/%'").references(:blob)
}

# -- Instance Methods --------------------------------------------------------
def label
l = read_attribute(:label)
return l if l.present?
attachment.attached? ? attachment.filename.to_s : nil
end

protected

def assign_position
max = Comfy::Cms::File.maximum(:position)
self.position = max ? max + 1 : 0
end

# TODO: Change db schema not to set blank string
def assign_label
return if label.present?
self.label = file&.original_filename
end

def process_attachment
return if @file.blank?
attachment.attach(@file)
Expand Down
24 changes: 16 additions & 8 deletions app/models/comfy/cms/layout.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,24 @@ class Comfy::Cms::Layout < ActiveRecord::Base

# -- Class Methods -----------------------------------------------------------
# Tree-like structure for layouts
def self.options_for_select(site, layout = nil, current_layout = nil, depth = 0, spacer = ". . ")
out = []
[current_layout || site.layouts.roots.order(:position)].flatten.each do |l|
next if layout == l
out << ["#{spacer * depth}#{l.label}", l.id]
l.children.order(:position).each do |child|
out += options_for_select(site, layout, child, depth + 1, spacer)
def self.options_for_select(site, current_layout = nil)
options = []

options_for_layout = ->(layout, depth = 0) do
return if layout == current_layout

options << ["#{'. . ' * depth}#{layout.label}", layout.id]

layout.children.order(:position).each do |child_layout|
options_for_layout.call(child_layout, depth + 1)
end
end
out.compact

site.layouts.roots.order(:position).each do |layout|
options_for_layout.call(layout)
end

options
end

# List of available application layouts
Expand Down
41 changes: 21 additions & 20 deletions app/models/comfy/cms/page.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ class Comfy::Cms::Page < ActiveRecord::Base
include Comfy::Cms::WithFragments
include Comfy::Cms::WithCategories

cms_acts_as_tree counter_cache: :children_count
cms_acts_as_tree counter_cache: :children_count, order: :position
cms_has_revisions_for :fragments_attributes

attr_accessor :content

# -- Relationships -----------------------------------------------------------
belongs_to :site
belongs_to :target_page,
Expand Down Expand Up @@ -45,28 +47,22 @@ class Comfy::Cms::Page < ActiveRecord::Base

# -- Class Methods -----------------------------------------------------------
# Tree-like structure for pages
def self.options_for_select(site:, page: nil, current_page: nil, depth: 0, exclude_self: true, spacer: ". . ")
return [] if (current_page ||= site.pages.root) == page && exclude_self || !current_page
out = []
def self.options_for_select(site:, current_page: nil, exclude_self: false)
options = []

unless current_page == page
out << ["#{spacer * depth}#{current_page.label}", current_page.id]
end
options_for_page = ->(page, depth = 0) do
return if exclude_self && page == current_page

options << ["#{'. . ' * depth}#{page.label}", page.id]

if current_page.children_count.nonzero?
current_page.children.each do |child|
out += options_for_select(
site: site,
page: page,
current_page: child,
depth: depth + 1,
exclude_self: exclude_self,
spacer: spacer
)
page.children.order(:position).each do |child_page|
options_for_page.call(child_page, depth + 1)
end
end

out.compact
options_for_page.call(site.pages.root)

options
end

# -- Instance Methods --------------------------------------------------------
Expand All @@ -88,8 +84,13 @@ def url(relative: false)

# This method will mutate page object by transfering attributes from translation
# for a given locale.
def translate!(locale)
translation = translations.published.find_by!(locale: locale)
def translate!
# If site locale same as page's or there's no translastions, we do nothing
if site.locale == I18n.locale.to_s || translations.blank?
return
end

translation = translations.published.find_by!(locale: I18n.locale)
self.layout = translation.layout
self.label = translation.label
self.content_cache = translation.content_cache
Expand Down
2 changes: 1 addition & 1 deletion app/views/comfy/admin/cms/files/_file.html.haml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
%li{data: {id: file.id}}
:ruby
file_tag = cms_file_link_tag(file)
thumb_url = url_for(file.attachment.variant(combine_options: Comfy::Cms::File::VARIANT_SIZE[:thumb])) if file.attachment.image?
thumb_url = url_for(file.attachment.variant(combine_options: Comfy::Cms::File::VARIANT_SIZE[:thumb])) if file.attachment.variable?
.row
.col-md-5.item
.item-controls.d-none.d-lg-block
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
.fragment-attachments
- attachments.each do |attachment|
:ruby
thumb_url = if attachment.image?
thumb_url = if attachment.variable?
url_for(attachment.variant(combine_options: Comfy::Cms::File::VARIANT_SIZE[:thumb]))
end
filename = attachment.filename.to_s
Expand Down
4 changes: 2 additions & 2 deletions app/views/comfy/admin/cms/pages/_form.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
- if @page.parent.present?
= form.text_field :slug, data: { slug: true }, prepend: @page.parent.full_path

- if (options = ::Comfy::Cms::Page.options_for_select(site: @site, page: @page, exclude_self: true)).present?
- if (options = ::Comfy::Cms::Page.options_for_select(site: @site, current_page: @page, exclude_self: true)).present?
= form.select :parent_id, options, bootstrap: {custom_control: true}

- if (options = ::Comfy::Cms::Layout.options_for_select(@site)).present?
Expand All @@ -18,7 +18,7 @@

= render "comfy/admin/cms/categories/form", form: form

- if (options = ::Comfy::Cms::Page.options_for_select(site: @site, page: @page)).present?
- if (options = ::Comfy::Cms::Page.options_for_select(site: @site, current_page: @page)).present?
= form.select :target_page_id, [["---- #{t('.select_target_page')} ----", nil]] + options, bootstrap: {custom_control: true}

= form.check_box :is_published
Expand Down
2 changes: 1 addition & 1 deletion app/views/kaminari/comfy/_first_page.html.haml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
- unless current_page.first?
%li.page-item
= link_to_unless current_page.first?, raw(t 'views.pagination.first'), url, class: 'page-link', remote: remote
= link_to_unless current_page.first?, raw(t 'views.pagination.first'), url, class: 'page-link', remote: remote
2 changes: 1 addition & 1 deletion comfortable_mexican_sofa.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,5 @@ Gem::Specification.new do |s|
s.add_dependency "mini_magick", ">= 4.8.0"
s.add_dependency "rails", ">= 5.2.0"
s.add_dependency "rails-i18n", ">= 5.0.0"
s.add_dependency "sass-rails", ">= 5.0.0"
s.add_dependency "sassc-rails", ">= 2.0.0"
end
4 changes: 4 additions & 0 deletions config/environments/test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,8 @@
config.active_job.queue_adapter = :inline

config.action_view.raise_on_missing_translations = true

# TODO: This is temporary fix so TravisCI runs until Rails 5.2.3 is out.
# See: https://github.com/rails/rails/pull/35607
config.secret_key_base = SecureRandom.hex(64)
end
6 changes: 6 additions & 0 deletions config/initializers/comfortable_mexican_sofa.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,12 @@
# Reveal partials that can be overwritten in the admin area.
# Default is false.
# config.reveal_cms_partials = false
#
# Customize the returned content json data
# include fragments in content json
# config.content_json_options = {
# include: [:fragments]
# }
end

# Default credentials for ComfortableMexicanSofa::AccessControl::AdminAuthentication
Expand Down
Loading

0 comments on commit c267045

Please sign in to comment.