diff --git a/app/assets/javascripts/datepicker_init.js b/app/assets/javascripts/datepicker_init.js index d89435ca6..2f890d3fc 100644 --- a/app/assets/javascripts/datepicker_init.js +++ b/app/assets/javascripts/datepicker_init.js @@ -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'); } }); diff --git a/app/jobs/publish_content_item_job.rb b/app/jobs/publish_content_item_job.rb deleted file mode 100644 index 1512a98a8..000000000 --- a/app/jobs/publish_content_item_job.rb +++ /dev/null @@ -1,7 +0,0 @@ -class PublishContentItemJob < ApplicationJob - queue_as :default - - def perform(content_item) - content_item.publish! - end -end diff --git a/app/models/content_item.rb b/app/models/content_item.rb index 451b14c26..f40017e91 100644 --- a/app/models/content_item.rb +++ b/app/models/content_item.rb @@ -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 @@ -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) @@ -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 diff --git a/app/services/publish_state_service.rb b/app/services/publish_state_service.rb new file mode 100644 index 000000000..9b0ee590e --- /dev/null +++ b/app/services/publish_state_service.rb @@ -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 diff --git a/app/views/content_items/_form.html.haml b/app/views/content_items/_form.html.haml index fd7dce0b6..4230f54ee 100644 --- a/app/views/content_items/_form.html.haml +++ b/app/views/content_items/_form.html.haml @@ -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' diff --git a/lib/tasks/employer/blog.rake b/lib/tasks/employer/blog.rake index 1f6560f6c..879fa9a85 100644 --- a/lib/tasks/employer/blog.rake +++ b/lib/tasks/employer/blog.rake @@ -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" }) @@ -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') @@ -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 } ] }, @@ -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 } ] }