Skip to content

Commit

Permalink
Implement Object#try and use to get field value.
Browse files Browse the repository at this point in the history
  • Loading branch information
UweKubosch committed Feb 2, 2024
1 parent e4fec41 commit 7b26de3
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 4 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ plugins {
}

group 'no.datek'
version '0.12.2'
version '0.12.3'
final String JRUBY_VERSION = '9.4.5.0';

repositories {
Expand Down
6 changes: 6 additions & 0 deletions src/main/resources/ruby/core_ext.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
class Object
def present? = !blank?
def blank? = false

def try(method, *args)
return nil unless self.respond_to?(method)
return send(method, *args)
end
end

class NilClass
def blank? = true
def try(*) = nil
end

class String
Expand Down
6 changes: 3 additions & 3 deletions src/main/resources/ruby/form_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -141,15 +141,15 @@ def text_input(object, field_name, **opts)
classes = +"form-control"

classes << ' ' << custom_classes if custom_classes
classes << ' is-invalid' if object&.hasFieldErrors(field_name.to_s)
classes << ' is-invalid' if object.try(:hasFieldErrors, field_name.to_s)
classes
end

private def select_classes(object, field_name, custom_classes)
classes = +"form-select"

classes << ' ' << custom_classes if custom_classes
classes << ' is-invalid' if object&.hasFieldErrors(field_name.to_s)
classes << ' is-invalid' if object.try(:hasFieldErrors, field_name.to_s)
classes
end

Expand Down Expand Up @@ -233,7 +233,7 @@ def textarea(object, field_name, **opts)
html << %{ #{key}="#{value}"}
end
html << ">"
html << object[field_name]
html << (object.try(field_name) || object.try(:[], field_name)).to_s
html << "</textarea>"
html << "<label for='#{id_name}' style='width:auto'>#{appendix}</label></span>" if appendix
html << "<br/>" unless no_break
Expand Down

0 comments on commit 7b26de3

Please sign in to comment.