Skip to content

Commit 0541458

Browse files
committed
Merge branch 'develop'
2 parents dfc1c99 + 9d5dc58 commit 0541458

File tree

6 files changed

+61
-53
lines changed

6 files changed

+61
-53
lines changed

app/assets/javascripts/datepicker_init.js

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,21 @@ var datepicker_init = function() {
44
if (datepicker.length) {
55
datepicker.datetimepicker({
66
dateFormat: 'dd/mm/yy',
7+
timeFormat: 'HH:mm Z',
78
onSelect: function (date) {
8-
var button = $('.new_publish_state_button');
9-
var currentDate = new Date();
10-
var selectedDate = moment(date, 'DD-MM-YYYY HH:mm');
11-
12-
if (button.length > 0 && selectedDate > currentDate) {
13-
button.val('schedule');
14-
button.text('Schedule Post');
15-
}
16-
else if (button.length > 0 && selectedDate <= currentDate) {
17-
button.val('published');
18-
button.text('Publish Now');
19-
}
9+
var button = $('.form-button--submission');
10+
button.text('Schedule Post');
11+
button.val('schedule');
2012
}
2113
});
2214

2315
datepicker.on('focusout', function(ev){
2416
if ($(this).val() === "") {
17+
var button = $('.form-button--submission');
18+
19+
button.text('Save Now');
20+
button.val('draft');
21+
2522
$(this).closest('div').addClass('is-dirty');
2623
}
2724
});

app/jobs/publish_content_item_job.rb

Lines changed: 0 additions & 7 deletions
This file was deleted.

app/models/content_item.rb

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,16 @@ class ContentItem < ApplicationRecord
33
include Elasticsearch::Model
44
include Elasticsearch::Model::Callbacks
55

6-
state_machine :initial => :default do
6+
state_machine do
77
state :draft
8-
state :scheduled, :enter => lambda { |content_item| content_item.schedule_publish }
9-
state :published
10-
state :default #the default state that is given to an object - this should only ever exist on ContentItems where the ContentType is not publishable
11-
12-
event :publish do
13-
transitions :to => :published, :from => [:default, :draft, :scheduled]
14-
end
8+
state :scheduled
159

1610
event :schedule do
17-
transitions :to => :scheduled, :from => [:default, :draft]
11+
transitions :to => :scheduled, :from => [:draft]
1812
end
1913

2014
event :draft do
21-
transitions :to => :draft, :from => [:default, :published, :scheduled]
15+
transitions :to => :draft, :from => [:scheduled]
2216
end
2317
end
2418

@@ -47,12 +41,7 @@ def author_email
4741
end
4842

4943
def publish_state
50-
state.titleize
51-
end
52-
53-
def schedule_publish
54-
timestamp = field_items.find { |field_item| field_item.field.name == "Publish Date" }.data["timestamp"]
55-
PublishContentItemJob.set(wait_until: DateTime.parse(timestamp)).perform_later(self)
44+
PublishStateService.new.content_item_state(self)
5645
end
5746

5847
def rss_url(base_url, slug_field_id)
@@ -115,4 +104,16 @@ def update_tag_lists
115104
ContentItemService.update_tags(self, tags)
116105
end
117106
end
107+
108+
# Metaprograms a number of convenience methods for content_items
109+
def method_missing(*args)
110+
if args[0].to_s.include?("?")
111+
# Used to check state - allows for methods such as #published? and #expired?
112+
# Will return true if the active_state corresponds to the name of the method
113+
"#{publish_state.downcase}?" == args[0].to_s
114+
else
115+
# Used to query for any field on the relevant ContentType and return data from the content_item
116+
field_items.select { |field_item| field_item.field.name.parameterize({ separator: '_' }) == args[0].to_s }.first.data.values[0]
117+
end
118+
end
118119
end
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class PublishStateService < ApplicationService
2+
def content_item_state(content_item)
3+
@content_item = content_item
4+
5+
active_state
6+
end
7+
8+
private
9+
10+
def active_state
11+
sorted_field_items.each do |field_item|
12+
if !field_item.data['timestamp'].blank? && DateTime.now > DateTime.parse(field_item.data["timestamp"])
13+
return field_item.field.metadata["state"]
14+
break
15+
end
16+
end
17+
18+
return @content_item.state.titleize
19+
end
20+
21+
def sorted_field_items
22+
@sorted_field_items ||= @content_item.field_items.select { |fi| fi.field.field_type_instance.is_a?(DateTimeFieldType) && !fi.field.metadata["state"].nil? }.sort_by{ |a| a.data["timestamp"] }.reverse
23+
end
24+
end

app/views/content_items/_form.html.haml

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,5 @@
44
= cell('wizard', @wizard, context: { content_item: @content_item, form: form }).()
55
%footer.mdl-grid
66
.mdl-cell.mdl-cell--12-col
7-
- if @content_type.publishable
8-
- case @content_item.state
9-
- when 'published'
10-
= form.submit "Update Post", class: 'mdl-button mdl-js-button mdl-button--raised mdl-button--success mdl-js-ripple-effect'
11-
= form.button 'Save as Draft', value: 'draft', name: 'content_item[state]', class: 'mdl-button mdl-js-button mdl-button--cb mdl-js-ripple-effect', type: 'submit'
12-
- when 'scheduled'
13-
= form.button 'Schedule Post', value: 'schedule', name: 'content_item[state]', class: 'mdl-button mdl-js-button mdl-button--cb mdl-js-ripple-effect', type: 'submit'
14-
= form.button 'Publish Now', value: 'publish', name: 'content_item[state]', class: 'mdl-button mdl-js-button mdl-button--raised mdl-button--success mdl-js-ripple-effect', type: 'submit'
15-
= form.button 'Save as Draft', value: 'draft', name: 'content_item[state]', class: 'mdl-button mdl-js-button mdl-button--cb mdl-js-ripple-effect', type: 'submit'
16-
- else
17-
= form.button 'Publish Now', value: 'publish', name: 'content_item[state]', class: 'mdl-button mdl-js-button mdl-button--raised mdl-button--success mdl-js-ripple-effect new_publish_state_button', type: 'submit'
18-
= form.button 'Save as Draft', value: 'draft', name: 'content_item[state]', class: 'mdl-button mdl-js-button mdl-button--cb mdl-js-ripple-effect', type: 'submit'
19-
- else
20-
= form.submit 'Submit', class: 'mdl-button mdl-js-button mdl-button--raised mdl-button--success mdl-js-ripple-effect'
7+
= form.button 'Save Now', value: @content_item.state || 'draft', name: 'content_item[state]', class: 'mdl-button form-button--submission mdl-js-button mdl-button--raised mdl-button--success mdl-js-ripple-effect', type: 'submit'
218
= link_to 'Cancel', content_type_content_items_path(@content_type), class: 'mdl-button mdl-js-button mdl-button--cb mdl-js-ripple-effect'

lib/tasks/employer/blog.rake

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,10 @@ namespace :employer do
1414

1515
def category_tree
1616
tree = Tree.new
17-
tree.add_node({ name: "Recruitment Solutions" })
17+
tree.add_node({ name: "Product News" })
18+
tree.add_node({ name: "Company News, Research and Trends" })
19+
tree.add_node({ name: "Client Success Stories" })
20+
tree.add_node({ name: "Recruiting Solutions" })
1821
tree.add_node({ name: "Employment Screening" })
1922
tree.add_node({ name: "Human Capital Management" })
2023

@@ -65,8 +68,8 @@ namespace :employer do
6568
blog.fields.new(name: 'Slug', field_type: 'text_field_type', validations: {presence: true, uniqueness: true, length: { maximum: 75 } })
6669
blog.fields.new(name: 'Author', field_type: 'author_field_type')
6770
blog.fields.new(name: 'Tags', field_type: 'tag_field_type')
68-
blog.fields.new(name: 'Publish Date', field_type: 'date_time_field_type')
69-
blog.fields.new(name: 'Expiration Date', field_type: 'date_time_field_type')
71+
blog.fields.new(name: 'Publish Date', field_type: 'date_time_field_type', metadata: {state: 'Published'})
72+
blog.fields.new(name: 'Expiration Date', field_type: 'date_time_field_type', metadata: {state: 'Expired'})
7073
blog.fields.new(name: 'SEO Title', field_type: 'text_field_type', validations: {presence: true, length: {maximum: 70}, uniqueness: true })
7174
blog.fields.new(name: 'SEO Description', field_type: 'text_field_type', validations: {presence: true, length: {maximum: 160} })
7275
blog.fields.new(name: 'SEO Keywords', field_type: 'tag_field_type')
@@ -140,7 +143,7 @@ namespace :employer do
140143
"id": blog.fields.find_by_name('Publish Date').id
141144
},
142145
{
143-
"id": blog.fields.find_by_name('Author').id
146+
"id": blog.fields.find_by_name('Expiration Date').id
144147
}
145148
]
146149
},
@@ -153,6 +156,9 @@ namespace :employer do
153156
{
154157
"id": blog.fields.find_by_name('Slug').id,
155158
"tooltip": "This is your post's URL. Between each word, place a hyphen. Best if between 35-50 characters and don't include years/dates."
159+
},
160+
{
161+
"id": blog.fields.find_by_name('Author').id
156162
}
157163
]
158164
}

0 commit comments

Comments
 (0)