From 1912fc37666f79cbe983ee421bb6021bed014bc0 Mon Sep 17 00:00:00 2001 From: Yuki Date: Tue, 25 Aug 2015 13:56:36 +0530 Subject: [PATCH] Model goop also made the theme selection logic for user profiles and current user, and the saving logic --- app/controllers/user_controller.rb | 33 ++++++++++++++++-- app/helpers/theme_helper.rb | 21 +++++++++--- app/models/theme.rb | 30 ++++++++++++++++ app/models/user.rb | 2 ++ app/views/layouts/application.html.haml | 6 +++- app/views/user/theme.css.scss.erb | 17 ++++----- config/routes.rb | 4 ++- db/migrate/20150825073030_create_themes.rb | 40 ++++++++++++++++++++++ 8 files changed, 137 insertions(+), 16 deletions(-) create mode 100644 app/models/theme.rb create mode 100644 db/migrate/20150825073030_create_themes.rb diff --git a/app/controllers/user_controller.rb b/app/controllers/user_controller.rb index 546404a3..7af373b1 100644 --- a/app/controllers/user_controller.rb +++ b/app/controllers/user_controller.rb @@ -94,7 +94,36 @@ class UserController < ApplicationController def data end - def theme - render body: render_theme_with_context, content_type: 'text/css' + def edit_theme + end + + def update_theme + update_attributes = params.require(:theme).permit([ + :primary_color, :primary_text, + :danger_color, :danger_text, + :success_color, :success_text, + :warning_color, :warning_text, + :info_color, :info_text, + :default_color, :default_text, + :panel_color, :panel_text, + :link_color, :background_color, + :background_text, :background_muted + ]) + + if current_user.theme.nil? + current_user.theme = Theme.new update_attributes + current_user.theme.user_id = current_user.id + + if current_user.theme.save + flash[:success] = t('flash.user.update_theme.success') + else + flash[:error] = t('flash.user.update_theme.error') + end + elsif current_user.theme.update_attributes(user_attributes) + flash[:success] = t('flash.user.update_theme.success') + else + flash[:error] = t('flash.user.update_theme.error') + end + redirect_to edit_user_theme_path end end diff --git a/app/helpers/theme_helper.rb b/app/helpers/theme_helper.rb index 6e75e3ca..dcaa0576 100644 --- a/app/helpers/theme_helper.rb +++ b/app/helpers/theme_helper.rb @@ -3,12 +3,25 @@ module ThemeHelper klass = Class.new do def initialize(hash = {}) if hash.is_a? ActiveRecord::Base - hash = hash.serializable_hash - end + x = [:primary_color, :primary_text, + :danger_color, :danger_text, + :success_color, :success_text, + :warning_color, :warning_text, + :info_color, :info_text, + :default_color, :default_text, + :panel_color, :panel_text, + :link_color, :background_color, + :background_text, :background_muted] - if hash.is_a? Hash + x.each do |v| + next if hash[v].nil? + self.instance_variable_set "@#{v}", hash[v].to_s(16)[-6, 6] + end + elsif hash.is_a? Hash hash.each do |k, v| - self.instance_variable_set "@#{k}", v + next unless v.is_a? Fixnum + + self.instance_variable_set "@#{k}", v.to_s(16)[-6, 6] end end end diff --git a/app/models/theme.rb b/app/models/theme.rb new file mode 100644 index 00000000..4a05d480 --- /dev/null +++ b/app/models/theme.rb @@ -0,0 +1,30 @@ +class Theme < ActiveRecord::Base + include ThemeHelper + + belongs_to :user + + validates_numericality_of :primary_color, :primary_text, + :danger_color, :danger_text, + :success_color, :success_text, + :warning_color, :warning_text, + :info_color, :info_text, + :default_color, :default_text, + :panel_color, :panel_text, + :link_color, :background_color, + :background_text, :background_muted, + greater_than_or_equal_to: 0, less_than_or_equal_to: 0xFFFFFF, + allow_nil: true, only_integer: true + + before_save do + style = StringIO.new(render_theme_with_context(self).render) + + style.class.class_eval { + attr_accessor :original_filename, :content_type + } + + style.content_type = 'text/stylesheet' + style.original_filename = 'theme.css' + + self.css = style + end +end diff --git a/app/models/user.rb b/app/models/user.rb index 1872ece9..507821ea 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -31,6 +31,8 @@ class User < ActiveRecord::Base has_many :subscriptions, dependent: :destroy + has_one :theme, dependent: :destroy + SCREEN_NAME_REGEX = /\A[a-zA-Z0-9_]{1,16}\z/ WEBSITE_REGEX = /https?:\/\/([A-Za-z.\-]+)\/?(?:.*)/i diff --git a/app/views/layouts/application.html.haml b/app/views/layouts/application.html.haml index 68376f4a..c2bd4ed5 100644 --- a/app/views/layouts/application.html.haml +++ b/app/views/layouts/application.html.haml @@ -12,7 +12,11 @@ %title= yield(:title) = javascript_include_tag 'i18n', 'data-turbolinks-track' => true = stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track' => true - %link{rel: 'stylesheet', href: '/yuki/theme.css', media: :all, 'data-turbolinks-track' => true} + - if user_signed_in? and current_user.theme.present? + %link{rel: 'stylesheet', href: current_user.theme.css.url, media: :all, 'data-turbolinks-track' => true} + - elsif @user.present? and @user.theme.present? + %link{rel: 'stylesheet', href: @user.theme.css.url, media: :all, 'data-turbolinks-track' => true} + - else = javascript_include_tag 'application', 'data-turbolinks-track' => true - if user_signed_in? - if current_user.mod? diff --git a/app/views/user/theme.css.scss.erb b/app/views/user/theme.css.scss.erb index 9c9b94aa..6ec29334 100644 --- a/app/views/user/theme.css.scss.erb +++ b/app/views/user/theme.css.scss.erb @@ -7,47 +7,47 @@ // PRIMARY COLOR $primary_color: <%= @primary_color || "#5e35b1" %>; -$primary_border: <%= @primary_border || "darken(adjust-hue(#5e35b1, -10), 5%)" %>; +$primary_border: darken(adjust-hue($primary_color, -10), 5%); $primary_text: <%= @primary_text || "white" %>; // DANGER COLOR $danger_color: <%= @danger_color || "#FF0039" %>; -$danger_border: <%= @danger_border || "darken(adjust-hue(#FF0039, -10), 5%)" %>; +$danger_border: darken(adjust-hue($danger_color, -10), 5%); $danger_text: <%= @danger_text || "white" %>; // SUCCESS COLOR $success_color: <%= @success_color || "#3FB618" %>; -$success_border: <%= @success_border || "darken(adjust-hue(#3FB618, -10), 5%)" %>; +$success_border: darken(adjust-hue($success_color, -10), 5%); $success_text: <%= @success_text || "white" %>; // WARNING COLOR $warning_color: <%= @warning_color || "#FF7518" %>; -$warning_border: <%= @warning_border || "darken(adjust-hue(#FF7518, -10), 5%)" %>; +$warning_border: darken(adjust-hue($warning_color, -10), 5%); $warning_text: <%= @warning_text || "white" %>; // INFO COLOR $info_color: <%= @info_color || "#9954BB" %>; -$info_border: <%= @info_border || "darken(adjust-hue(#9954BB, -10), 5%)" %>; +$info_border: darken(adjust-hue($info_color, -10), 5%); $info_text: <%= @info_text || "white" %>; // DEFAULT COLOR $default_color: <%= @default_color || "#222222" %>; -$default_border: <%= @default_border || "darken(adjust-hue(#222222, -10), 5%)" %>; +$default_border: darken(adjust-hue($default_color, -10), 5%); $default_text: <%= @default_text || "#eeeeee" %>; // PANEL COLOR $panel_color: <%= @panel_color || "#F9F9F9" %>; -$panel_border: <%= @panel_border || "darken(adjust-hue(#F9F9F9, -10), 5%)" %>; +$panel_border: darken(adjust-hue($panel_color, -10), 5%); $panel_text: <%= @panel_text || "#151515" %>; // AUXILIARY COLOR -$link_color: <%= @link_color || "#5e35b1" %>; +$link_color: <%= @link_color || "#5E35B1" %>; $background_color: <%= @background_color || "#ffffff" %>; $background_text: <%= @background_text || "#222222" %>; $background_muted: <%= @background_muted || "#bbbbbb" %>; @@ -76,6 +76,7 @@ body#version1 { background-color: $primary_color; border-color: $primary_border; border-top: none; + color: $primary_text; .media, .dropdown-header { diff --git a/config/routes.rb b/config/routes.rb index 068a2c28..3f9f2cad 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -54,6 +54,9 @@ Rails.application.routes.draw do match '/settings/profile', to: 'user#edit', via: 'get', as: :edit_user_profile match '/settings/profile', to: 'user#update', via: 'patch', as: :update_user_profile + match '/settings/theme', to: 'user#edit_theme', via: 'get', as: :edit_user_theme + match '/settings/theme', to: 'user#update_theme', via: 'patch', as: :update_user_theme + # resources :services, only: [:index, :destroy] match '/settings/services', to: 'services#index', via: 'get', as: :services match '/settings/services/:id', to: 'services#destroy', via: 'delete', as: :service @@ -113,7 +116,6 @@ Rails.application.routes.draw do match '/:username(/p/:page)', to: 'user#show', via: 'get', as: :show_user_profile, defaults: {page: 1} match '/:username/a/:id', to: 'answer#show', via: 'get', as: :show_user_answer match '/:username/q/:id', to: 'question#show', via: 'get', as: :show_user_question - match '/:username/theme.css', to: 'user#theme', via: 'get', as: :show_user_theme match '/:username/followers(/p/:page)', to: 'user#followers', via: 'get', as: :show_user_followers, defaults: {page: 1} match '/:username/friends(/p/:page)', to: 'user#friends', via: 'get', as: :show_user_friends, defaults: {page: 1} match '/:username/groups(/p/:page)', to: 'user#groups', via: 'get', as: :show_user_groups, defaults: {page: 1} diff --git a/db/migrate/20150825073030_create_themes.rb b/db/migrate/20150825073030_create_themes.rb new file mode 100644 index 00000000..dec69a9f --- /dev/null +++ b/db/migrate/20150825073030_create_themes.rb @@ -0,0 +1,40 @@ +class CreateThemes < ActiveRecord::Migration + def change + create_table :themes do |t| + t.integer :user_id, null: false + + t.integer :primary_color, limit: 4, default: 0x5E35B1 + t.integer :primary_text, limit: 4, default: 0xFFFFFF + + t.integer :danger_color, limit: 4, default: 0xFF0039 + t.integer :danger_text, limit: 4, default: 0xFFFFFF + + t.integer :success_color, limit: 4, default: 0x3FB618 + t.integer :success_text, limit: 4, default: 0xFFFFFF + + t.integer :warning_color, limit: 4, default: 0xFF7518 + t.integer :warning_text, limit: 4, default: 0xFFFFFF + + t.integer :info_color, limit: 4, default: 0x9954BB + t.integer :info_text, limit: 4, default: 0xFFFFFF + + t.integer :default_color, limit: 4, default: 0x222222 + t.integer :default_text, limit: 4, default: 0xEEEEEE + + t.integer :panel_color, limit: 4, default: 0xF9F9F9 + t.integer :panel_text, limit: 4, default: 0x151515 + + t.integer :link_color, limit: 4, default: 0x5E35B1 + + t.integer :background_color, limit: 4, default: 0xFFFFFF + t.integer :background_text, limit: 4, default: 0x222222 + t.integer :background_muted, limit: 4, default: 0xBBBBBB + + t.attachment :css + + t.timestamps null: false + end + + add_index :themes, [:user_id, :created_at] + end +end