From 30096f6b0a08272040219dedba9aeaf53cdb0277 Mon Sep 17 00:00:00 2001 From: Matchu Date: Sun, 31 Jul 2011 02:48:16 -0400 Subject: [PATCH] items petpage export --- app/controllers/closet_hangers_controller.rb | 58 ++++++----- app/helpers/closet_hangers_helper.rb | 20 ++++ app/models/closet_list.rb | 1 + app/stylesheets/closet_hangers/_petpage.sass | 12 +++ app/stylesheets/screen.sass | 1 + .../closet_hangers/_petpage_content.html.haml | 97 +++++++++++++++++++ .../closet_hangers/_petpage_hanger.html.haml | 6 ++ app/views/closet_hangers/petpage.html.haml | 22 +++++ config/routes.rb | 8 +- public/stylesheets/compiled/screen.css | 26 +++++ spec/views/petpages/new.html.erb_spec.rb | 5 + 11 files changed, 233 insertions(+), 23 deletions(-) create mode 100644 app/stylesheets/closet_hangers/_petpage.sass create mode 100644 app/views/closet_hangers/_petpage_content.html.haml create mode 100644 app/views/closet_hangers/_petpage_hanger.html.haml create mode 100644 app/views/closet_hangers/petpage.html.haml create mode 100644 spec/views/petpages/new.html.erb_spec.rb diff --git a/app/controllers/closet_hangers_controller.rb b/app/controllers/closet_hangers_controller.rb index 7f8470cd..56c7ea33 100644 --- a/app/controllers/closet_hangers_controller.rb +++ b/app/controllers/closet_hangers_controller.rb @@ -1,6 +1,7 @@ class ClosetHangersController < ApplicationController - before_filter :authorize_user!, :only => [:destroy, :create, :update] + before_filter :authorize_user!, :only => [:destroy, :create, :update, :petpage] before_filter :find_item, :only => [:destroy, :create, :update] + before_filter :find_user, :only => [:index, :petpage] def destroy raise ActiveRecord::RecordNotFound unless params[:closet_hanger] @@ -13,29 +14,9 @@ class ClosetHangersController < ApplicationController end def index - @user = User.find params[:user_id] @public_perspective = params.has_key?(:public) || !user_is?(@user) - @perspective_user = current_user unless @public_perspective - @closet_lists_by_owned = @user.closet_lists. - alphabetical.includes(:hangers => :item) - unless @perspective_user == @user - # If we run this when the user matches, we'll end up with effectively: - # WHERE belongs_to_user AND (is_public OR belongs_to_user) - # and it's a bit silly to put the SQL server through a condition that's - # always true. - @closet_lists_by_owned = @closet_lists_by_owned.visible_to(@perspective_user) - end - @closet_lists_by_owned = @closet_lists_by_owned.group_by(&:hangers_owned) - - visible_groups = @user.closet_hangers_groups_visible_to(@perspective_user) - unless visible_groups.empty? - @unlisted_closet_hangers_by_owned = @user.closet_hangers.unlisted. - owned_before_wanted.alphabetical_by_item_name.includes(:item). - where(:owned => [visible_groups]).group_by(&:owned) - else - @unlisted_closet_hangers_by_owned = {} - end + find_closet_hangers! if @public_perspective && user_signed_in? items = [] @@ -53,6 +34,11 @@ class ClosetHangersController < ApplicationController end end + def petpage + @public_perspective = true + find_closet_hangers! + end + # Since the user does not care about the idea of a hanger, but rather the # quantity of an item they own, the user would expect a create form to work # even after the record already exists, and an update form to work even after @@ -108,6 +94,34 @@ class ClosetHangersController < ApplicationController @item = Item.find params[:item_id] end + def find_user + @user = User.find params[:user_id] + end + + def find_closet_hangers! + @perspective_user = current_user unless @public_perspective + + @closet_lists_by_owned = @user.closet_lists. + alphabetical.includes(:hangers => :item) + unless @perspective_user == @user + # If we run this when the user matches, we'll end up with effectively: + # WHERE belongs_to_user AND (is_public OR belongs_to_user) + # and it's a bit silly to put the SQL server through a condition that's + # always true. + @closet_lists_by_owned = @closet_lists_by_owned.visible_to(@perspective_user) + end + @closet_lists_by_owned = @closet_lists_by_owned.group_by(&:hangers_owned) + + visible_groups = @user.closet_hangers_groups_visible_to(@perspective_user) + unless visible_groups.empty? + @unlisted_closet_hangers_by_owned = @user.closet_hangers.unlisted. + owned_before_wanted.alphabetical_by_item_name.includes(:item). + where(:owned => [visible_groups]).group_by(&:owned) + else + @unlisted_closet_hangers_by_owned = {} + end + end + def owned owned = true if params[:closet_hanger] diff --git a/app/helpers/closet_hangers_helper.rb b/app/helpers/closet_hangers_helper.rb index 81aa3577..71706cde 100644 --- a/app/helpers/closet_hangers_helper.rb +++ b/app/helpers/closet_hangers_helper.rb @@ -61,10 +61,30 @@ module ClosetHangersHelper link_to(content, path, options) end + def nc_icon_url + "http://#{request.host}#{image_path 'nc.png'}" + end + + def petpage_item_name(item) + item.name.gsub(/ on/i, ' on') + end + def public_perspective? @public_perspective end + PETPAGE_HANGER_BATCH_SIZE = 5 + def render_batched_petpage_hangers(hangers) + output do |html| + hangers.in_groups_of(PETPAGE_HANGER_BATCH_SIZE) do |batch| + content = batch.map do |hanger| + render 'petpage_hanger', :hanger => hanger if hanger + end.join.html_safe + html << content_tag(:div, content, :class => 'dti-item-row') + end + end + end + def render_closet_lists(lists) if lists render :partial => 'closet_lists/closet_list', :collection => lists, diff --git a/app/models/closet_list.rb b/app/models/closet_list.rb index 10358be3..57722726 100644 --- a/app/models/closet_list.rb +++ b/app/models/closet_list.rb @@ -10,6 +10,7 @@ class ClosetList < ActiveRecord::Base validates :hangers_owned, :inclusion => {:in => [true, false], :message => "can't be blank"} scope :alphabetical, order(:name) + scope :public, where(arel_table[:visibility].gteq(ClosetVisibility[:public].id)) scope :visible_to, lambda { |user| condition = arel_table[:visibility].gteq(ClosetVisibility[:public].id) condition = condition.or(arel_table[:user_id].eq(user.id)) if user diff --git a/app/stylesheets/closet_hangers/_petpage.sass b/app/stylesheets/closet_hangers/_petpage.sass new file mode 100644 index 00000000..5a8a51a8 --- /dev/null +++ b/app/stylesheets/closet_hangers/_petpage.sass @@ -0,0 +1,12 @@ +body.closet_hangers-petpage + +secondary-nav + + #intro + clear: both + + #petpage-output + display: block + height: 30em + margin: 0 auto + width: 50% + diff --git a/app/stylesheets/screen.sass b/app/stylesheets/screen.sass index df1111c4..c8db393f 100644 --- a/app/stylesheets/screen.sass +++ b/app/stylesheets/screen.sass @@ -7,6 +7,7 @@ @import partials/jquery.jgrowl @import closet_hangers/index +@import closet_hangers/petpage @import closet_lists/form @import closet_pages/new @import contributions/index diff --git a/app/views/closet_hangers/_petpage_content.html.haml b/app/views/closet_hangers/_petpage_content.html.haml new file mode 100644 index 00000000..4274acc0 --- /dev/null +++ b/app/views/closet_hangers/_petpage_content.html.haml @@ -0,0 +1,97 @@ +%style{:type => 'text/css'} + :plain + .dti-item-group, .dti-item-list, .dti-unlisted-items { + margin: 0; + padding: 0; + } + + .dti-item-group-header, .dti-item-group, #dti-item-footer { + color: #040; + font-family: Helvetica, Arial, Verdana, sans-serif; + line-height: 1.5; + margin: 0 auto; + text-align: center; + width: 560px; + } + + .dti-item-group-header { + border-top: 1px solid #060; + font-size: 175%; + margin-top: 1em; + } + + .dti-item-group-header, .dti-item-list { + border-bottom: 1px solid #ADA; + } + + .dti-item-group { + list-style: none; + } + + .dti-item-list h3 { + font-size: 150%; + } + + .dti-items { + border-spacing: 10px; + display: table; + } + + .dti-item-row { + display: table-row; + } + + .dti-item { + display: table-cell; + margin: 0 10px; + padding: 8px; + text-align: right; + width: 84px; + } + + .dti-item-thumbnail { + height: 80px; + width: 80px; + } + + .dti-item span { + display: block; + text-align: center; + } + + .dti-item-nc { + margin-top: -16px; + } + + .dti-unlisted-items h3 { + font-style: italic; + } + + #dti-item-footer { + font-size: 85%; + margin-top: 2em; + } + +- [true, false].each do |owned| + - lists = lists_by_owned[owned] + - if lists || unlisted_hangers_by_owned[owned] + %h2.dti-item-group-header Items #OWNER #{ClosetHanger.verb(:someone, owned)} + %ul.dti-item-group + - lists.each do |list| + - unless list.hangers.empty? + %li.dti-item-list + %h3= list.name + - if list.description? + .dti-item-list-desc + = closet_list_description_format list + %div.dti-items + = render_batched_petpage_hangers(list.hangers.sort { |a,b| a.item.name <=> b.item.name }) + %li.dti-unlisted-items + - unless lists.empty? + %h3 (Not in a list) + %div + = render_batched_petpage_hangers(unlisted_hangers_by_owned[owned]) + +#dti-item-footer + I made this list on Dress to Impress. You can, too! + diff --git a/app/views/closet_hangers/_petpage_hanger.html.haml b/app/views/closet_hangers/_petpage_hanger.html.haml new file mode 100644 index 00000000..e3d69b20 --- /dev/null +++ b/app/views/closet_hangers/_petpage_hanger.html.haml @@ -0,0 +1,6 @@ +%div.dti-item + = image_tag hanger.item.thumbnail_url, :alt => nil, :class => 'dti-item-thumbnail' + - if hanger.item.nc? + = image_tag nc_icon_url, :alt => 'NC', :class => 'dti-item-nc', :title => 'This is an NC Mall item' + %span= petpage_item_name hanger.item + diff --git a/app/views/closet_hangers/petpage.html.haml b/app/views/closet_hangers/petpage.html.haml new file mode 100644 index 00000000..6bf770d8 --- /dev/null +++ b/app/views/closet_hangers/petpage.html.haml @@ -0,0 +1,22 @@ +- title 'Export to Petpage' +- secondary_nav do + = link_to 'Back to Your Items', user_closet_hangers_path(current_user), :class => 'button' + +#intro + %p + We took your public lists and created a nice, simple HTML file for your + Neopet's petpage. By default it's styled as a table, but it doesn't have to + be. The HTML is flexible, so, if you're the artsy type, you're free to mess + with the styles all you want! + + %p + Copy the HTML from the box below, then paste it into + = succeed '.' do + = link_to "your pet's page", 'http://www.neopets.com/edithomepage.phtml' + Then head to the Neoboards to show off! Have fun! + +%textarea#petpage-output + = '' + render('petpage_content', + :lists_by_owned => @closet_lists_by_owned, + :unlisted_hangers_by_owned => @unlisted_closet_hangers_by_owned) + diff --git a/config/routes.rb b/config/routes.rb index 49cf66c8..b54352b7 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,4 +1,6 @@ OpenneoImpressItems::Application.routes.draw do |map| + get "petpages/new" + get "closet_lists/new" get "closet_lists/create" @@ -41,7 +43,11 @@ OpenneoImpressItems::Application.routes.draw do |map| resources :users, :path => 'user', :only => [:update] do resources :contributions, :only => [:index] - resources :closet_hangers, :only => [:index], :path => 'closet' + resources :closet_hangers, :only => [:index], :path => 'closet' do + collection do + get :petpage + end + end resources :closet_lists, :only => [:new, :create, :edit, :update, :destroy], :path => 'closet/lists' resources :items, :only => [] do diff --git a/public/stylesheets/compiled/screen.css b/public/stylesheets/compiled/screen.css index 36cf32db..83444aae 100644 --- a/public/stylesheets/compiled/screen.css +++ b/public/stylesheets/compiled/screen.css @@ -1152,6 +1152,32 @@ body.closet_hangers-index.current-user.js .closet-hangers-group.hidden header .s display: block; } +/* line 2, ../../../app/stylesheets/partials/_secondary_nav.sass */ +body.closet_hangers-petpage #title { + float: left; + margin-right: 0.5em; +} +/* line 6, ../../../app/stylesheets/partials/_secondary_nav.sass */ +body.closet_hangers-petpage .flash { + clear: both; +} +/* line 9, ../../../app/stylesheets/partials/_secondary_nav.sass */ +body.closet_hangers-petpage #secondary-nav { + display: block; + margin-top: 0.75em; +} +/* line 4, ../../../app/stylesheets/closet_hangers/_petpage.sass */ +body.closet_hangers-petpage #intro { + clear: both; +} +/* line 7, ../../../app/stylesheets/closet_hangers/_petpage.sass */ +body.closet_hangers-petpage #petpage-output { + display: block; + height: 30em; + margin: 0 auto; + width: 50%; +} + /* line 2, ../../../app/stylesheets/partials/_secondary_nav.sass */ body.closet_lists-new #title, body.closet_lists-create #title, body.closet_lists-edit #title, body.closet_lists-update #title { float: left; diff --git a/spec/views/petpages/new.html.erb_spec.rb b/spec/views/petpages/new.html.erb_spec.rb new file mode 100644 index 00000000..b9434a86 --- /dev/null +++ b/spec/views/petpages/new.html.erb_spec.rb @@ -0,0 +1,5 @@ +require 'spec_helper' + +describe "petpages/new.html.erb" do + pending "add some examples to (or delete) #{__FILE__}" +end