lib/granite/form/model/attributes/base.rb in granite-form-0.3.0 vs lib/granite/form/model/attributes/base.rb in granite-form-0.4.0
- old
+ new
@@ -1,17 +1,16 @@
module Granite
module Form
module Model
module Attributes
class Base
- attr_reader :owner, :reflection
+ attr_reader :type_definition
+ delegate :type, :reflection, :owner, :enum, to: :type_definition
delegate :name, :readonly, to: :reflection
- delegate :type, to: :type_definition
- def initialize(reflection, owner)
- @reflection = reflection
- @owner = owner
+ def initialize(type_definition)
+ @type_definition = type_definition
@origin = :default
end
def write_value(value, origin: :user)
reset
@@ -51,17 +50,13 @@
def query
!(read.respond_to?(:zero?) ? read.zero? : read.blank?)
end
def readonly?
- !!(readonly.is_a?(Proc) ? evaluate(&readonly) : readonly)
+ !!owner.evaluate(readonly)
end
- def type_definition
- @type_definition ||= build_type_definition(reflection.type)
- end
-
def inspect_attribute
value = case read
when Date, Time, DateTime
%("#{read.to_s(:db)}")
else
@@ -93,22 +88,9 @@
yield
end
end
private
-
- def build_type_definition(type)
- Granite::Form.type_for(type).new(type, reflection, owner)
- end
-
- def evaluate(*args, &block)
- if block.arity >= 0 && block.arity <= args.length
- owner.instance_exec(*args.first(block.arity), &block)
- else
- args = block.arity.negative? ? args : args.first(block.arity)
- yield(*args, owner)
- end
- end
def remove_variable(*names)
names.flatten.each do |name|
name = :"@#{name}"
remove_instance_variable(name) if instance_variable_defined?(name)