Model goop

also made the theme selection logic for user profiles and current user, and the saving logic
This commit is contained in:
Yuki 2015-08-25 13:56:36 +05:30
parent e42f81193b
commit 1912fc3766
8 changed files with 137 additions and 16 deletions

View File

@ -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

View File

@ -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

30
app/models/theme.rb Normal file
View File

@ -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

View File

@ -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

View File

@ -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?

View File

@ -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 {

View File

@ -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}

View File

@ -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