Skip to content

Commit

Permalink
Merge pull request #2757 from projecthydra/content_block_concern
Browse files Browse the repository at this point in the history
ContentBlockBehavior
  • Loading branch information
jcoyne authored Sep 30, 2016
2 parents 53cddc7 + 34da582 commit 815faaa
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 39 deletions.
47 changes: 47 additions & 0 deletions app/models/concerns/sufia/content_block_behavior.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
module Sufia
module ContentBlockBehavior
extend ActiveSupport::Concern

MARKETING = 'marketing_text'.freeze
RESEARCHER = 'featured_researcher'.freeze
ANNOUNCEMENT = 'announcement_text'.freeze

def external_key_name
self.class.external_keys.fetch(name) { 'External Key' }
end

class_methods do
def marketing_text
find_or_create_by(name: MARKETING)
end

def marketing_text=(value)
marketing_text.update(value: value)
end

def announcement_text
find_or_create_by(name: ANNOUNCEMENT)
end

def announcement_text=(value)
announcement_text.update(value: value)
end

def recent_researchers
where(name: RESEARCHER).order('created_at DESC')
end

def featured_researcher
recent_researchers.first_or_create(name: RESEARCHER)
end

def featured_researcher=(value)
create(name: RESEARCHER, value: value)
end

def external_keys
{ RESEARCHER => 'User' }
end
end
end
end
40 changes: 1 addition & 39 deletions app/models/content_block.rb
Original file line number Diff line number Diff line change
@@ -1,41 +1,3 @@
class ContentBlock < ActiveRecord::Base
MARKETING = 'marketing_text'.freeze
RESEARCHER = 'featured_researcher'.freeze
ANNOUNCEMENT = 'announcement_text'.freeze

def self.marketing_text
find_or_create_by(name: MARKETING)
end

def self.marketing_text=(value)
marketing_text.update(value: value)
end

def self.announcement_text
find_or_create_by(name: ANNOUNCEMENT)
end

def self.announcement_text=(value)
announcement_text.update(value: value)
end

def self.recent_researchers
where(name: RESEARCHER).order('created_at DESC')
end

def self.featured_researcher
recent_researchers.first_or_create(name: RESEARCHER)
end

def self.featured_researcher=(value)
create(name: RESEARCHER, value: value)
end

def self.external_keys
{ RESEARCHER => 'User' }
end

def external_key_name
self.class.external_keys.fetch(name) { 'External Key' }
end
include Sufia::ContentBlockBehavior
end

0 comments on commit 815faaa

Please sign in to comment.