Skip to content
Permalink
Browse files

More fixes for case when event is removed and add tests for this scen…

…ario
  • Loading branch information...
angusmcleod committed Sep 22, 2017
1 parent dad19d8 commit 2a3f31ae2ec0f6913118d94595cbe5c897d841eb
Showing with 36 additions and 20 deletions.
  1. +25 −19 plugin.rb
  2. +11 −1 spec/controllers/events_controllers_spec.rb
@@ -24,11 +24,14 @@
Topic.register_custom_field_type('event_start', :integer)
Topic.register_custom_field_type('event_end', :integer)

TopicList.preloaded_custom_fields << 'event_start' if TopicList.respond_to? :preloaded_custom_fields
TopicList.preloaded_custom_fields << 'event_end' if TopicList.respond_to? :preloaded_custom_fields

# but a combined hash with iso8601 dates is easier to work with
require_dependency 'topic'
class ::Topic
def has_event?
custom_fields['event_start'].present? && custom_fields['event_end'].present?
self.custom_fields['event_start']&.nonzero? && self.custom_fields['event_end']&.nonzero?
end

def event
@@ -41,40 +44,43 @@ def event
end
end

add_to_serializer(:topic_view, :include_event?) { object.topic.has_event? }
add_to_serializer(:topic_view, :event) { object.topic.event }
require_dependency 'topic_view_serializer'
class ::TopicViewSerializer
attributes :event

TopicList.preloaded_custom_fields << 'event_start' if TopicList.respond_to? :preloaded_custom_fields
TopicList.preloaded_custom_fields << 'event_end' if TopicList.respond_to? :preloaded_custom_fields
def event
object.topic.event
end

def include_event?
object.topic.has_event?
end
end

require_dependency 'topic_list_item_serializer'
class ::TopicListItemSerializer
attributes :event

def include_event?
object.has_event?
end

def event
object.event
end

def include_event?
object.has_event?
end
end

PostRevisor.track_topic_field(:event)

PostRevisor.class_eval do
track_topic_field(:event) do |tc, event|
if event['start']
event_start = event['start'].to_datetime.to_i
tc.record_change('event_start', tc.topic.custom_fields['event_start'], event_start)
tc.topic.custom_fields['event_start'] = event_start
end
event_start = event['start'] ? event['start'].to_datetime.to_i : nil
tc.record_change('event_start', tc.topic.custom_fields['event_start'], event_start)
tc.topic.custom_fields['event_start'] = event_start

if event['end']
event_end = event['end'].to_datetime.to_i
tc.record_change('event_end', tc.topic.custom_fields['event_start'], event_start)
tc.topic.custom_fields['event_end'] = event_end
end
event_end = event['start'] ? event['end'].to_datetime.to_i : nil
tc.record_change('event_end', tc.topic.custom_fields['event_start'], event_start)
tc.topic.custom_fields['event_end'] = event_end
end
end

@@ -10,7 +10,7 @@
'end' => '2017-09-18T17:00:00+08:00' } # 1505725200
end

describe 'post events' do
describe 'when creating an event topic' do
it 'works' do
xhr :post, :create, title: title, raw: 'New event', event: event1
expect(response).to be_success
@@ -25,6 +25,16 @@
).value).to eq('1505725200')
end
end

describe 'when an event topic has no start and end' do
let!(:topic) { Fabricate(:topic, user: user, custom_fields: { event_start: nil, event_end: nil }) }
let!(:post) { Fabricate(:post, user: user, topic: topic, post_number: 1) }

it 'allows the first post to be edited' do
xhr :put, :update, id: post.id, post: { raw: 'edited body', edit_reason: 'typo' }
expect(response).to be_success
end
end
end

describe TopicsController do

0 comments on commit 2a3f31a

Please sign in to comment.
You can’t perform that action at this time.