Skip to content

Commit ff944a0

Browse files
committed
refactor: include devise helpers in author cell
1 parent 2726603 commit ff944a0

File tree

2 files changed

+13
-17
lines changed

2 files changed

+13
-17
lines changed

app/cells/plugins/core/author_cell.rb

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,24 @@
11
module Plugins
22
module Core
33
class AuthorCell < Plugins::Core::Cell
4+
include Devise::Controllers::Helpers
5+
46
def input
57
render
68
end
79

810
private
911

10-
def current_user
11-
"#{@options[:current_user].firstname} #{@options[:current_user].lastname}"
12-
end
13-
1412
def value
15-
data&.[]('author_name') || current_user
13+
data&.[]('author_name') || current_user.fullname
1614
end
1715

1816
def render_label
19-
@options[:form].label 'data[text]', field.name, class: 'mdl-textfield__label'
17+
@options[:form].label 'data[author_name]', field.name, class: 'mdl-textfield__label'
2018
end
2119

2220
def render_input
23-
@options[:form].text_field 'data[text]', value: value, placeholder: @options[:placeholder], class: 'mdl-textfield__input', required: required?
21+
@options[:form].text_field 'data[author_name]', value: value, placeholder: @options[:placeholder], class: 'mdl-textfield__input', required: required?
2422
end
2523
end
2624
end

app/models/author_field_type.rb

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,22 @@
11
class AuthorFieldType < FieldType
2+
include Devise::Controllers::Helpers
3+
24
attr_accessor :author_name
35
jsonb_accessor :data, author_name: :string
46

57
validates :author_name, presence: true, if: :validate_presence?
68

79
def data=(data_hash)
8-
@author_name = data_hash.deep_symbolize_keys[:author_name]
10+
if data_hash.deep_symbolize_keys[:author_name].blank?
11+
@author_name = current_user.fullname
12+
else
13+
@author_name = data_hash.deep_symbolize_keys[:author_name]
14+
end
915
end
1016

1117
def field_item_as_indexed_json_for_field_type(field_item, options = {})
1218
json = {}
13-
json[mapping_field_author_name] = field_item.data['author_name']
19+
json[mapping_field_name] = field_item.data['author_name']
1420
json
1521
end
1622

@@ -28,15 +34,7 @@ def author_name_present
2834
errors.add(:author_name, 'must be present') if @author_name.empty?
2935
end
3036

31-
def validate_uniqueness?
32-
@validations.key? :uniqueness
33-
end
34-
3537
def validate_presence?
3638
@validations.key? :presence
3739
end
38-
39-
def validate_length?
40-
@validations.key? :length
41-
end
4240
end

0 commit comments

Comments
 (0)