Skip to content

Commit

Permalink
WIP: fix read_value method in Administrate::Field::Base
Browse files Browse the repository at this point in the history
  • Loading branch information
goosys committed Sep 14, 2024
1 parent f377679 commit a83e2ff
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 6 deletions.
11 changes: 7 additions & 4 deletions lib/administrate/field/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,10 @@ def self.permitted_attribute(attr, _options = nil)

def initialize(attribute, data, page, options = {})
@attribute = attribute
@data = data
@page = page
@resource = options.delete(:resource)
@options = options
@data = read_value if @data.nil?
@data = read_value(data)
end

def html_class
Expand All @@ -53,15 +52,19 @@ def name
attribute.to_s
end

def read_value
def read_value(data = nil)
if options.key?(:getter)
if options[:getter].respond_to?(:call)
options[:getter].call(self)
else
resource&.public_send(options[:getter])
end
else
resource&.public_send(attribute)
if data.nil?

Check failure on line 63 in lib/administrate/field/base.rb

View workflow job for this annotation

GitHub Actions / standardrb

Style/IfInsideElse: Convert `if` nested inside `else` to `elsif`.
resource&.public_send(attribute)

Check failure on line 64 in lib/administrate/field/base.rb

View workflow job for this annotation

GitHub Actions / standardrb

Layout/ElseAlignment: Align `else` with `if`.
else

Check failure on line 65 in lib/administrate/field/base.rb

View workflow job for this annotation

GitHub Actions / standardrb

Layout/IndentationWidth: Use 2 (not 4) spaces for indentation.
data
end
end
end

Expand Down
3 changes: 1 addition & 2 deletions spec/lib/fields/base_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -184,11 +184,10 @@
end

it "reads the value from the resource with a custom getter block" do
resource = double
resource = double("Model", custom_getter: "value")
field = field_class.new(:attribute, :date, :page, resource: resource, getter: ->(field) { field.resource.custom_getter })

Check failure on line 188 in spec/lib/fields/base_spec.rb

View workflow job for this annotation

GitHub Actions / standardrb

Lint/UselessAssignment: Useless assignment to variable - `field`.

expect(resource).to receive(:custom_getter)
field.read_value
end

it "returns nil if the resource is nil" do
Expand Down
38 changes: 38 additions & 0 deletions spec/lib/fields/virtual_field_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
require "rails_helper"
require "administrate/field/base"

module Administrate
module Field
class VirtualField < Field::Base
def self.searchable?
false
end

def read_value(data = nil)
resource.inspect
end

def foobar
"This is a Foobar: #{data}"
end
end
end
end

describe Administrate::Field::VirtualField do

Check failure on line 23 in spec/lib/fields/virtual_field_spec.rb

View workflow job for this annotation

GitHub Actions / standardrb

Layout/EmptyLinesAroundBlockBody: Extra empty line detected at block body beginning.
describe "#foobar" do
it "displays the foobar" do
resource = double('Model', inspect: 'Inspecting')

Check failure on line 26 in spec/lib/fields/virtual_field_spec.rb

View workflow job for this annotation

GitHub Actions / standardrb

Style/StringLiterals: Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.

Check failure on line 26 in spec/lib/fields/virtual_field_spec.rb

View workflow job for this annotation

GitHub Actions / standardrb

Style/StringLiterals: Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.

field = Administrate::Field::VirtualField.new(
:virtual_field,
nil,
:show,
resource: resource
)

expect(field.foobar).to eq("This is a Foobar: Inspecting")
end
end
end

0 comments on commit a83e2ff

Please sign in to comment.