Class: Basepack::Forms::Diff

Inherits:
Edit
  • Object
show all
Defined in:
lib/basepack/forms/diff.rb

Instance Attribute Summary collapse

Attributes inherited from Edit

#builder_default_options, #simple_form

Attributes inherited from Base

#association_chain, #factory, #groups, #nested_in, #partial, #resource, #resource_class, #view

Instance Method Summary collapse

Methods inherited from Edit

#builder, data_picker_options, #render_field!, #textfield_options, #with_simple_form

Methods inherited from Base

#chain, #chain_with_class, #configure, #content_for_field, #default_group, #field, #field_names, #fields, #fields_hash, #group, #has_field?, #hide_field, #hide_fields, #inspect, #inverse_of_nested_in?, #label, #label_plural, #new_form, #new_record?, #permit_params, #render_field, #render_field!, #sanitize_params, #show_fields, #show_only_fields, #translate, #visible_field, #visible_fields, #visible_groups, #with_resource

Constructor Details

#initialize(factory, chain1, chain2, options = {}) ⇒ Diff

Returns a new instance of Diff.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/basepack/forms/diff.rb', line 7

def initialize(factory, chain1, chain2, options = {})
  super(factory, chain1, options)
  @path = options[:path]
  @form2 = Basepack::Forms::Show.new(factory, chain2)

  @compare = {}
  compare '*' do |f1, f2|
    if f1.association?
      a1, a2 = Array.wrap(f1.value).compact, Array.wrap(f2.value).compact
      a1, a2 = a2, a1 if a1.size < a2.size
      a1.zip(a2).all? {|v1, v2| v2 ? v1.same_as?(v2) : false }
    else
      f1.value.presence == f2.value.presence
    end
  end
end

Instance Attribute Details

#form2Object (readonly)

Returns the value of attribute form2.



4
5
6
# File 'lib/basepack/forms/diff.rb', line 4

def form2
  @form2
end

#pathObject

Returns the value of attribute path.



5
6
7
# File 'lib/basepack/forms/diff.rb', line 5

def path
  @path
end

Instance Method Details

#build_from_factoryObject



24
25
26
27
# File 'lib/basepack/forms/diff.rb', line 24

def build_from_factory
  factory.build_form(@form2)
  factory.build_form(self)
end

#compare(field_name, &block) ⇒ Object



45
46
47
48
49
50
51
# File 'lib/basepack/forms/diff.rb', line 45

def compare(field_name, &block)
  if block_given?
    @compare[field_name] = block
  else
    @compare[field_name]
  end
end

#default_partialObject



60
61
62
# File 'lib/basepack/forms/diff.rb', line 60

def default_partial
  'forms/diff'
end

#field2(name, attributes = nil) ⇒ Object



33
34
35
36
37
38
39
# File 'lib/basepack/forms/diff.rb', line 33

def field2(name, attributes = nil)
  if field(name)
    @form2.field(name, attributes)
  else
    nil
  end
end

#field_show_values(name, &block) ⇒ Object



53
54
55
56
57
58
# File 'lib/basepack/forms/diff.rb', line 53

def field_show_values(name, &block)
  f1 = field(name)
  f2 = field2(name)
  f1.pretty_value = yield(f1, f2)
  f2.pretty_value = yield(f2, f1)
end

#resource2Object



29
30
31
# File 'lib/basepack/forms/diff.rb', line 29

def resource2
  @form2.resource
end

#visible_field2(name) ⇒ Object



41
42
43
# File 'lib/basepack/forms/diff.rb', line 41

def visible_field2(name)
  visible_field(name) ? @form2.visible_field(name) : nil
end