Skip to content
Browse files

added file attachment

  • Loading branch information...
1 parent b7e91ec commit 78a719bc00038f14a9113ec63556045b5368eb3c @viatcheslavmogilevsky committed Apr 2, 2012
View
1 .gitignore
@@ -13,3 +13,4 @@
# Ignore all logfiles and tempfiles.
/log/*.log
/tmp
+/public/system
View
1 Gemfile
@@ -12,6 +12,7 @@ gem 'pg'
gem 'rest_in_place'
gem 'tinymce-rails'
gem 'kaminari'
+gem 'paperclip', '~> 3.0'
# Gems used only for assets and not required
# in production environments by default.
group :assets do
View
8 Gemfile.lock
@@ -31,6 +31,7 @@ GEM
arel (3.0.2)
bcrypt-ruby (3.0.1)
builder (3.0.0)
+ cocaine (0.2.1)
coffee-rails (3.2.2)
coffee-script (>= 2.2.0)
railties (~> 3.2.0)
@@ -73,6 +74,12 @@ GEM
mime-types (1.17.2)
multi_json (1.1.0)
orm_adapter (0.0.6)
+ paperclip (3.0.1)
+ activemodel (>= 3.0.0)
+ activerecord (>= 3.0.0)
+ activesupport (>= 3.0.0)
+ cocaine (>= 0.0.2)
+ mime-types
pg (0.13.2)
polyglot (0.3.3)
rack (1.4.1)
@@ -152,6 +159,7 @@ DEPENDENCIES
faker
jquery-rails
kaminari
+ paperclip (~> 3.0)
pg
rails (= 3.2.1)
rest_in_place
View
21 app/models/event.rb
@@ -1,9 +1,26 @@
class Event < ActiveRecord::Base
belongs_to :user
- attr_accessible :calendar_date, :content, :repeat
+ attr_accessible :calendar_date, :content, :repeat, :picture
validates :content, :presence => true
- validates :calendar_date, :presence => true
+ validates :calendar_date, :presence => true, :unless => :every_day?
validates :user_id, :presence => true
+ before_create :change_date
paginates_per 10
scope :all_events, order("repeat DESC, calendar_date ASC")
+ has_attached_file :picture
+ protected
+ def change_date
+ case repeat
+ when 3
+ self.calendar_date = Date.new(2012,3,25)+self.calendar_date.cwday
+ when 2
+ self.calendar_date = Date.new(2012,3,self.calendar_date.day)
+ when 1
+ self.calendar_date = Date.new(2012,self.calendar_date.month,self.calendar_date.day)
+ end
+ end
+
+ def every_day?
+ repeat == 4
+ end
end
View
2 app/views/events/_event.html.erb
@@ -6,11 +6,13 @@
<div id="<%= dom_id event %>">
<%= link_to edit_event_path(event) do %>
<span class="event_content"><%= raw event.content %></span>
+ <%= image_tag event.picture.url if event.picture_file_name %><br/>
<% end %>
<%= link_to 'remove', event, :method => :delete, :remote => true %>
</div>
<% else %>
<span class="event_content"><%= raw(event.content) %></span>
+ <%= image_tag event.picture.url if event.picture_file_name %>
<% end %>
</td>
</tr>
View
4 app/views/shared/_event_form.html.erb
@@ -6,6 +6,10 @@
<%= f.text_area :content, :rows => 10 %>
</div>
<div class="field">
+ <label>Picture:</label>
+ <%= f.file_field :picture %>
+ </div>
+ <div class="field">
<label>Date:</label>
<%= f.text_field :calendar_date %>
</div>
View
1 config/environments/development.rb
@@ -34,4 +34,5 @@
config.assets.compress = false
# Expands the lines which load the assets
config.assets.debug = true
+ Paperclip.options[:command_path] = 'usr/bin'
end
View
5 db/migrate/20120330155929_add_index_to_events.rb
@@ -0,0 +1,5 @@
+class AddIndexToEvents < ActiveRecord::Migration
+ def change
+ add_index :events, :repeat
+ end
+end
View
7 db/migrate/20120401115445_add_picture_to_events.rb
@@ -0,0 +1,7 @@
+class AddPictureToEvents < ActiveRecord::Migration
+ def change
+ change_table :events do |t|
+ t.has_attached_file :picture
+ end
+ end
+end
View
14 db/schema.rb
@@ -11,17 +11,23 @@
#
# It's strongly recommended to check this file into your version control system.
-ActiveRecord::Schema.define(:version => 20120328173443) do
+ActiveRecord::Schema.define(:version => 20120401115445) do
create_table "events", :force => true do |t|
t.string "content"
t.date "calendar_date"
- t.datetime "created_at", :null => false
- t.datetime "updated_at", :null => false
+ t.datetime "created_at", :null => false
+ t.datetime "updated_at", :null => false
t.integer "user_id"
- t.integer "repeat", :default => 0
+ t.integer "repeat", :default => 0
+ t.string "picture_file_name"
+ t.string "picture_content_type"
+ t.integer "picture_file_size"
+ t.datetime "picture_updated_at"
end
+ add_index "events", ["repeat"], :name => "index_events_on_repeat"
+
create_table "users", :force => true do |t|
t.string "email", :default => "", :null => false
t.string "encrypted_password", :default => "", :null => false
View
14 lib/tasks/data.rake
@@ -10,9 +10,21 @@ namespace :db do
User.create!(:name => name, :email => email, :password => password, :password_confirmation => password);
end
User.all(:limit => 10).each do |user|
- 25.times do
+ 15.times do
user.events.create!(:content => Faker::Lorem.sentence(25), :calendar_date => Date.today.prev_year+rand(730))
end
+ rand(3).times do
+ user.events.create!(:content => Faker::Lorem.sentence(10), :calendar_date => Date.today.prev_year+rand(730), :repeat => 4)
+ end
+ rand(3).times do
+ user.events.create!(:content => Faker::Lorem.sentence(10), :calendar_date => Date.today.prev_year+rand(730), :repeat => 3)
+ end
+ rand(3).times do
+ user.events.create!(:content => Faker::Lorem.sentence(10), :calendar_date => Date.today.prev_year+rand(730), :repeat => 2)
+ end
+ rand(5).times do
+ user.events.create!(:content => Faker::Lorem.sentence(10), :calendar_date => Date.today.prev_year+rand(730), :repeat => 1)
+ end
end
end
end
View
7 spec/models/event_spec.rb
@@ -3,7 +3,7 @@
describe Event do
before(:each) do
@user = Factory(:user)
- @attr = {:content => 'event content', :calendar_date => Date.new(1996,3,4)}
+ @attr = {:content => 'event content', :calendar_date => Date.new(1996,3,4), :repeat => 0}
end
it "event with user should be valid" do
@@ -14,4 +14,9 @@
Event.new(@attr).should_not be_valid
end
+ it "if event happens every day then calendar_dat may be blank" do
+ @user.events.new(@attr.merge(:repeat => 4, :calendar_date => nil)).should be_valid
+ end
+
+
end

0 comments on commit 78a719b

Please sign in to comment.
Something went wrong with that request. Please try again.