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

View file

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

View file

@ -26,7 +26,7 @@
- if rtl? - if rtl?
= stylesheet_link_tag :rtl, media: 'all' = 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 = old_browser_js_support
<!--[if IE]> <!--[if IE]>

View file

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

View file

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

View file

@ -533,6 +533,12 @@ configuration: ## Section
## If not set your pod will provide a downloadable archive. ## If not set your pod will provide a downloadable archive.
#source_url: 'https://example.org/username/diaspora' #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). ## Posting from Diaspora to external services (all are disabled by default).
services: ## Section services: ## Section

View file

@ -1,18 +1,19 @@
require 'pathname'
# Generate the path to the .yml file with the available color themes. # 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. # Check in case config/color_themes.yml does not exist.
if color_themes_file.exist? if color_themes_file.exist?
# Load the file specified by the generated path. # Load the file specified by the generated path.
color_themes = YAML.load_file(color_themes_file) 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. # If the file contains one or more color themes, include them in AVAILABLE_COLOR_THEMES,
AVAILABLE_COLOR_THEMES = (color_themes["available"].length > 0) ? color_themes["available"] : { "original" => "original.scss" } # else include the original theme.
AVAILABLE_COLOR_THEMES =
if color_themes["available"].length > 0
color_themes["available"]
else
{"original" => "Original Dark"}
end
else else
AVAILABLE_COLOR_THEMES = { "original" => "original.scss" } AVAILABLE_COLOR_THEMES = {"original" => "Original Dark"}
end end
# Get all codes from available themes into a separate variable, so that they can be called easier. # 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 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" old_color_theme = "original"
@user.color_theme = old_color_theme @user.color_theme = old_color_theme
@user.save @user.save
put(:update, :id => @user.id, :user => { :color_theme => "dark_green"}) put(:update, id: @user.id, user: {color_theme: "dark_green"})
@user.reload @user.reload
expect(@user.color_theme).not_to eq(old_color_theme) expect(@user.color_theme).not_to eq(old_color_theme)
end end

View file

@ -1,4 +1,4 @@
require 'spec_helper' require "spec_helper"
describe UsersHelper, :type => :helper do describe UsersHelper, :type => :helper do
include Devise::TestHelpers include Devise::TestHelpers
@ -12,7 +12,7 @@ describe UsersHelper, :type => :helper do
end end
it "returns the default color theme" do 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
end end
@ -22,19 +22,20 @@ describe UsersHelper, :type => :helper do
def user_signed_in? def user_signed_in?
true true
end end
def current_user def current_user
@user @user
end end
end end
it "returns the default color theme if user has not selected any theme" do 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 end
it "returns the color theme selected by the user if there is a selected one" do it "returns the color theme selected by the user if there is a selected one" do
selected_theme = "test_theme" selected_theme = "test_theme"
@user.color_theme = selected_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 end
end end