Permalink
Browse files

Add articles, port shared things from notes to posts

  • Loading branch information...
adamdawkins committed Jan 18, 2019
1 parent 8508055 commit 20bba8c458c1e6408bdbefff139093a3c944bf60
@@ -8,6 +8,7 @@ gem 'bootsnap', '>= 1.1.0', require: false
gem 'data_migrate'
gem 'friendly_id', '~> 5.2.4'
gem 'jbuilder', '~> 2.5'
gem 'kramdown', '~> 1.17'
gem 'pg', '>= 0.18', '< 2.0'
gem 'puma', '~> 3.11'
gem 'twitter'
@@ -129,6 +129,7 @@ GEM
jbuilder (2.8.0)
activesupport (>= 4.2.0)
multi_json (>= 1.2)
kramdown (1.17.0)
listen (3.1.5)
rb-fsevent (~> 0.9, >= 0.9.4)
rb-inotify (~> 0.9, >= 0.9.7)
@@ -295,6 +296,7 @@ DEPENDENCIES
faker
friendly_id (~> 5.2.4)
jbuilder (~> 2.5)
kramdown (~> 1.17)
listen (>= 3.0.5, < 3.2)
pg (>= 0.18, < 2.0)
puma (~> 3.11)
@@ -0,0 +1,19 @@
body {
max-width: 100%;
}

input, textarea {
line-height: 1.5rem;
font-size: 1.25rem;
width: 100%;
margin-bottom: 1em;
}


textarea {

&.textarea--article {
height: 80vh;
}
}

@@ -0,0 +1,41 @@
class Adam::ArticlesController < AdamController
def index
@articles = Article.all
end

def new
@article = Article.new
end

def create
@article = Article.new(article_params)

@article.publish if params[:publish]

if @article.save
TwitterService.post(@article) if params[:publish] && params[:send_to_twitter]
redirect_to adam_articles_path, notice: "Article created successfully"
end
end

def show
set_article
end

def publish
set_article
if @article.publish!
redirect_to long_post_path(@article.params), notice: "Article published successfully"
end
end

private

def set_article
@article = Article.find(params[:id])
end

def article_params
params.require(:article).permit(:title, :content)
end
end
@@ -0,0 +1,17 @@
class PostsController < ApplicationController
before_action :set_published_date, only: :show
def show
@post = Post.where(published_at: @published_date.all_day, slug: params[:slug]).first

render "#{@post.type.downcase.pluralize}/show"
end

private

def set_published_date
@published_date = Date.new(params[:year].to_i,
params[:month].to_i,
params[:day].to_i
)
end
end
@@ -0,0 +1,2 @@
module PostsHelper
end
@@ -1,16 +1,3 @@
class Note < Post
validates :title, absence: true

def published?
published_at.present?
end

def publish
self.published_at = Time.now
end

def publish!
publish
save
end
end
@@ -19,6 +19,19 @@ class Post < ApplicationRecord

scope :published, -> { where("published_at IS NOT NULL").order("published_at DESC") }

def published?
published_at.present?
end

def publish
self.published_at = Time.now
end

def publish!
publish
save
end

def long_url_params
[self.published_at.year,
self.published_at.strftime('%m'),
@@ -0,0 +1,10 @@
<article class="article">
<%= link_to article.title, adam_article_path(article) %>
(
<% if article.published? %>
<%= link_to "published", article %>
<% else %>
Draft
<% end %>
)
</article>
@@ -0,0 +1,7 @@
<div class="action-bar">
<%= link_to "New Article", new_adam_article_path, role: "button", style: "margin-bottom: 2rem" %>
</div>
<h1>Articles</h1>
<div>
<%= render @articles %>
</div>
@@ -0,0 +1,21 @@
<h1>New Article</h1>
<%= form_for @article, url: adam_articles_path do |f| %>
<%= f.text_field :title, autofocus: true, placeholder: "Title" %>
<%= f.text_area :content, class:"textarea--article" %>
<div>
<%= label_tag :publish do %>
<%= check_box_tag :publish %>
Publish
<% end %>
</div>

<div>
<%= label_tag :send_to_twitter do %>
<%= check_box_tag :send_to_twitter %>
Send to Twitter?
<% end %>
</div>
<div style="margin-top: 1em">
<%= f.submit %>
</div>
<% end %>
@@ -0,0 +1,9 @@
<div class="action-bar">
<% unless @article.published? %>
<%= form_tag adam_publish_article_path(@article), method: :put do %>
<%= submit_tag "Publish" %>
<% end %>
<% end %>
</div>
<h1 class="title"><%= @article.title %></h1>
<%= Kramdown::Document.new(@article.content).to_html.html_safe %>
@@ -0,0 +1,2 @@
<h1 class="title"><%= @post.title %></h1>
<%= Kramdown::Document.new(@post.content).to_html.html_safe %>
@@ -9,6 +9,7 @@
<meta name="mobile-web-app-capable" content="yes" />
<meta name="viewport" content="width=device-width, initial-scale=1">
<%= stylesheet_link_tag "application" %>
<%= stylesheet_link_tag "adam" %>
</head>

<body>
@@ -1 +1 @@
<%= render @note %>
<%= render @post, as: :note %>
@@ -0,0 +1 @@
Rails.application.config.assets.precompile += %w( adam.css )
@@ -8,12 +8,14 @@

namespace :adam do
resources :notes
resources :articles
put "notes/:id/publish", to: "notes#publish", as: "publish_note"
put "articles/:id/publish", to: "articles#publish", as: "publish_article"
end

resources :articles, only: :index

get ':year/:month/:day/:slug', to: 'notes#show', as: :long_note
get ':year/:month/:day/:slug', to: 'posts#show', as: :long_post
get 'indiemark', to: 'indiemarks#index'

resources :notes
@@ -0,0 +1,5 @@
require 'rails_helper'

RSpec.describe PostsController, type: :controller do

end
@@ -0,0 +1,15 @@
require 'rails_helper'

# Specs in this file have access to a helper object that includes
# the PostsHelper. For example:
#
# describe PostsHelper do
# describe "string concat" do
# it "concats two strings with spaces" do
# expect(helper.concat_strings("this","that")).to eq("this that")
# end
# end
# end
RSpec.describe PostsHelper, type: :helper do
pending "add some examples to (or delete) #{__FILE__}"
end

0 comments on commit 20bba8c

Please sign in to comment.