Skip to content

Commit

Permalink
basic image uploading, some caching
Browse files Browse the repository at this point in the history
  • Loading branch information
jywarren committed Mar 28, 2013
1 parent dd087cb commit 3e7c1aa
Show file tree
Hide file tree
Showing 16 changed files with 160 additions and 18 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ gem 'geokit-rails3'
gem 'spatial_adapter'
gem 'rails_autolink'
gem 'rb-readline'
gem "paperclip"

gem "nifty-generators", :group => :development
gem "ruby-openid", :require => "openid"
Expand Down
9 changes: 5 additions & 4 deletions app/controllers/home_controller.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
class HomeController < ApplicationController

before_filter :local
#caches_action :index, :cache_path => proc { |c|
# node = DrupalNode.find :last #c.params[:id]
# { :n => node.updated_at.to_i }
#end

def local

end
#caches_action :index, :cache_path => { :last => DrupalNode.find(:last).updated_at.to_i }

def index
@title = "Home"
Expand Down
60 changes: 60 additions & 0 deletions app/controllers/images_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
class ImagesController < ApplicationController

def create
if current_user && current_user.username == "warren"
@image = Image.new({
:uid => current_user.uid,
:photo => params[:image][:photo],
:title => params[:image][:title],
:notes => params[:image][:notes]
})
@image.nid = DrupalNode.find params[:nid] if params[:nid]
if @image.save!
flash[:notice] = "Image saved."
else
flash[:error] = "The image could not be saved."
end
redirect_to "/profile/"+current_user.username
else
prompt_login "You must be logged in to upload."
end
end

def new
@image = Image.new()
end

def update
if current_user && current_user.username == "warren"
@image = Image.find params[:id]
# make changes
if @image.save
flash[:notice] = "Image updated."
else
flash[:error] = "The image could not be updated."
end
redirect_to "/profile/"+@image.user.username
else
prompt_login "You must be logged in to edit images."
end
end

def delete
if current_user && current_user.username == "warren"
@image = Image.find params[:id]
if @image.uid == current_user.uid # or current_user.role == "admin"
if @image.delete
flash[:notice] = "Image deleted."
else
flash[:error] = "The image could not be deleted."
end
redirect_to "/profile/"+@image.user.username
else
prompt_login "Only the owner can delete this image."
end
else
prompt_login "You must be logged in to delete images."
end
end

end
2 changes: 1 addition & 1 deletion app/models/drupal_comment.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
class DrupalComment < ActiveRecord::Base
belongs_to :drupal_node, :foreign_key => 'nid'
belongs_to :drupal_node, :foreign_key => 'nid', :touch => true
has_one :drupal_users, :foreign_key => 'uid'

validates :comment, :presence => true
Expand Down
5 changes: 4 additions & 1 deletion app/models/drupal_node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class DrupalNode < ActiveRecord::Base
has_many :drupal_content_type_map, :foreign_key => 'nid'
has_many :drupal_content_field_bboxes, :foreign_key => 'nid'
has_many :drupal_content_field_image_gallery, :foreign_key => 'nid'
has_many :images, :foreign_key => :nid

validates :title, :presence => :true
#validates :name, :format => {:with => /^[\w-]*$/, :message => "can only include letters, numbers, and dashes"}
Expand Down Expand Up @@ -126,7 +127,9 @@ def has_tag(tag)
end

def mailing_list
RSS::Parser.parse(open('https://groups.google.com/group/'+self.power_tag('list')+'/feed/rss_v2_0_topics.xml').read, false).items
Rails.cache.fetch("feed-"+self.id.to_s+"-"+(self.updated_at.to_i/300).to_i.to_s) do
RSS::Parser.parse(open('https://groups.google.com/group/'+self.power_tag('list')+'/feed/rss_v2_0_topics.xml').read, false).items
end
end

def icon
Expand Down
11 changes: 5 additions & 6 deletions app/models/drupal_tag.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,11 @@ def self.find_nodes_by_type_with_all_tags(tagnames,type = "note",limit = 10)
DrupalNode.find nids, :order => "nid DESC", :limit => limit
end

def self.find_popular_notes(tag,limit = 8)
nodes = []
self.find_by_name(tag).drupal_node.filter_by_type('note',limit).each do |node|
nodes << node if node.totalcount > 20
end
nodes.uniq.sort{|a,b| b.created <=> a.created}
def self.find_popular_notes(tag,views = 20,limit = 10)
tids = DrupalTag.find(:all, :conditions => {:name => tag}).collect(&:tid)
nids = DrupalNodeCommunityTag.find(:all, :conditions => ["tid IN (?)",tids]).collect(&:nid)
nids += DrupalNodeTag.find(:all, :conditions => ["tid IN (?)",tids]).collect(&:nid)
DrupalNode.find_all_by_type "note", :conditions => ["node.nid in (?) AND node_counter.totalcount > (?)",nids.uniq,views], :order => "changed DESC", :limit => limit, :include => :drupal_node_counter
end

end
30 changes: 30 additions & 0 deletions app/models/image.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
class Image < ActiveRecord::Base
attr_accessible :uid, :notes, :title, :photo, :nid

#has_many :comments, :dependent => :destroy
#has_many :likes, :dependent => :destroy
#has_many :tags, :dependent => :destroy
has_one :user, :foreign_key => :uid
has_one :node, :foreign_key => :nid

has_attached_file :photo, :styles => { :small => "150x150>", :medium => "500x375>", :large => "800x600>" },
:url => "/assets/products/:id/:style/:basename.:extension",
:path => ":rails_root/public/assets/products/:id/:style/:basename.:extension"

validates :uid, :presence => :true
validates :photo, :presence => :true
validates :title, :presence => :true, :format => {:with => /\A[a-zA-Z0-9\ -_]+\z/, :message => "Only letters, numbers, and spaces allowed"}, :length => { :maximum => 60 }

# Paperclip
has_attached_file :photo,
:styles => {
:thumb=> "300x100!",
:large => "800x200!" }

has_attached_file :baseline,
:styles => {
:thumb=> "300x100!",
:large => "800x200!" }


end
2 changes: 2 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ class User < ActiveRecord::Base
:email]
end

has_many :images, :foreign_key => :uid

def uid
DrupalUsers.find_by_name(self.username).uid
end
Expand Down
2 changes: 0 additions & 2 deletions app/views/home/_editor.html.erb
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
<script src="/assets/editor.js" type="text/javascript"></script>

<div class="btn-toolbar">
<div class="btn-group">
<a class="btn btn-small" onClick="$E.bold()"><i class="icon-bold"></i></a>
Expand Down
5 changes: 4 additions & 1 deletion app/views/home/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,10 @@
'near-infrared-camera',
'thermal-photography'].each_with_index do |tagname,index| %>
<div class="tab-pane<%= ' active' if index == 0 %>" id="<%= tagname %>">
<%= render :partial => "notes/tagged_notes", :locals => {:nodes => DrupalTag.find_popular_notes(tagname,4)} %>
<% @nodes = DrupalTag.find_popular_notes(tagname,20,4) %>
<% cache "front-"+tagname+"-"+@nodes.first.id.to_s+"-"+@nodes.first.updated_at.to_i.to_s do %>
<%= render :partial => "notes/tagged_notes", :locals => {:nodes => @nodes} %>
<% end %>
<p><a href="/tag/<%= tagname %>">More "<%= tagname %>" research &raquo;</a></p>
</div>
<% end %>
Expand Down
12 changes: 12 additions & 0 deletions app/views/images/new.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<h2>Upload an image</h2>

<%= form_for @image, :html => { :multipart => true } do |f| %>
<%= f.error_messages %>

<%= f.file_field :photo %>

<p><%= f.text_field :title, {:placeholder => "Image title", :tabindex => 2} %></p>
<p><%= f.text_field :notes, {:class => "span12", :placeholder => "a short description"} %></p>

<p><button class="btn-large btn-primary" type="submit">Upload</button></p>
<% end %>
2 changes: 1 addition & 1 deletion app/views/like/_like.html.erb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<div class="btn-toolbar pull-right" style="margin-left:10px;margin-top:0;">
<div class="btn-group">
<a class="btn btn-small" rel="popover" data-content="<%= "No tags" if @tagnames.length == 0 %><% if @tagnames %><% @tagnames.each do |tag| %><a class='label label-inverse'><%= tag %></a> <% end %><% end %><hr /><a class='btn btn-small'><i class='icon-user'></i> <%= @node.author.name %></a>" title="Follow by tag or author" data-placement="bottom"><b class="caret"></b></a>
<a class="btn btn-small" rel="popover" data-content="<%= "No tags" if @tagnames.nil? || @tagnames.length == 0 %><% if @tagnames %><% @tagnames.each do |tag| %><a class='label label-inverse'><%= tag %></a> <% end %><% end %><hr /><a class='btn btn-small'><i class='icon-user'></i> <%= @node.author.name %></a>" title="Follow by tag or author" data-placement="bottom"><b class="caret"></b></a>
<a rel="tooltip" title="Helpful? Awesome?" class="btn btn-small" href="#"><i class="icon-star"></i><span class="hidden-phone"> Like (8)</span></a>
<a rel="tooltip" title="Updates/comments" id="follow" class="btn btn-small" href="#"><i class="icon-eye-open"></i><span class="hidden-phone"> Follow</span></a>
</div>
Expand Down
10 changes: 10 additions & 0 deletions app/views/users/profile.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,16 @@

<p><%= raw RDiscount.new(@user.bio).to_html %></p>

<h3>Recent images</h3>

<p>Temporary display of images as part of the file upload feature.</p>

<% if current_user %>
<% current_user.images.each do |image| %>
<%= image_tag image.photo.url(:thumb) %>
<% end %>
<% end %>

<hr />

<div id="donut"></div>
Expand Down
5 changes: 3 additions & 2 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
Plots2::Application.routes.draw do
resources :rusers
resources :users
resources :user_sessions
resources :images

# The priority is based upon order of creation:
# first created -> highest priority.
Expand All @@ -13,8 +16,6 @@
match 'register' => 'users#create'
match 'users/list' => 'users#list'
match 'signup' => 'users#new'
resources :users
resources :user_sessions

match 'wiki/:id' => 'wiki#show'
match 'wiki/revisions/:id' => 'wiki#revisions'
Expand Down
22 changes: 22 additions & 0 deletions db/migrate/20130327222827_create_images.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
class CreateImages < ActiveRecord::Migration
def up
create_table :images do |t|
t.string :title
t.integer :uid
t.integer :nid
t.string :notes
t.integer :version, :default => 0

# attachment (paperclip)
t.string :photo_file_name
t.string :photo_content_type
t.string :photo_file_size

t.timestamps
end
end

def down
drop_table :images
end
end
Empty file added public/system/PLACEHOLDER
Empty file.

0 comments on commit 3e7c1aa

Please sign in to comment.