Skip to content

Commit 1498e6e

Browse files
Merge pull request #51 from cortex-cms/topic/COR-607-Alternate-Authors
COR-607: alternate authors
2 parents 45a6d20 + 100113a commit 1498e6e

File tree

3 files changed

+69
-0
lines changed

3 files changed

+69
-0
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
.mdl-textfield.mdl-js-textfield.authorfield
2+
= render_field_id
3+
= render_label
4+
= render_input
5+
= render_default_value

app/cells/plugins/core/author_cell.rb

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
module Plugins
2+
module Core
3+
class AuthorCell < Plugins::Core::Cell
4+
include Devise::Controllers::Helpers
5+
6+
def input
7+
render
8+
end
9+
10+
private
11+
12+
def value
13+
data&.[]('author_name') || current_user.fullname
14+
end
15+
16+
def render_label
17+
@options[:form].label 'data[author_name]', field.name, class: 'mdl-textfield__label'
18+
end
19+
20+
def render_input
21+
@options[:form].text_field 'data[author_name]', value: value, placeholder: @options[:placeholder], class: 'mdl-textfield__input', required: required?
22+
end
23+
24+
def render_default_value
25+
@options[:form].hidden_field 'data[default_author_name]', value: current_user.fullname
26+
end
27+
end
28+
end
29+
end

app/models/author_field_type.rb

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
class AuthorFieldType < FieldType
2+
attr_accessor :author_name
3+
jsonb_accessor :data, author_name: :string
4+
5+
validates :author_name, presence: true, if: :validate_presence?
6+
7+
def data=(data_hash)
8+
data_hash[:author_name] = data_hash[:default_author_name] if data_hash.deep_symbolize_keys[:author_name].blank?
9+
@author_name = data_hash.deep_symbolize_keys[:author_name]
10+
end
11+
12+
def field_item_as_indexed_json_for_field_type(field_item, options = {})
13+
json = {}
14+
json[mapping_field_name] = field_item.data['author_name']
15+
json
16+
end
17+
18+
def mapping
19+
{author_name: mapping_field_name, type: :string, analyzer: :snowball}
20+
end
21+
22+
private
23+
24+
def mapping_field_name
25+
"#{field_name.parameterize('_')}_author_name"
26+
end
27+
28+
def author_name_present
29+
errors.add(:author_name, 'must be present') if @author_name.empty?
30+
end
31+
32+
def validate_presence?
33+
@validations.key? :presence
34+
end
35+
end

0 commit comments

Comments
 (0)