Skip to content

Commit

Permalink
Merge ac7d9e8 into e00d681
Browse files Browse the repository at this point in the history
  • Loading branch information
unRARed committed Sep 24, 2019
2 parents e00d681 + ac7d9e8 commit 4bf913e
Show file tree
Hide file tree
Showing 9 changed files with 99 additions and 15 deletions.
4 changes: 4 additions & 0 deletions app/models/comfy/blog/post.rb
Expand Up @@ -13,6 +13,10 @@ class Comfy::Blog::Post < ActiveRecord::Base
belongs_to :site,
class_name: "Comfy::Cms::Site"

belongs_to :file,
class_name: "Comfy::Cms::File",
optional: true

# -- Validations -------------------------------------------------------------
validates :title, :slug, :year, :month,
presence: true
Expand Down
19 changes: 17 additions & 2 deletions app/views/comfy/admin/blog/posts/_form.html.haml
Expand Up @@ -2,9 +2,24 @@

= form.text_field :title, data: {slugify: @post.new_record?}
= form.text_field :slug, data: {slug: true}

= form.select :file_id, |
options_for_select(@site.files.order(:label).map{|f| [f.label, f.id] }, |
@post.file&.id), |
include_blank: " - #{'DE' if @post.file.present?}SELECT FILE - "
- if @post.file.present?
- if @post.file.attachment.attached?
- attachment = @post.file.attachment
= form.form_group bootstrap: {label: {text: 'File Preview'}} do
.file-attachment
= link_to url_for(attachment) do
- if attachment.variable?
= image_tag attachment.variant(resize: "320x240").processed
- else
= attachment.filename.to_s
- if (options = ::Comfy::Cms::Layout.options_for_select(@site)).present?
= form.select :layout_id, options, {}, {data: {url: form_fragments_comfy_admin_blog_post_path(@site, @post.id.to_i)}, id: "fragments-toggle"}
= form.select :layout_id, options, {}, {data: |
{url: form_fragments_comfy_admin_blog_post_path(@site, @post.id.to_i)}, |
id: "fragments-toggle"}

#form-fragments-container
= render "comfy/admin/cms/fragments/form_fragments", record: @post, scope: :post
Expand Down
15 changes: 10 additions & 5 deletions app/views/comfy/blog/posts/index.html.haml
Expand Up @@ -4,11 +4,16 @@
%h1 Blog Posts

- @blog_posts.each do |post|

%h2
= link_to post.title, comfy_blog_post_path(@cms_site.path, post.year, post.month, post.slug)
.date
= post.published_at.to_s(:db)
.row
- image_attached = post.file.present? && post.file.attachment.attached? && post.file.attachment.content_type.include?("image")
%div{class: "col-md-#{image_attached ? '9' : '12'}"}
%h2
= link_to post.title, comfy_blog_post_path(@cms_site.path, post.year, post.month, post.slug)
.date
= post.published_at.to_s(:db)
- if image_attached && post.file.attachment.variable?
.col-md-3
= image_tag post.file.attachment.variant(resize: "120x120").processed

= comfy_paginate @blog_posts

Expand Down
7 changes: 7 additions & 0 deletions app/views/comfy/blog/posts/show.html.haml
@@ -1,5 +1,12 @@
%h1= @cms_post.title

- if @cms_post.file.present? && @cms_post.file.attachment.attached?
- attachment = @cms_post.file.attachment
= link_to url_for(attachment) do
- if attachment.content_type.include? "image"
= image_tag attachment.variant(resize: "640x480").processed
- else
= attachment.filename.to_s
%p
- label = Comfy::Blog::Post.human_attribute_name(:published_on)
- time = @cms_post.published_at.to_formatted_s(:short)
Expand Down
9 changes: 9 additions & 0 deletions db/migrate/02_add_file_reference_to_blog_posts.rb
@@ -0,0 +1,9 @@
class AddFileReferenceToBlogPosts < ActiveRecord::Migration[5.2]
def change
add_reference :comfy_blog_posts,
:file,
index: true,
foreign_key: { to_table: :comfy_cms_files },
null: true
end
end
29 changes: 21 additions & 8 deletions lib/generators/comfy/blog/blog_generator.rb
Expand Up @@ -16,14 +16,27 @@ def self.next_migration_number(dirname)
end

def generate_migration
destination = File.expand_path("db/migrate/01_create_blog.rb", destination_root)
migration_dir = File.dirname(destination)
destination = self.class.migration_exists?(migration_dir, "create_blog")

if destination
puts "\e[0m\e[31mFound existing create_blog migration. Remove it if you want to regenerate.\e[0m"
else
migration_template "db/migrate/01_create_blog.rb", "db/migrate/create_blog.rb"
migrations = %w[
01_create_blog
02_add_file_reference_to_blog_posts
]
version_regex = %r{\d.*?_}

migrations.each do |identifier|
destination = File.expand_path(
"db/migrate/#{identifier}.rb", destination_root
)
migration_dir = File.dirname(destination)
destination = self.class.migration_exists?(
migration_dir, identifier.gsub(version_regex, "")
)

if destination
puts "\e[0m\e[31mFound existing #{identifier.gsub(version_regex, '')} migration. Remove to regenerate.\e[0m"
else
migration_template "db/migrate/#{identifier}.rb",
"db/migrate/#{identifier.gsub(version_regex, '')}.rb"
end
end
end

Expand Down
28 changes: 28 additions & 0 deletions test/controllers/comfy/admin/blog/posts_controller_test.rb
Expand Up @@ -7,6 +7,7 @@ class Comfy::Admin::Blog::PostsControllerTest < ActionDispatch::IntegrationTest
setup do
@site = comfy_cms_sites(:default)
@layout = comfy_cms_layouts(:default)
@file = comfy_cms_files(:default)
@post = comfy_blog_posts(:default)
end

Expand Down Expand Up @@ -74,6 +75,33 @@ def test_creation
end
end

def test_creation_with_file
post_count = -> { Comfy::Blog::Post.count }
frag_count = -> { Comfy::Cms::Fragment.count }
@file.attachment = fixture_file_upload(
File.open(Rails.root.join("test", "support", "test-image.png")),
"image/png"
)
@file.save!
assert_difference [post_count, frag_count] do
r :post, comfy_admin_blog_posts_path(@site), params: { post: {
title: "Test Post",
slug: "test-post",
published_at: 2.days.ago.to_s(:db),
is_published: "1",
file_id: @file,
layout_id: @layout,
fragments_attributes: [
{ identifier: "content",
content: "this post references an uploaded file" }
]
} }
assert_response :redirect
assert_redirected_to action: :edit, id: assigns(:post)
assert_equal "Blog Post created", flash[:success]
end
end

def test_creation_failure
assert_no_difference -> { Comfy::Blog::Post.count } do
r :post, comfy_admin_blog_posts_path(@site), params: { post: {} }
Expand Down
3 changes: 3 additions & 0 deletions test/fixtures/comfy/cms/files.yml
@@ -0,0 +1,3 @@
default:
site: default
label: Default File
Binary file added test/support/test-image.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 4bf913e

Please sign in to comment.