FIX: prevents exception when accessing `post.local_dates` (#125)

`post.local_dates` is coming from the core discourse plugin, which has the following implementation:

```
  add_to_class(:post, :local_dates) do
    custom_fields[DiscourseLocalDates::POST_CUSTOM_FIELD] || []
  end
```

Meaning we should always at least get an array, however, if the core local dates plugin is disabled, add_to_class will be called and the method added , however it will return nothing if the plugin is not enabled.

Source location correctly leads to the `add_to_class` block:

```
[9] pry(main)> Post.last.method(:local_dates).source_location
=> ["/discourse/lib/plugin/instance.rb", 260]
```
This commit is contained in:
Joffrey JAFFEUX 2021-04-27 21:08:12 +02:00 committed by GitHub
parent 227080a2da
commit 7d6e1f1a76
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 1 additions and 1 deletions

View File

@ -9,7 +9,7 @@ class CalendarEvent < ActiveRecord::Base
CalendarEvent.where(post_id: post.id).destroy_all
dates = post.local_dates
return if dates.size < 1 || dates.size > 2
return if !dates || dates.size < 1 || dates.size > 2
first_post = post.topic&.first_post
return if !first_post || !first_post.custom_fields[DiscourseCalendar::CALENDAR_CUSTOM_FIELD]