1
0
Fork 0
mirror of https://github.com/heartcombo/simple_form.git synced 2022-11-09 12:19:26 -05:00
heartcombo--simple_form/lib/simple_form/components/readonly.rb
Dillon Welch cac943e891 Add frozen_string_literal in all .rb files
I noticed these files all had strings such as "", "  ", "_" that were
allocated each time some common methods were called, over 1000x on a
page in my app. This comment freezes all of these strings such that
they're only allocated once, saving many KB of memory allocation.
2017-11-27 17:06:18 -08:00

23 lines
600 B
Ruby

# frozen_string_literal: true
module SimpleForm
module Components
# Needs to be enabled in order to do automatic lookups.
module Readonly
def readonly(wrapper_options = nil)
if readonly_attribute? && !has_readonly?
input_html_options[:readonly] ||= true
input_html_classes << :readonly
end
nil
end
private
def readonly_attribute?
object.class.respond_to?(:readonly_attributes) &&
object.persisted? &&
object.class.readonly_attributes.include?(attribute_name.to_s)
end
end
end
end