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

Turn Theme into Plain Old Ruby Object #45

Merged
merged 6 commits into from Jul 2, 2014
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 15 additions & 5 deletions CHANGELOG.md
Expand Up @@ -3,11 +3,21 @@
### Changes on `master`

- **Breaking Change**: Themes have been splitted in Themes and Themings. Themings
exist per account containing configuration like copyright/imprint
link urls and reference a theme. Themes now exclusively represent
the available CSS themes and correspond directly to directories
under `pageflow/themes`. Run the migrations to convert your
database.
exist per account in the database containing configuration like
copyright/imprint link urls and reference a theme. Themes represent
available CSS and correspond directly to directories under
`pageflow/themes`. Themes are registered in the Pageflow
initializer. To update your application:

* Add the following line to your `config/initializers/pageflow.rb`:

config.themes.register(:default)

* Install and run the migrations to convert your database.

- Theme CSS files are automatically registered for asset precompilation.
- Theming attributes can now be edited via the accounts admin.
- `pageflow:theme` generator to copy theme template to main application.

- Bug fix: Improve video playback support on iOS and Android.
- Bug fix: Missing translations for attribute/model names in admin.
Expand Down
6 changes: 3 additions & 3 deletions admins/pageflow/accounts.rb
Expand Up @@ -23,7 +23,7 @@ module Pageflow
attributes_table_for account.default_theming do
row :cname, :class => 'cname'
row :theme, :class => 'theme' do
account.default_theming.theme.css_dir
account.default_theming.theme.name
end
end

Expand Down Expand Up @@ -70,12 +70,12 @@ def new

def create
@account = Account.new(permitted_params[:account])
@account.build_default_theming(:theme_id => permitted_params[:account][:default_theming_attributes][:theme_id])
@account.build_default_theming(permitted_params[:account][:default_theming_attributes])
super
end

def permitted_params
params.permit(:account => [:name, :default_file_rights, :default_theming_attributes => [:cname, :theme_id]])
params.permit(:account => [:name, :default_file_rights, :default_theming_attributes => [:cname, :theme_name, :imprint_link_url, :imprint_link_label, :copyright_link_url, :copyright_link_label]])
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion app/helpers/pageflow/entries_helper.rb
Expand Up @@ -25,7 +25,7 @@ def entry_file_rights(entry)
end

def entry_theme_stylesheet_link_tag(entry)
stylesheet_link_tag("pageflow/themes/#{entry.theming.theme.css_dir}.css", :media => 'all')
stylesheet_link_tag(entry.theming.theme.stylesheet_path, :media => 'all')
end

def entry_stylesheet_link_tag(entry)
Expand Down
8 changes: 7 additions & 1 deletion app/models/pageflow/account.rb
Expand Up @@ -3,14 +3,20 @@ class Account < ActiveRecord::Base
has_many :users
has_many :entries
has_many :folders
has_and_belongs_to_many :themes, :join_table => 'pageflow_accounts_themes'

has_many :themings
belongs_to :default_theming, :class_name => 'Theming'

validates :default_theming, :presence => true

accepts_nested_attributes_for :default_theming, :update_only => true

scope :with_landing_page, -> { where.not(:landing_page_name => '') }

def build_default_theming(*args)
super.tap do |theming|
theming.account = self
end
end
end
end
1 change: 0 additions & 1 deletion app/models/pageflow/entry.rb
Expand Up @@ -19,7 +19,6 @@ class Entry < ActiveRecord::Base
has_many :video_files
has_many :audio_files

has_one :theme, :through => :theming
has_one :draft, -> { editable }, :class_name => 'Revision'
has_one :published_revision, -> { published }, :class_name => 'Revision'

Expand Down
6 changes: 0 additions & 6 deletions app/models/pageflow/theme.rb

This file was deleted.

14 changes: 10 additions & 4 deletions app/models/pageflow/theming.rb
@@ -1,14 +1,20 @@
module Pageflow
class Theming < ActiveRecord::Base
belongs_to :theme
has_one :account, :foreign_key => :default_theming_id, :inverse_of => :default_theming
belongs_to :account

validates :account, :presence => true
validates_inclusion_of :theme_name, :in => ->(_) { Pageflow.config.themes.names }

def cname_domain
cname.split('.').pop(2).join('.')
end

def theme
Pageflow.config.themes.get(theme_name)
end

def name
I18n.t('admin.themings.name')
I18n.t('admin.themings.name', :account_name => account.name, :theme_name => theme_name)
end
end
end
end
6 changes: 5 additions & 1 deletion app/views/admin/accounts/_form.html.erb
Expand Up @@ -4,7 +4,11 @@
<%= f.input :default_file_rights %>
<%= f.semantic_fields_for :default_theming do |theming| %>
<%= theming.input :cname %>
<%= theming.input :theme, :include_blank => false, :collection => Pageflow::Theme.all, :member_label => :css_dir %>
<%= theming.input :theme_name, :include_blank => false, :collection => Pageflow.config.themes.names %>
<%= theming.input :imprint_link_label %>
<%= theming.input :imprint_link_url %>
<%= theming.input :copyright_link_label %>
<%= theming.input :copyright_link_url %>
<% end %>
<% end %>
<%= f.actions do %>
Expand Down
14 changes: 0 additions & 14 deletions app/views/admin/themings/_form.html.erb

This file was deleted.

2 changes: 1 addition & 1 deletion app/views/pageflow/entries/_entry.html.erb
@@ -1,7 +1,7 @@
<div id="outer_wrapper">
<%= render 'pageflow/entries/skip_links' %>
<h1 class="hidden"><%= entry.title %></h1>
<img src="<%= image_path("pageflow/themes/#{entry.theming.theme.css_dir}/logo_print.png") %>" class="print_image" alt="Logo image for print version">
<img src="<%= image_path("pageflow/themes/#{entry.theming.theme.directory_name}/logo_print.png") %>" class="print_image" alt="Logo image for print version">
<span class="print_only page_url"><%= request.original_url %></span>
<%= content_tag :div, :id => 'content', :class => 'entry', :data => {:role => 'slideshow'} do %>
<%= render entry.pages %>
Expand Down
2 changes: 1 addition & 1 deletion app/views/pageflow/entries/show.html.erb
Expand Up @@ -4,7 +4,7 @@
<%= entry_theme_stylesheet_link_tag(@entry) %>
<%= entry_stylesheet_link_tag(@entry) %>
<%= render 'pageflow/entries/social_meta_tags', :entry => @entry %>
<%= tag :link, :rel => 'icon', :href => image_path("#{@entry.theming.theme.css_dir}/favicon.ico"), :type => 'image/ico' %>
<%= tag :link, :rel => 'icon', :href => image_path("#{@entry.theming.theme.directory_name}/favicon.ico"), :type => 'image/ico' %>
<% end %>

<%= cache @entry do %>
Expand Down
6 changes: 6 additions & 0 deletions config/locales/activerecord.de.yml
Expand Up @@ -55,6 +55,7 @@ de:
default_file_rights: "Standard Urheber"
default_theming: "Standard Theming"
landing_page_name: "Einstiegsseite"
created_at : "Erstellt am"
user:
email: "E-Mail-Adresse"
unconfirmed_email: "Noch nicht bestätigte E-Mail-Adresse"
Expand Down Expand Up @@ -137,6 +138,11 @@ de:
duration: "Länge"
"pageflow/theming":
cname: "CNAME"
theme_name: "Theme"
imprint_link_label: "Impressum-Link Text"
imprint_link_url: "Impressum-Link URL"
copyright_link_label: "Copyright-Link Text"
copyright_link_url: "Copyright-Link URL"
values:
"pageflow/page":
template:
Expand Down
2 changes: 1 addition & 1 deletion config/locales/admin/themings.yml
Expand Up @@ -4,4 +4,4 @@ de:
cname_hint: "Verwendet für Social Network Sharing."
show: "Anzeigen"
remove_logo: "Logo entfernen"
name: ""
name: "%{account_name}"
14 changes: 14 additions & 0 deletions db/migrate/20140625184800_drop_themes.rb
@@ -0,0 +1,14 @@
class DropThemes < ActiveRecord::Migration
def up
rename_column :pageflow_accounts_themes, :theme_id, :theme_name
add_column :pageflow_themings, :theme_name, :string

execute(<<-SQL)
UPDATE pageflow_themings SET theme_name =
(SELECT css_dir FROM pageflow_themes WHERE pageflow_themes.id = pageflow_themings.theme_id);
SQL

remove_reference :pageflow_themings, :theme
drop_table :pageflow_themes
end
end
9 changes: 9 additions & 0 deletions lib/generators/pageflow/initializer/templates/pageflow.rb
Expand Up @@ -15,6 +15,15 @@
config.register_page_type(Pageflow::BuiltInPageType.internal_links)
# config.register_page_type(Pageflow::Rainbow::PageType.new)

# Add custom themes by invoking the pageflow:theme generator and
# registering the theme here.
#
# $ rails generate pageflow:theme my_custom_theme
# => creates app/assets/stylesheets/pageflow/themes/my_custom_theme.css.scss
#
config.themes.register(:default)
# config.themes.register(:my_custom_theme)

# String to interpolate into paths of files generated by paperclip
# preprocessors. This allows to refresh cdn caches after
# reprocessing attachments.
Expand Down
28 changes: 28 additions & 0 deletions lib/generators/pageflow/theme/theme_generator.rb
@@ -0,0 +1,28 @@
module Pageflow
module Generators
class ThemeGenerator < Rails::Generators::Base
desc 'Copies a theme template to the app.'

argument :name, required: false, default: 'custom',
desc: 'The scope to copy views to'

source_root File.expand_path("../../../../../app/assets", __FILE__)

def copy_template
directory('stylesheets/pageflow/themes/default', themes_path('stylesheets', name))
directory('images/pageflow/themes/default', themes_path('images', name))

template('stylesheets/pageflow/themes/default.css.scss', themes_path('stylesheets', "#{name}.css.scss")) do |content|
content.gsub!('$theme-name: "default";', %Q'$theme-name: "#{name}";')
content.gsub!('@import "./default/', %Q'@import "./#{name}/')
end
end

private

def themes_path(type, path)
File.join('app', 'assets', type, 'pageflow', 'themes', path)
end
end
end
end
10 changes: 10 additions & 0 deletions lib/pageflow/configuration.rb
Expand Up @@ -51,6 +51,15 @@ class Configuration
#
attr_accessor :quotas

# Additional themes can be registered to use custom css.
#
# Example:
#
# config.themes.register(:custom)
#
# @return [Themes]
attr_reader :themes

# Either a lambda or an object with a `match?` method, to restrict
# access to the editor routes defined by Pageflow.
#
Expand All @@ -69,6 +78,7 @@ def initialize

@hooks = Hooks.new
@quotas = Quotas.new
@themes = Themes.new
end

# Make a page type available for use in the system.
Expand Down
7 changes: 6 additions & 1 deletion lib/pageflow/engine.rb
Expand Up @@ -51,10 +51,15 @@ class Engine < ::Rails::Engine
# provided by the main app.
config.assets.precompile += %w(pageflow/editor.js pageflow/editor.css
pageflow/print_view.css
pageflow/themes/default.css
pageflow/lt_ie9.js pageflow/lt_ie9.css pageflow/ie9.js pageflow/ie9.css
video-js.swf vjs.eot vjs.svg vjs.ttf vjs.woff)

config.assets.precompile << lambda do |path|
Pageflow.config.themes.any? do |theme|
path == theme.stylesheet_path
end
end

# Make sure the configuration is recreated when classes are
# reloded. Otherwise registered page types might still point to
# unloaded classes in development mode.
Expand Down
30 changes: 12 additions & 18 deletions lib/pageflow/seeds.rb
Expand Up @@ -3,27 +3,21 @@
module Pageflow
# Default Account

if Theming.any?
default_theming = Theming.first
puts " Theming exists."
else
theme = Theme.create(:css_dir => 'default')
default_theming = Theming.create!(:theme_id => theme.id,
:imprint_link_label => 'Impressum',
:imprint_link_url => 'http://example.com/impressum.html',
:copyright_link_label => '&copy; Pageflow 2014',
:copyright_link_url => 'http://www.example.com/copyright.html')
puts " Created theming."
end

# Default Account

if Account.any?
default_account = Account.first
default_account.default_theming = default_theming if default_account.default_theming.blank?
puts " Account exists."
else
default_account = Account.create!(:name => 'Pageflow', :default_theming_id => default_theming.id)
default_account = Account.create!(:name => 'Pageflow') do |account|
account.build_default_theming do |theming|
theming.theme_name = Pageflow.config.themes.names.first

theming.imprint_link_label = 'Impressum'
theming.imprint_link_url = 'http://example.com/impressum.html'
theming.copyright_link_label = '&copy; Pageflow 2014'
theming.copyright_link_url = 'http://www.example.com/copyright.html'
end
end

puts " Created default account."
end

Expand Down Expand Up @@ -66,7 +60,7 @@ def self.create_user(options)
# Sample entry

unless Entry.any?
entry = default_account.entries.create!(:title => 'Fiese Flut', :theming_id => default_theming.id)
entry = default_account.entries.create!(:title => 'Fiese Flut', :theming_id => default_account.default_theming.id)

chapter = entry.draft.chapters.create!(:title => 'Kapitel 1')
chapter.pages.create!(:template => 'background_image')
Expand Down
14 changes: 14 additions & 0 deletions lib/pageflow/theme.rb
@@ -0,0 +1,14 @@
module Pageflow
class Theme
attr_reader :name, :directory_name

def initialize(name)
@name = name.to_s
@directory_name = name.to_s
end

def stylesheet_path
"pageflow/themes/#{name}.css"
end
end
end
25 changes: 25 additions & 0 deletions lib/pageflow/themes.rb
@@ -0,0 +1,25 @@
module Pageflow
class Themes
include Enumerable

def initialize
@themes = HashWithIndifferentAccess.new
end

def register(name)
@themes[name] = Theme.new(name)
end

def get(name)
@themes.fetch(name) { raise(ArgumentError, "Unknown theme '#{name}'.") }
end

def names
map(&:name)
end

def each(&block)
@themes.values.each(&block)
end
end
end