Skip to content

Commit

Permalink
#37 사용자 사진목록 페이지에서 yogy 태그들 출력
Browse files Browse the repository at this point in the history
  • Loading branch information
iam312 committed Jul 29, 2015
1 parent 7eea0e7 commit 50587a1
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
2 changes: 2 additions & 0 deletions app/controllers/images_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,8 @@ def ajax_cancel_dislike

def filter_by_user
@images = Image.filter_by_user( params[:user_id], 0, 100 )
@yogies_string = Image.generate_yogies_with_images( @images ).map{ |v| "##{v}" }.join(",")
@user = User.find params[:user_id]
end


Expand Down
16 changes: 15 additions & 1 deletion app/models/image.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,26 @@ class Image < ActiveRecord::Base
scope :get_next_image_id, ->(id) { where( ["id > ?", id] ).order( 'id asc' ).limit(1) }
scope :all_images, ->(offset, limit) { available_images.order('created_at desc').offset(offset).limit(limit) }

def yogies_to_array
yogies.split('#').drop(1).map { |item| item[/[[:word:] ]+/].strip.gsub(/\s+/, '_') }
end

def self.generate_yogies_with_images( images )
yogies = {}
images.each do |image|
image.yogies_to_array.each do |yogy|
yogies.has_key?(yogy) ? yogies[yogy] += 1 : yogies[yogy] = 1
end
end
yogies.sort_by{ |k, v| v }.reverse.map{ |v| v[0] }
end

def process!( current_user )
yogy_ids = []
image = Image.find_by id: id
image.transaction do
hash_tags = yogies.split('#').drop(1).map { |item| item[/[[:word:] ]+/].strip.gsub(/\s+/, '_') }
#hash_tags = yogies.split('#').drop(1).map { |item| item[/[[:word:] ]+/].strip.gsub(/\s+/, '_') }
hash_tags = yogies_to_array
hash_tags.each do |hash_tag|
yogy = Yogies.new
yogy.title = hash_tag
Expand Down
10 changes: 10 additions & 0 deletions app/views/images/filter_by_user.html.erb
Original file line number Diff line number Diff line change
@@ -1 +1,11 @@
<div class="panel panel-default">
<div class="panel-body">
<h3> <%= @user.name %>
<small>
님이 찍은 <%= raw link_yogies(@yogies_string) %> 사진입니다
</small>
</h3>
</div>
</div>

<%= render partial: 'list', locals: {images: @images} %>

0 comments on commit 50587a1

Please sign in to comment.