Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Flag events as time-based

  • Loading branch information...
commit 1526ea20602c822d9b8ca36631987129e369b724 1 parent 51b9f9a
@gylaz gylaz authored
View
15 app/models/event.rb
@@ -21,6 +21,7 @@ class Event < ActiveRecord::Base
before_validation :generate_uuid, on: :create
before_validation :set_first_suggestion
before_create :set_owner_watermark
+ before_save :set_time_based
validates :name, presence: { message: 'This field is required' }
validates :name, length: { maximum: NAME_MAX_LENGTH }
@@ -65,15 +66,6 @@ def suggestions
@suggestions ||= Sorter.new(primary_suggestions).sort
end
- def time_based?
- begin
- DateTime.parse(winning_suggestion.full_description)
- true
- rescue ArgumentError
- false
- end
- end
-
def to_param
uuid
end
@@ -107,6 +99,11 @@ def set_owner_watermark
end
end
+ def set_time_based
+ self.time_based = primary_suggestions.all?(&:time_based?)
+ true
+ end
+
def remaining_suggestions
primary_suggestions.reject do |s|
s.marked_for_destruction?
View
9 app/models/primary_suggestion.rb
@@ -16,6 +16,15 @@ def suggestions
@suggestions ||= Sorter.new(secondary_suggestions).sort
end
+ def time_based?
+ begin
+ DateTime.parse(description)
+ true
+ rescue ArgumentError
+ false
+ end
+ end
+
private
def secondary_descriptions
View
38 db/migrate/20130424210132_add_time_based_to_events.rb
@@ -0,0 +1,38 @@
+class AddTimeBasedToEvents < ActiveRecord::Migration
+ def change
+ add_column :events, :time_based, :boolean, default: false, null: false
+
+ set_time_based_on_existing_rows
+ end
+
+ private
+
+ def set_time_based_on_existing_rows
+ rows = select_all('SELECT event_id, description FROM primary_suggestions')
+
+ convert_to_hash(rows).each do |id, descriptions|
+ time_based = descriptions.all? { |text| time_based?(text) }
+
+ if time_based
+ execute("UPDATE events SET time_based = TRUE WHERE id = #{id}")
+ end
+ end
+ end
+
+ def time_based?(text)
+ begin
+ DateTime.parse(text)
+ true
+ rescue ArgumentError
+ false
+ end
+ end
+
+ def convert_to_hash(rows)
+ rows.inject({}) do |result, row|
+ result[row['event_id']] ||= []
+ result[row['event_id']] << row['description']
+ result
+ end
+ end
+end
View
13 db/schema.rb
@@ -11,7 +11,7 @@
#
# It's strongly recommended to check this file into your version control system.
-ActiveRecord::Schema.define(:version => 20130410182528) do
+ActiveRecord::Schema.define(:version => 20130424210132) do
create_table "delayed_jobs", :force => true do |t|
t.integer "priority", :default => 0
@@ -30,14 +30,15 @@
add_index "delayed_jobs", ["priority", "run_at"], :name => "delayed_jobs_priority"
create_table "events", :force => true do |t|
- t.string "name", :null => false
- t.datetime "created_at", :null => false
- t.datetime "updated_at", :null => false
- t.integer "user_id", :null => false
- t.string "uuid", :limit => 8, :null => false
+ t.string "name", :null => false
+ t.datetime "created_at", :null => false
+ t.datetime "updated_at", :null => false
+ t.integer "user_id", :null => false
+ t.string "uuid", :limit => 8, :null => false
t.boolean "open", :default => true
t.integer "winning_suggestion_id"
t.string "winning_suggestion_type"
+ t.boolean "time_based", :default => false, :null => false
end
add_index "events", ["name"], :name => "index_events_on_name"
View
25 spec/models/event_spec.rb
@@ -45,6 +45,31 @@
end
end
+describe Event, 'before_create' do
+ context 'when event has a time based suggestion' do
+ it 'sets time_based to true' do
+ suggestion = build(:suggestion, description: 'Monday')
+ event = build(:event, primary_suggestions: [suggestion])
+
+ event.save
+
+ expect(event).to be_time_based
+ end
+ end
+
+ context 'when event does not have all time based suggestions' do
+ it 'sets time_based to false' do
+ suggestion1 = build(:suggestion, description: 'March 31')
+ suggestion2 = build(:suggestion, description: 'Bananas')
+ event = build(:event, primary_suggestions: [suggestion1, suggestion2])
+
+ event.save
+
+ expect(event).not_to be_time_based
+ end
+ end
+end
+
describe Event, 'validations' do
it { expect(subject).to validate_presence_of(:name).with_message(/field is required/) }
it { expect(subject).to ensure_length_of(:name).is_at_most(Event::NAME_MAX_LENGTH) }
Please sign in to comment.
Something went wrong with that request. Please try again.