Issue #4297: Style fixes - Default theme is configurable

This commit is contained in:
efstrian 2015-06-04 16:38:49 +03:00
parent 31d760867c
commit d64ea15891
9 changed files with 40 additions and 40 deletions

View file

@ -13,39 +13,30 @@ module UsersHelper
# is not signed in or has not specified a color theme, the
# default (original) color theme is loaded.
#
# ==== Examples
#
# # if user is not signed in
# current_color_theme # => "color_themes/original"
# # if user Alice has not selected a color theme
# current_color_theme # => "color_themes/original"
# # if user Alice has selected a "magenta" theme
# current_color_theme # => "color_themes/magenta"
# @example if user is not signed in
# current_color_theme #=> "color_themes/original"
# @example if user Alice has not selected a color theme
# current_color_theme #=> "color_themes/original"
# @example if user Alice has selected a "magenta" theme
# current_color_theme #=> "color_themes/magenta"
def current_color_theme
if user_signed_in?
color_theme = current_user.color_theme
end
color_theme ||= DEFAULT_COLOR_THEME
current_color_theme = "color_themes/" + color_theme
color_theme ||= AppConfig.settings.default_color_theme
"color_themes/#{color_theme}"
end
# Returns an array of the color themes available, as
# specified from AVAILABLE_COLOR_THEMES in
# config/initializers/color_themes.rb.
#
# ==== Examples
#
# # if config/color_themes.yml is:
# available:
# original: "Original dark"
# dark_green: "Dark green"
# # and AVAILABLE_COLOR_THEMES is accordingly initialized,
# # then:
# @example if AVAILABLE_COLOR_THEMES = {"original"=>"Original dark", "dark_green" => "Dark green"}
# available_color_themes
# # => [["Original dark", "original"], ["Dark green", "dark_green"]]
# #=> [["Original dark", "original"], ["Dark green", "dark_green"]]
def available_color_themes
opts = []
AVAILABLE_COLOR_THEMES.each do |theme_code, theme_name|
AVAILABLE_COLOR_THEMES.map do |theme_code, theme_name|
opts << [theme_name, theme_code]
end
opts

View file

@ -23,14 +23,14 @@ class User < ActiveRecord::Base
before_validation :strip_and_downcase_username
before_validation :set_current_language, :on => :create
before_validation :set_default_color_theme, :on => :create
before_validation :set_default_color_theme, on: :create
validates :username, :presence => true, :uniqueness => true
validates_format_of :username, :with => /\A[A-Za-z0-9_]+\z/
validates_length_of :username, :maximum => 32
validates_exclusion_of :username, :in => AppConfig.settings.username_blacklist
validates_inclusion_of :language, :in => AVAILABLE_LANGUAGE_CODES
validates_inclusion_of :color_theme, :in => AVAILABLE_COLOR_THEME_CODES, :allow_blank => true
validates :color_theme, inclusion: {in: AVAILABLE_COLOR_THEME_CODES}, allow_blank: true
validates_format_of :unconfirmed_email, :with => Devise.email_regexp, :allow_blank => true
validates_presence_of :person, :unless => proc {|user| user.invitation_token.present?}
@ -201,7 +201,7 @@ class User < ActiveRecord::Base
end
def set_default_color_theme
self.color_theme ||= DEFAULT_COLOR_THEME
self.color_theme ||= AppConfig.settings.default_color_theme
end
# This override allows a user to enter either their email address or their username into the username field.
@ -435,7 +435,7 @@ class User < ActiveRecord::Base
self.language = opts[:language]
self.language ||= I18n.locale.to_s
self.color_theme = opts[:color_theme]
self.color_theme ||= DEFAULT_COLOR_THEME
self.color_theme ||= AppConfig.settings.default_color_theme
self.valid?
errors = self.errors
errors.delete :person

View file

@ -26,7 +26,7 @@
- if rtl?
= stylesheet_link_tag :rtl, media: 'all'
= stylesheet_link_tag current_color_theme, :media => "all"
= stylesheet_link_tag current_color_theme, media: "all"
= old_browser_js_support
<!--[if IE]>

View file

@ -46,7 +46,7 @@
- if rtl?
= stylesheet_link_tag :rtl, :media => 'all'
= stylesheet_link_tag current_color_theme, :media => "all"
= stylesheet_link_tag current_color_theme, media: "all"
= yield(:head)

View file

@ -143,6 +143,7 @@ defaults:
warn_days: 30
limit_removals_to_per_day: 100
source_url:
default_color_theme: "original"
services:
facebook:
enable: false

View file

@ -533,6 +533,12 @@ configuration: ## Section
## If not set your pod will provide a downloadable archive.
#source_url: 'https://example.org/username/diaspora'
## Default color theme
## You can change which color theme is displayed when a user is not signed in
## or has not selected any color theme from the available ones. You simply have
## to enter the name of the theme's .scss file in
## app/assets/stylesheets/color_themes/ without the file extension.
#default_color_theme: "original"
## Posting from Diaspora to external services (all are disabled by default).
services: ## Section

View file

@ -1,18 +1,19 @@
require 'pathname'
# Generate the path to the .yml file with the available color themes.
color_themes_file = Pathname.new(__FILE__).dirname.join('..').expand_path.join("color_themes.yml")
color_themes_file = Rails.root.join("config/color_themes.yml")
# Check in case config/color_themes.yml does not exist.
if color_themes_file.exist?
# Load the file specified by the generated path.
color_themes = YAML.load_file(color_themes_file)
# If the file contains one or more color themes, include them in AVAILABLE_COLOR_THEMES, else include the original theme.
AVAILABLE_COLOR_THEMES = (color_themes["available"].length > 0) ? color_themes["available"] : { "original" => "original.scss" }
# If the file contains one or more color themes, include them in AVAILABLE_COLOR_THEMES,
# else include the original theme.
AVAILABLE_COLOR_THEMES =
if color_themes["available"].length > 0
color_themes["available"]
else
{"original" => "Original Dark"}
end
else
AVAILABLE_COLOR_THEMES = { "original" => "original.scss" }
AVAILABLE_COLOR_THEMES = {"original" => "Original Dark"}
end
# Get all codes from available themes into a separate variable, so that they can be called easier.
AVAILABLE_COLOR_THEME_CODES = AVAILABLE_COLOR_THEMES.keys
# The default color theme, that will be called when no color theme is specified.
DEFAULT_COLOR_THEME = "original"

View file

@ -157,7 +157,7 @@ describe UsersController, :type => :controller do
old_color_theme = "original"
@user.color_theme = old_color_theme
@user.save
put(:update, :id => @user.id, :user => { :color_theme => "dark_green"})
put(:update, id: @user.id, user: {color_theme: "dark_green"})
@user.reload
expect(@user.color_theme).not_to eq(old_color_theme)
end

View file

@ -1,4 +1,4 @@
require 'spec_helper'
require "spec_helper"
describe UsersHelper, :type => :helper do
include Devise::TestHelpers
@ -12,7 +12,7 @@ describe UsersHelper, :type => :helper do
end
it "returns the default color theme" do
expect(current_color_theme).to eq("color_themes/" + DEFAULT_COLOR_THEME)
expect(current_color_theme).to eq("color_themes/#{AppConfig.settings.default_color_theme}")
end
end
@ -22,19 +22,20 @@ describe UsersHelper, :type => :helper do
def user_signed_in?
true
end
def current_user
@user
end
end
it "returns the default color theme if user has not selected any theme" do
expect(current_color_theme).to eq("color_themes/" + DEFAULT_COLOR_THEME)
expect(current_color_theme).to eq("color_themes/#{AppConfig.settings.default_color_theme}")
end
it "returns the color theme selected by the user if there is a selected one" do
selected_theme = "test_theme"
@user.color_theme = selected_theme
expect(current_color_theme).to eq("color_themes/" + selected_theme)
expect(current_color_theme).to eq("color_themes/#{selected_theme}")
end
end
end