Skip to content

Commit

Permalink
Merge e815b31 into 8c6f3dd
Browse files Browse the repository at this point in the history
  • Loading branch information
kwappa committed Jan 19, 2015
2 parents 8c6f3dd + e815b31 commit 8b4d29f
Show file tree
Hide file tree
Showing 26 changed files with 510 additions and 22 deletions.
1 change: 1 addition & 0 deletions app/assets/stylesheets/application.css.scss
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
@import 'compass';
@import 'font-awesome';
@import 'pure';
@import 'compass/utilities/general/clearfix';

@import 'normalize';
@import 'pygments_default';
Expand Down
29 changes: 29 additions & 0 deletions app/assets/stylesheets/users/profile.css.scss
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,34 @@ a.edit_link {
font-size: 0.7em;
}


.user_tags {
@include clearfix;
margin: 16px 4px;

.user_tag {
@include border-radius(16px);
float: left;
background-color: whitesmoke;
border: 1px solid silver;
padding: 2px 8px;
margin: 8px 4px;
}

form.tag_detach_form {
display: inline;
button {
position: relative;
top: -0.5em;
border: none;
background: none;
font-size: 50%;
margin: 0;
padding: 0;
}
}
}

.sidebar {
background: rgb(61, 79, 93);
color: #fff;
Expand Down Expand Up @@ -220,3 +248,4 @@ img.gravatar_icon {
white-space: pre-wrap;
font-family: Consolas,"Liberation Mono",Menlo,Courier,monospace;
}

6 changes: 6 additions & 0 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ class ApplicationController < ActionController::Base
]

before_action :configure_permitted_parameters, if: :devise_controller?
before_action :prepare_current_user

def configure_permitted_parameters
PERMITED_COLUMNS_FOR_USER.each do |column|
devise_parameter_sanitizer.for(:sign_up) << column
Expand All @@ -17,4 +19,8 @@ def configure_permitted_parameters
end

protect_from_forgery with: :exception

def prepare_current_user
@user = current_user if user_signed_in?
end
end
41 changes: 41 additions & 0 deletions app/controllers/user_tags_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
class UserTagsController < ApplicationController
TAGS_PER_PAGE = 100

before_action :authenticate_user!, only: [:attach, :detach]
before_action :set_target_user, only: [:attach, :detach]

layout 'users'

def attach
@user.tag_keyword(params[:name])
redirect_to home_path(@user.nick)
end

def detach
if view_context.myself?(@user)
tag = UserTag.find(params[:tag_id])
@user.detach(tag) if tag.present?
end
redirect_to home_path(@user.nick)
end

def index
@tags = UserTag.page(params[:page]).per(TAGS_PER_PAGE)
end

def show
@tag = UserTag.find(params[:id])
@members = @tag.users.page(params[:page]).per(UsersController::MEMBERS_PER_PAGE)
end

private

def set_target_user
@user = User.find_by(nick: params[:nick])
raise ActiveRecord::RecordNotFound unless @user
end

def user_tag_params
params.require(:user_tag).permit(:user_id, :name)
end
end
1 change: 0 additions & 1 deletion app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ def show
end

def list
@user = current_user
relation = case params[:sort]
when 'recent' then User.recent
when 'member_number' then User.order_by_member_number
Expand Down
12 changes: 11 additions & 1 deletion app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ class User < ActiveRecord::Base
format: /\A[a-zA-Z0-9_\-]+\Z/,
uniqueness: { case_sensitive: false },
length: { minimum: 3, maximum: 240 },
username_not_reserved: true
username_not_reserved: { additional_reserved_names: %w[foo bar] },
}

has_one :resume, class_name: 'UserResume'
has_many :user_taggings
has_many :tags, through: :user_taggings, class_name: 'UserTag'

devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :validatable, :omniauthable
Expand All @@ -20,4 +22,12 @@ class User < ActiveRecord::Base
scope :order_by_nick, -> { order(:nick) }
scope :recent, -> { order(updated_at: :desc) }
scope :order_by_member_number, -> { order(:member_number) }

def tag_keyword(keyword)
UserTag.retrieve(keyword).try(:attach, self)
end

def detach(tag)
user_taggings.find_by(user_tag_id: tag).try(:destroy)
end
end
27 changes: 27 additions & 0 deletions app/models/user_tag.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
class UserTag < ActiveRecord::Base
require 'nkf'

has_many :user_taggings
has_many :users, through: :user_taggings

def self.retrieve(name)
keyword = normalize_keyword(name)
return nil if keyword.blank?
create_with(name: keyword).find_or_create_by(search_hash: hash_keyword(name))
end

def self.normalize_keyword(keyword)
NKF.nkf('-w -X -Z', keyword.gsub(/[\s ]/, ''))
end

def self.hash_keyword(keyword)
Digest::SHA256.hexdigest(self.normalize_keyword(keyword).downcase)
end

def attach(user)
unless (user.tags.include?(self))
UserTagging.create(user_id: user.id, user_tag_id: self.id)
end
self
end
end
4 changes: 4 additions & 0 deletions app/models/user_tagging.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class UserTagging < ActiveRecord::Base
belongs_to :user
belongs_to :tag, class_name: 'UserTag', foreign_key: 'user_tag_id'
end
1 change: 1 addition & 0 deletions app/views/layouts/application.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
%head
%title Ena
= csrf_meta_tags
= javascript_include_tag 'application'
= yield :header
%body
= content_for?(:content) ? yield(:content) : yield
11 changes: 11 additions & 0 deletions app/views/user_tags/_tags.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
- user ||= user
%div.user_tags
- tags.each do |tag|
%div.user_tag
%i.fa.fa-tag.fa-lg
= link_to tag.name, user_tag_path(tag)
- if user.present? && myself?(user)
= form_tag(user_tag_detach_path(user.nick), method: :delete, class: "tag_detach_form", data: { confirm: "detach tag [#{tag.name}] from you. are yo sure?" }) do
= hidden_field_tag :tag_id, tag.id
= button_tag do
%i.fa.fa-remove.fa-lg
12 changes: 12 additions & 0 deletions app/views/user_tags/index.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
%div.content.pure-u-1.pure-u-md-3-4
%div
%div.posts
%h1.content-subhead
%section.post
%header.post-header
%h2
%i.fa.fa-tags.fa-lg
User Tags
%div.post-description
= render 'tags', tags: @tags
= paginate @tags
12 changes: 12 additions & 0 deletions app/views/user_tags/show.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
%div.content.pure-u-1.pure-u-md-3-4
%div
%div.posts
%h1.content-subhead
%section.post
%header.post-header
%h2
%i.fa.fa-tag.fa-lg
= @tag.name
%div.post-description
= render 'users/user_list', members: @members
= paginate @members
3 changes: 3 additions & 0 deletions app/views/users/_common_linkbar.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
= link_to users_list_path, class: 'pure-button' do
%i.fa.fa-users.fa-lg

= link_to user_tags_path, class: 'pure-button' do
%i.fa.fa-tags.fa-lg

- if user_signed_in?
= form_tag(destroy_user_session_path, method: :delete) do
%button.pure-button{ type: 'submit' }
Expand Down
17 changes: 17 additions & 0 deletions app/views/users/_user_list.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
%table.user_list.pure-table
%thead
%tr
%th icon
%th nick
%th name
%th member number
%th last update
%tbody
- members.each_with_index do |member, line|
%tr{ class: line.odd? ? 'pure-table-odd' : '' }
%td= gravatar_image_tag(member)
%td
= link_to member.nick, home_path(member.nick)
%td= member.name
%td= member.member_number
%td= member.updated_at
18 changes: 1 addition & 17 deletions app/views/users/list.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,5 @@
%h2
Member List
%div.post-description
%table.user_list.pure-table
%thead
%tr
%th icon
%th nick
%th name
%th member number
%th last update
%tbody
- @members.each_with_index do |member, line|
%tr{ class: line.odd? ? 'pure-table-odd' : '' }
%td= gravatar_image_tag(member)
%td
= link_to member.nick, home_path(member.nick)
%td= member.name
%td= member.member_number
%td= member.updated_at
= render 'user_list', members: @members
= paginate @members
16 changes: 16 additions & 0 deletions app/views/users/show.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,22 @@
Member Number
%td
= @user.member_number
%div.posts
%h1.content-subhead
%section.post
%header.post-header
%h2
%i.fa.fa-tags.fa-lg
Tags
%div.post-description
= render 'user_tags/tags', tags: @user.tags, user: @user
- if user_signed_in?
%div.attach_user_tag
= form_tag(user_tag_attach_path(@user.nick)) do
= text_field_tag :name
= button_tag class: 'pure-button' do
%i.fa.fa-tag.fa-lg
Attach
%div.posts
%h1.content-subhead
%section.post
Expand Down
2 changes: 1 addition & 1 deletion config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class Application < Rails::Application
config.autoload_paths += [
Rails.root.join('lib/autoload'),
]
config.assets.precompile += %w(*.png *.jpg *.jpeg *.gif *.css)
config.assets.precompile += %w(*.png *.jpg *.jpeg *.gif *.css *.js)
config.action_mailer.default_url_options = { host: 'localhost', port: 3000 }

config.generators do |g|
Expand Down
6 changes: 6 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,13 @@
resource :user_resumes, controller: '/user_resumes', as: :resume, path: :resume
end

resources :user_tags, only: [:index, :show]

scope ':nick' do
get '/', controller: :users, action: :show, as: :home
scope :user_tags, controller: :user_tags, as: :user_tag, path: :tag do
post :attach
delete :detach
end
end
end
21 changes: 21 additions & 0 deletions db/migrate/20150114120333_create_user_tag_and_tagging.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
class CreateUserTagAndTagging < ActiveRecord::Migration
def change
create_table :user_tags do |t|
t.string :name, null: false
t.string :search_hash, null: false

t.timestamps null: false
end

add_index :user_tags, :search_hash, unique: true

create_table :user_taggings do |t|
t.integer :user_id, null: false
t.integer :user_tag_id, null: false
end

add_index :user_taggings, :user_id
add_index :user_taggings, :user_tag_id
add_index :user_taggings, [:user_id, :user_tag_id], unique: true
end
end
20 changes: 19 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20150114033834) do
ActiveRecord::Schema.define(version: 20150114120333) do

create_table "user_resumes", force: :cascade do |t|
t.integer "user_id", null: false
Expand All @@ -22,6 +22,24 @@

add_index "user_resumes", ["user_id"], name: "index_user_resumes_on_user_id"

create_table "user_taggings", force: :cascade do |t|
t.integer "user_id", null: false
t.integer "user_tag_id", null: false
end

add_index "user_taggings", ["user_id", "user_tag_id"], name: "index_user_taggings_on_user_id_and_user_tag_id", unique: true
add_index "user_taggings", ["user_id"], name: "index_user_taggings_on_user_id"
add_index "user_taggings", ["user_tag_id"], name: "index_user_taggings_on_user_tag_id"

create_table "user_tags", force: :cascade do |t|
t.string "name", null: false
t.string "search_hash", null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end

add_index "user_tags", ["search_hash"], name: "index_user_tags_on_search_hash", unique: true

create_table "users", force: :cascade do |t|
t.string "name", null: false
t.string "email", null: false
Expand Down
Loading

0 comments on commit 8b4d29f

Please sign in to comment.