lib/administrate/field/base.rb in administrate-0.14.0 vs lib/administrate/field/base.rb in administrate-0.15.0
- old
+ new
@@ -46,9 +46,35 @@
def to_partial_path
"/fields/#{self.class.field_type}/#{page}"
end
+ def required?
+ return false unless resource.class.respond_to?(:validators_on)
+
+ resource.class.validators_on(attribute).any? do |v|
+ next false unless v.class == ActiveRecord::Validations::PresenceValidator
+
+ options = v.options
+ next false if options.include?(:if)
+ next false if options.include?(:unless)
+
+ if on_option = options[:on]
+ if on_option == :create && !resource.persisted?
+ next true
+ end
+
+ if on_option == :update && resource.persisted?
+ next true
+ end
+
+ next false
+ end
+
+ true
+ end
+ end
+
attr_reader :attribute, :data, :options, :page, :resource
end
end
end