Permalink
Browse files

Add embed code field to entry admin attributes table

Display a text field with the iframe snippet.
  • Loading branch information...
1 parent c3ab4c2 commit 323417f827dd4071f116e39409f54ba3f59776e2 @tf tf committed Oct 28, 2016
@@ -155,6 +155,7 @@ module Pageflow
controller do
helper FoldersHelper
helper EntriesHelper
+ helper EmbedCodeHelper
helper Admin::EntriesHelper
helper Admin::FeaturesHelper
helper Admin::FormHelper
@@ -2,6 +2,7 @@
@import "pageflow/admin/badge_list";
@import "pageflow/admin/columns";
+@import "pageflow/admin/embed_code";
@import "pageflow/admin/embedded_index_table";
@import "pageflow/admin/entries";
@import "pageflow/admin/features";
@@ -0,0 +1,13 @@
+.embed_code {
+ $hint-color: #9c9c9c;
+
+ input {
+ width: 100%;
+ }
+
+ p {
+ margin: 5px 0 0 0;
+ color: $hint-color;
+ font-style: italic;
+ }
+}
@@ -0,0 +1,32 @@
+module Pageflow
+ module EmbedCodeHelper
+ def embed_code_entry_snippet(entry)
+ Pageflow::EmbedCodeHelper::EntrySnippet.new(pageflow, entry)
+ end
+
+ class EntrySnippet
+ def initialize(routes, entry)
+ @routes = routes
+ @entry = entry
+ end
+
+ def call
+ %'<iframe src="#{url(entry)}" scrolling="no" allowfullscreen></iframe>'
+ end
+
+ private
+
+ attr_reader :routes, :entry
+
+ def url(entry)
+ routes.entry_embed_url(entry, entry_embed_url_options(entry))
+ end
+
+ def entry_embed_url_options(entry)
+ options = Pageflow.config.entry_embed_url_options
+ options = options.call(entry.theming) if options.respond_to?(:call)
+ options
+ end
+ end
+ end
+end
@@ -35,4 +35,9 @@ attributes_table_for entry do
row :url do
pretty_entry_url(entry)
end
+ row :embed_code do
+ embed_code_field(embed_code_entry_snippet(entry),
+ name: :entry_embed_code,
+ hint: I18n.t('pageflow.admin.entries.embed_code_hint'))
+ end
end
@@ -0,0 +1,15 @@
+module Pageflow
+ module Admin
+ class EmbedCodeField < Pageflow::ViewComponent
+ builder_method :embed_code_field
+
+ def build(snippet, options = {})
+ super(class: 'embed_code')
+ text_node(text_field_tag(options.fetch(:name),
+ snippet.call,
+ onclick: '$(this).select()'))
+ para(options[:hint])
+ end
+ end
+ end
+end
@@ -0,0 +1,9 @@
+de:
+ pageflow:
+ admin:
+ entries:
+ embed_code_hint: "Bette den veröffentlichten Pageflow auf einer Website ein."
+ activerecord:
+ attributes:
+ pageflow/entry:
+ embed_code: "Einbetten"
@@ -0,0 +1,9 @@
+en:
+ pageflow:
+ admin:
+ entries:
+ embed_code_hint: "Embed the published entry on a website."
+ activerecord:
+ attributes:
+ pageflow/entry:
+ embed_code: "Embed"
@@ -169,6 +169,11 @@ class Configuration
# end
attr_accessor :public_entry_url_options
+ # Either a lambda or an object with a `call` method taking a
+ # {Theming} as paramater and returing a hash of options used to
+ # construct the embed url of a published entry.
+ attr_accessor :entry_embed_url_options
+
# Submit video/audio encoding jobs only after the user has
# explicitly confirmed in the editor. Defaults to false.
attr_accessor :confirm_encoding_jobs
@@ -266,6 +271,7 @@ def initialize
@theming_request_scope = CnameThemingRequestScope.new
@public_entry_request_scope = lambda { |entries, request| entries }
@public_entry_url_options = Pageflow::ThemingsHelper::DEFAULT_PUBLIC_ENTRY_OPTIONS
+ @entry_embed_url_options = {protocol: 'https'}
@confirm_encoding_jobs = false
@@ -0,0 +1,49 @@
+require 'spec_helper'
+
+module Pageflow
+ describe EmbedCodeHelper do
+ describe '#embed_code_entry_snippet' do
+ it 'returns HTML code for an iframe' do
+ entry = create(:entry)
+ snippet = helper.embed_code_entry_snippet(entry)
+
+ result = snippet.call
+
+ expect(result).to have_selector('iframe')
+ end
+
+ it 'uses https protocol by default' do
+ entry = create(:entry, title: 'my-entry')
+ snippet = helper.embed_code_entry_snippet(entry)
+
+ result = snippet.call
+
+ expected_url = 'https://test.host/my-entry/embed'
+ expect(result).to have_selector("iframe[src='#{expected_url}']")
+ end
+
+ it 'uses entry_embed_url_options supplied as hash' do
+ Pageflow.config.entry_embed_url_options = {host: 'some.example.com'}
+ entry = create(:entry, title: 'my-entry')
+ snippet = helper.embed_code_entry_snippet(entry)
+
+ result = snippet.call
+
+ expected_domain = 'http://some.example.com/my-entry/embed'
+ expect(result).to have_selector("iframe[src='#{expected_domain}']")
+ end
+
+ it 'uses entry_embed_url_options supplied as lambda' do
+ Pageflow.config.entry_embed_url_options = ->(theming) { {host: theming.cname} }
+ theming = create(:theming, cname: 'example.com')
+ entry = create(:entry, theming: theming, title: 'my-entry')
+ snippet = helper.embed_code_entry_snippet(entry)
+
+ result = snippet.call
+
+ expected_domain = 'http://example.com/my-entry/embed'
+ expect(result).to have_selector("iframe[src*='#{expected_domain}']")
+ end
+ end
+ end
+end
@@ -0,0 +1,31 @@
+require 'spec_helper'
+
+module Pageflow
+ module Admin
+ describe EmbedCodeField, type: :view_component do
+ it 'renders a text field with the given name' do
+ snippet = -> { '<iframe />' }
+
+ render(snippet, name: 'some_embed')
+
+ expect(rendered).to have_selector('input[name=some_embed]')
+ end
+
+ it 'renders a text field with the html snippet' do
+ snippet = -> { '<iframe />' }
+
+ render(snippet, name: 'some_embed')
+
+ expect(rendered).to have_selector("input[value='<iframe />']")
+ end
+
+ it 'can render a hint' do
+ snippet = -> { '<iframe />' }
+
+ render(snippet, name: 'some_embed', hint: 'a hint')
+
+ expect(rendered).to have_selector('p', text: 'a hint')
+ end
+ end
+ end
+end

0 comments on commit 323417f

Please sign in to comment.