Permalink
Browse files

Most of Tags working I think. Spotted nilClass error when no Favorite…

… view page.
  • Loading branch information...
1 parent ee94fd2 commit 1645e202bd713351dff5544fa9e6f77681ab0d28 @robertmilner committed Mar 17, 2012
@@ -64,8 +64,8 @@ def create
def update
@user = @current_user
@excuse = Excuse.find(params[:id])
- @excuse.location = @user.location
- favorites = @excuse.favorites.build
+ # @excuse.location = @user.location
+ # favorites = @excuse.favorites.build
respond_to do |format|
if @excuse.update_attributes(params[:excuse])
@@ -15,10 +15,12 @@ def search
def location
# arguments [0] = params[:search], [1] = total number of results
- @excuses = Excuse.location(params[:search], 5)
+ @excuses = Excuse.search_location(params[:search], 5)
end
def tag
+ # @excuses = Excuse.search_tag(params[:search], 5)
+ @excuses = Excuse.limit(5)
end
def favorite
View
@@ -4,15 +4,25 @@ class Excuse < ActiveRecord::Base
belongs_to :user
belongs_to :location
has_many :favorites, :as => :favorable
+ has_many :taggings, :dependent => :destroy
+ has_many :tags, :through => :taggings
+
+ # attributes
+ attr_writer :tag_names
# validations
validates_presence_of :text
validates_length_of :text, :within => 3..255
# callbacks
-
+ after_save :assign_tags
# methods
+
+ def tag_names
+ @tag_names || tags.map(&:name).join(' ')
+ end
+
# pages_contoller#excuse
def self.random(total)
self.find(:all, :order => 'random()', :limit => total)
@@ -35,7 +45,7 @@ def self.search(search, total)
end
# pages_contoller#location
- def self.location(search, total)
+ def self.search_location(search, total)
if search
all( :joins => { :location => { } },
:conditions => { :locations => { :name => search } },
@@ -51,4 +61,22 @@ def self.location(search, total)
end
end
+ def self.search_tag(search, total)
+ if search
+ all( :joins => { :tags => {} },
+ :conditions => { :tags => { :name => search } },
+ :limit => total,
+ :order => 'random()' )
+ end
+ end
+
+ # pages_contoller#tag
+ def assign_tags
+ if @tag_names
+ self.tags = @tag_names.split.map do |name|
+ Tag.find_or_create_by_name(name)
+ end
+ end
+ end
+
end
@@ -7,4 +7,7 @@
%li= f.label :text
%li= f.text_field :text
+ %li= f.label :tag_names, "Tags"
+ %li= f.text_field :tag_names
+
%li= f.submit 'Save'
@@ -5,6 +5,7 @@
%th Text
%th User
%th Location
+ %th Tags
%th
%th
%th
@@ -13,7 +14,8 @@
%tr
%td= excuse.text
%td= excuse.user.name
- %td= excuse.location.name unless excuse.location.nil?
+ %td= excuse.location.name if excuse.location.present?
+ %td= excuse.tag_names
%td= link_to 'Show', excuse
%td= link_to 'Edit', edit_excuse_path(excuse)
%td= link_to 'Destroy', excuse, :confirm => 'Are you sure?', :method => :delete
@@ -4,7 +4,6 @@
- if session[:user_id]
%ul
%li= link_to "Create excuse", new_excuse_path
- %li= link_to "Logout", logout_path
- else
%ul
%li= link_to "Sign up", new_user_path
@@ -3,8 +3,13 @@
- if notice
= render 'layouts/notice', :notice => notice, :type => "info"
-= render 'search_box', :type => 'tag', :placeholder => 'Search by tags'
+= render 'search_box', form_link: app_tag_path, placeholder: 'Search by tags'
-%ul
-- @tags do |tag|
- %li= tag
+- if @excuses.count > 0
+ %ul
+ - @excuses.each do |excuse|
+ %li= excuse.text
+ %li= excuse.tag_names
+- else
+ %ul
+ %li Can you believe it? There are no excuses with this tag... Yet. Why don't you create one?
View
@@ -11,7 +11,7 @@
#
# It's strongly recommended to check this file into your version control system.
-ActiveRecord::Schema.define(:version => 20120316034034) do
+ActiveRecord::Schema.define(:version => 20120317163709) do
create_table "excuses", :force => true do |t|
t.string "text"
@@ -45,6 +45,19 @@
t.datetime "updated_at", :null => false
end
+ create_table "taggings", :force => true do |t|
+ t.integer "tag_id"
+ t.integer "excuse_id"
+ t.datetime "created_at", :null => false
+ t.datetime "updated_at", :null => false
+ end
+
+ create_table "tags", :force => true do |t|
+ t.string "name"
+ t.datetime "created_at", :null => false
+ t.datetime "updated_at", :null => false
+ end
+
create_table "users", :force => true do |t|
t.string "name"
t.string "email"
View
@@ -1,6 +1,15 @@
-User.destroy_all
-Location.destroy_all
+
+puts "Start time: #{Time.now}"
+
+# Destroy all records in the database.
Excuse.destroy_all
+Favorite.destroy_all
+Location.destroy_all
+Search.destroy_all
+Tag.destroy_all
+Tagging.destroy_all
+User.destroy_all
+
# Create an array of locations that I actually know of.
# Limit to low amount so we can see location_id being repeated
@@ -17,7 +26,7 @@
u.save!
10.times do
- u.excuses.create text: Faker::Company.bs, location_id: u.location.id
+ u.excuses.create text: Faker::Lorem.sentence(word_count = rand(6..10)), location_id: u.location.id, tag_names: Faker::Lorem.words(num = rand(2..6)).join(' ')
end
end
user.save!
@@ -33,12 +42,19 @@
u.save!
10.times do
- u.excuses.create text: Faker::Company.bs, location_id: u.location.id
+ u.excuses.create text: Faker::Lorem.sentence(word_count = rand(6..10)), location_id: u.location.id, tag_names: Faker::Lorem.words(num = rand(2..6)).join(' ')
end
end
end
user.save!
-puts "#{User.count} users created"
+puts "End time: #{Time.now}"
+
+# What did we create?
+puts "#{Excuse.count} excuses created"
+puts "#{Favorite.count} favorites created"
puts "#{Location.count} locations created"
-puts "#{Excuse.count} excuses created"
+puts "#{Search.count} searches created"
+puts "#{Tag.count} tags created"
+puts "#{Tagging.count} taggings created"
+puts "#{User.count} users created"

0 comments on commit 1645e20

Please sign in to comment.