Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
toastercup committed Apr 26, 2017
2 parents dfc1c99 + 9d5dc58 commit 0541458
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 53 deletions.
21 changes: 9 additions & 12 deletions app/assets/javascripts/datepicker_init.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,21 @@ var datepicker_init = function() {
if (datepicker.length) {
datepicker.datetimepicker({
dateFormat: 'dd/mm/yy',
timeFormat: 'HH:mm Z',
onSelect: function (date) {
var button = $('.new_publish_state_button');
var currentDate = new Date();
var selectedDate = moment(date, 'DD-MM-YYYY HH:mm');

if (button.length > 0 && selectedDate > currentDate) {
button.val('schedule');
button.text('Schedule Post');
}
else if (button.length > 0 && selectedDate <= currentDate) {
button.val('published');
button.text('Publish Now');
}
var button = $('.form-button--submission');
button.text('Schedule Post');
button.val('schedule');
}
});

datepicker.on('focusout', function(ev){
if ($(this).val() === "") {
var button = $('.form-button--submission');

button.text('Save Now');
button.val('draft');

$(this).closest('div').addClass('is-dirty');
}
});
Expand Down
7 changes: 0 additions & 7 deletions app/jobs/publish_content_item_job.rb

This file was deleted.

33 changes: 17 additions & 16 deletions app/models/content_item.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,16 @@ class ContentItem < ApplicationRecord
include Elasticsearch::Model
include Elasticsearch::Model::Callbacks

state_machine :initial => :default do
state_machine do
state :draft
state :scheduled, :enter => lambda { |content_item| content_item.schedule_publish }
state :published
state :default #the default state that is given to an object - this should only ever exist on ContentItems where the ContentType is not publishable

event :publish do
transitions :to => :published, :from => [:default, :draft, :scheduled]
end
state :scheduled

event :schedule do
transitions :to => :scheduled, :from => [:default, :draft]
transitions :to => :scheduled, :from => [:draft]
end

event :draft do
transitions :to => :draft, :from => [:default, :published, :scheduled]
transitions :to => :draft, :from => [:scheduled]
end
end

Expand Down Expand Up @@ -47,12 +41,7 @@ def author_email
end

def publish_state
state.titleize
end

def schedule_publish
timestamp = field_items.find { |field_item| field_item.field.name == "Publish Date" }.data["timestamp"]
PublishContentItemJob.set(wait_until: DateTime.parse(timestamp)).perform_later(self)
PublishStateService.new.content_item_state(self)
end

def rss_url(base_url, slug_field_id)
Expand Down Expand Up @@ -115,4 +104,16 @@ def update_tag_lists
ContentItemService.update_tags(self, tags)
end
end

# Metaprograms a number of convenience methods for content_items
def method_missing(*args)
if args[0].to_s.include?("?")
# Used to check state - allows for methods such as #published? and #expired?
# Will return true if the active_state corresponds to the name of the method
"#{publish_state.downcase}?" == args[0].to_s
else
# Used to query for any field on the relevant ContentType and return data from the content_item
field_items.select { |field_item| field_item.field.name.parameterize({ separator: '_' }) == args[0].to_s }.first.data.values[0]
end
end
end
24 changes: 24 additions & 0 deletions app/services/publish_state_service.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
class PublishStateService < ApplicationService
def content_item_state(content_item)
@content_item = content_item

active_state
end

private

def active_state
sorted_field_items.each do |field_item|
if !field_item.data['timestamp'].blank? && DateTime.now > DateTime.parse(field_item.data["timestamp"])
return field_item.field.metadata["state"]
break
end
end

return @content_item.state.titleize
end

def sorted_field_items
@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
end
end
15 changes: 1 addition & 14 deletions app/views/content_items/_form.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,5 @@
= cell('wizard', @wizard, context: { content_item: @content_item, form: form }).()
%footer.mdl-grid
.mdl-cell.mdl-cell--12-col
- if @content_type.publishable
- case @content_item.state
- when 'published'
= form.submit "Update Post", class: 'mdl-button mdl-js-button mdl-button--raised mdl-button--success mdl-js-ripple-effect'
= 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'
- when 'scheduled'
= 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'
= 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'
= 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'
- else
= 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'
= 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'
- else
= form.submit 'Submit', class: 'mdl-button mdl-js-button mdl-button--raised mdl-button--success mdl-js-ripple-effect'
= 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'
= link_to 'Cancel', content_type_content_items_path(@content_type), class: 'mdl-button mdl-js-button mdl-button--cb mdl-js-ripple-effect'
14 changes: 10 additions & 4 deletions lib/tasks/employer/blog.rake
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ namespace :employer do

def category_tree
tree = Tree.new
tree.add_node({ name: "Recruitment Solutions" })
tree.add_node({ name: "Product News" })
tree.add_node({ name: "Company News, Research and Trends" })
tree.add_node({ name: "Client Success Stories" })
tree.add_node({ name: "Recruiting Solutions" })
tree.add_node({ name: "Employment Screening" })
tree.add_node({ name: "Human Capital Management" })

Expand Down Expand Up @@ -65,8 +68,8 @@ namespace :employer do
blog.fields.new(name: 'Slug', field_type: 'text_field_type', validations: {presence: true, uniqueness: true, length: { maximum: 75 } })
blog.fields.new(name: 'Author', field_type: 'author_field_type')
blog.fields.new(name: 'Tags', field_type: 'tag_field_type')
blog.fields.new(name: 'Publish Date', field_type: 'date_time_field_type')
blog.fields.new(name: 'Expiration Date', field_type: 'date_time_field_type')
blog.fields.new(name: 'Publish Date', field_type: 'date_time_field_type', metadata: {state: 'Published'})
blog.fields.new(name: 'Expiration Date', field_type: 'date_time_field_type', metadata: {state: 'Expired'})
blog.fields.new(name: 'SEO Title', field_type: 'text_field_type', validations: {presence: true, length: {maximum: 70}, uniqueness: true })
blog.fields.new(name: 'SEO Description', field_type: 'text_field_type', validations: {presence: true, length: {maximum: 160} })
blog.fields.new(name: 'SEO Keywords', field_type: 'tag_field_type')
Expand Down Expand Up @@ -140,7 +143,7 @@ namespace :employer do
"id": blog.fields.find_by_name('Publish Date').id
},
{
"id": blog.fields.find_by_name('Author').id
"id": blog.fields.find_by_name('Expiration Date').id
}
]
},
Expand All @@ -153,6 +156,9 @@ namespace :employer do
{
"id": blog.fields.find_by_name('Slug').id,
"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."
},
{
"id": blog.fields.find_by_name('Author').id
}
]
}
Expand Down

0 comments on commit 0541458

Please sign in to comment.