Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make ST rails fully compatible with ST.js 0.5.0 and add tests #47

Merged
merged 3 commits into from
Jul 22, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion app/views/sir_trevor/blocks/_heading_block.html.erb
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
<h2><%= heading_block.text %></h2>
<h2 class="st__content-block st__content-block--heading">
<%= without_p_wrap(sir_trevor_format(heading_block.text, format: heading_block.format)) %>
</h2>
10 changes: 9 additions & 1 deletion app/views/sir_trevor/blocks/_list_block.html.erb
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
<div class="st__content-block st__content-block--list">
<%= sir_trevor_markdown list_block.text %>
<% if list_block.format == :html %>
<ul>
<% list_block.listItems.each do |item| %>
<li><%= sir_trevor_format item[:content], format: list_block.format %></li>
<% end %>
</ul>
<% else %>
<%= sir_trevor_markdown list_block.text %>
<% end %>
</div>
8 changes: 7 additions & 1 deletion app/views/sir_trevor/blocks/_quote_block.html.erb
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
<div class="st__content-block st__content-block--quote">
<div class="quote">
<div class="quote__content">
<%= sir_trevor_markdown quote_block.text %>
<% if quote_block.format == :html %>
<blockquote>
<%= sir_trevor_format quote_block.text, format: quote_block.format %>
</blockquote>
<% else %>
<%= sir_trevor_markdown quote_block.text %>
<% end %>
</div>

<% if quote_block.cite.present? %>
Expand Down
2 changes: 1 addition & 1 deletion app/views/sir_trevor/blocks/_text_block.html.erb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
<div class="st__content-block st__content-block--text">
<%= sir_trevor_markdown text_block.text %>
<%= sir_trevor_format text_block.text, format: text_block.format%>
</div>
6 changes: 3 additions & 3 deletions app/views/sir_trevor/blocks/_tweet_block.html.erb
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<div class="st__content-block st__content-block--tweet">
<%= link_to image_tag(tweet_block.profile_image_url, class: 'img'), tweet_block.screen_name %>
<%= link_to image_tag(tweet_block.profile_image_url, class: 'img'), tweet_block.profile_url %>
<p>
<%= tweet_block.render_tweet_body %>
</p>
<cite>From <%= link_to tweet_block.at_name, tweet_block.screen_name %> on Twitter:</cite>
<time datetime="<%= tweet_block.created_at %>">(<%= link_to Time.parse(tweet_block.created_at), tweet_block.status_url %>)</time>
<cite>From <%= link_to tweet_block.at_name, tweet_block.profile_url %> on Twitter:</cite>
<time datetime="<%= tweet_block.created_at %>">(<%= link_to Time.zone.parse(tweet_block.created_at), tweet_block.status_url %>)</time>
</div>

20 changes: 15 additions & 5 deletions lib/sir_trevor_rails/block.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,37 @@

module SirTrevorRails
class Block < OpenStruct
DEFAULT_FORMAT = :markdown

def self.from_hash(hash, parent)
hash = hash.deep_dup
hash = hash.deep_dup.with_indifferent_access
self.type_klass(hash).new(hash, parent)
end

def format
send(:[], :format).present? ? send(:[], :format).to_sym : DEFAULT_FORMAT
end

def initialize(hash, parent)
@as_json = hash
@raw_data = hash
@parent = parent
@type = hash[:type].to_sym

super(hash[:data])
end

attr_reader :parent, :type, :as_json
attr_reader :parent, :type

def to_partial_path
"sir_trevor/blocks/" << self.class.name.demodulize.underscore
end

def as_json(*attrs)
{
type: @type.to_s,
data: marshal_dump
}
end

private

# Infers the block class.
Expand Down Expand Up @@ -58,6 +69,5 @@ def self.type_klass(hash)
self
end
end

end
end
4 changes: 4 additions & 0 deletions lib/sir_trevor_rails/block_array.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ def self.from_json(blocks, parent = nil)
}
end

def to_s
{data: as_json}.to_json
end

def has_block_of_type?(type)
klass = Block.block_class(type)
any? { |b| b.is_a? klass }
Expand Down
2 changes: 1 addition & 1 deletion lib/sir_trevor_rails/blocks/tweet_block.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,4 @@ def profile_image_url(size="bigger")

end
end
end
end
19 changes: 19 additions & 0 deletions lib/sir_trevor_rails/helpers/view_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,21 @@ module SirTrevorRails
module Helpers
module ViewHelper
extend ActiveSupport::Concern
included do
include ActionView::Helpers::SanitizeHelper
end

def sir_trevor_format(text, format: :markdown)
if format.to_s.to_sym == :html
sir_trevor_html(text)
else
sir_trevor_markdown(text)
end
end

def sir_trevor_html(text)
sanitize(text, tags: %w(b i a br p))
end

def sir_trevor_markdown(text)
rndr = CustomMarkdownFormatter.new(hard_wrap: true, filter_html: true,
Expand All @@ -11,6 +26,10 @@ def sir_trevor_markdown(text)
markdown = Redcarpet::Markdown.new(rndr)
markdown.render(text).html_safe
end

def without_p_wrap(html)
Regexp.new('^<p>(.*)<\/p>$').match(html)[1].html_safe rescue html
end
end
end
end
Expand Down
4 changes: 3 additions & 1 deletion sir_trevor_rails.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,11 @@ Gem::Specification.new do |spec|
spec.add_development_dependency "pry-rails"
spec.add_development_dependency "combustion"
spec.add_development_dependency "sqlite3"
spec.add_development_dependency "capybara"
spec.add_development_dependency "launchy"

spec.add_dependency "rails"
spec.add_dependency "redcarpet", ">= 2.0.1", "< 4"
spec.add_dependency "twitter-text", "~> 1.4"

spec.add_dependency 'multi_json', '~> 1.0'
end
7 changes: 7 additions & 0 deletions spec/fixtures/blocks/custom.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "custom",
"data": {
"text": "Body",
"title": "Name"
}
}
7 changes: 7 additions & 0 deletions spec/fixtures/blocks/heading_html.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "heading",
"data": {
"text": "Heading <i>1</i>",
"format": "html"
}
}
6 changes: 6 additions & 0 deletions spec/fixtures/blocks/heading_markdown.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"type": "heading",
"data": {
"text": "Heading _1_"
}
}
26 changes: 26 additions & 0 deletions spec/fixtures/blocks/image.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"type":"image",
"data":{
"id":1,
"updated_at":"2015-01-01T01:00:00+01:00",
"created_at":"2015-01-01T01:00:00+01:00",
"file":{
"url":"http://placeimg.com/1000/600/any",
"thumb":{
"url":"http://placeimg.com/100/100/any"
},
"large":{
"url":"http://placeimg.com/800/400/any"
},
"xlarge":{
"url":"http://placeimg.com/1000/600/any"
},
"medium":{
"url":"http://placeimg.com/600/300/any"
},
"mobile":{
"url":"http://placeimg.com/800/400/any"
}
}
}
}
10 changes: 10 additions & 0 deletions spec/fixtures/blocks/list_html.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"type": "list",
"data": {
"listItems": [
{ "content": "List Item 1" },
{ "content": "<b>List Item 2</b>" }
],
"format": "html"
}
}
6 changes: 6 additions & 0 deletions spec/fixtures/blocks/list_markdown.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"type": "list",
"data": {
"text": " - List Item 1\n - **List Item 2**\n"
}
}
8 changes: 8 additions & 0 deletions spec/fixtures/blocks/quote_html.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"type": "quote",
"data": {
"text": "This is quoted text",
"cite": "Author",
"format": "html"
}
}
7 changes: 7 additions & 0 deletions spec/fixtures/blocks/quote_markdown.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "quote",
"data": {
"text": "> This is quoted text",
"cite": "Author"
}
}
7 changes: 7 additions & 0 deletions spec/fixtures/blocks/text_html.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "text",
"data": {
"text": "One <i>two</i> <b>three</b> <a href='http://example.com'>four</a>",
"format": "html"
}
}
6 changes: 6 additions & 0 deletions spec/fixtures/blocks/text_markdown.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"type": "text",
"data": {
"text": "One _two_ **three** [four](http://example.com)"
}
}
21 changes: 21 additions & 0 deletions spec/fixtures/blocks/tweet.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"type":"tweet",
"data":{
"user":{
"profile_image_url":"http://pbs.twimg.com/profile_images/613336689893896192/5nQjxG2o_normal.jpg",
"profile_image_url_https":"https://pbs.twimg.com/profile_images/613336689893896192/5nQjxG2o_normal.jpg",
"screen_name":"SachseInTheCity",
"name":"Nelson Sachse"
},
"id":"615846875078504448",
"text":"There's no pain without some gain !\n\nlet's run \\o/",
"created_at":"Tue Jun 30 11:38:31 +0000 2015",
"entities":{
"hashtags":[],
"symbols":[],
"user_mentions":[],
"urls":[]
},
"status_url":"https://twitter.com/SachseInTheCity/status/615846875078504448"
}
}
19 changes: 19 additions & 0 deletions spec/integration/editing_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
require 'spec_helper'

class EditingSpec < ActionDispatch::IntegrationTest
describe 'Edit form' do
let(:post) { Post.create(body: blocks_json(:text_html)) }

it 'serializes BlockArray to correct json' do
visit edit_post_path(post)
expect { find_field('Body').value == {data: post.body.as_json}.to_json }
end

it 'serializes empty BlockArray to empty array json' do
post = Post.create()

visit edit_post_path(post)
expect { find_field('Body').value == "{\"data\":[]}" }
end
end
end
Loading