Module: Slices::HasSlices

Extended by:
ActiveSupport::Concern
Included in:
Page
Defined in:
lib/slices/has_slices.rb

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#embeded_slices(&block) ⇒ Object



73
74
75
76
77
# File 'lib/slices/has_slices.rb', line 73

def embeded_slices(&block)
  self.class.slice_embeds.each do |embed_name|
    yield(embed_name)
  end
end

#messages_from_errors(errors) ⇒ Object



79
80
81
82
83
84
85
# File 'lib/slices/has_slices.rb', line 79

def messages_from_errors(errors)
  if errors.respond_to?(:messages)
    errors.messages
  else
    errors
  end
end

#ordered_slices_for(embed_name = nil) ⇒ Object



49
50
51
52
# File 'lib/slices/has_slices.rb', line 49

def ordered_slices_for(embed_name=nil)
  embed_name = self.class.slice_embeds.first if embed_name.nil?
  send("ordered_#{embed_name}")
end

#slice_errors_for(embed_name) ⇒ Object



63
64
65
66
67
68
69
70
71
# File 'lib/slices/has_slices.rb', line 63

def slice_errors_for(embed_name)
  Hash.new.tap do |message|
    slices_for(embed_name).each do |slice|
      if ! slice.valid? && ! slice.to_delete?
        message[slice.id_or_client_id] = messages_from_errors(slice.errors)
      end
    end
  end
end

#slices_for(embed_name) ⇒ Object



45
46
47
# File 'lib/slices/has_slices.rb', line 45

def slices_for(embed_name)
  send(embed_name)
end

#update_attributes(attributes) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/slices/has_slices.rb', line 29

def update_attributes(attributes)
  [:slices, :set_slices].each do |type|
    next if attributes[type].nil?

    attributes[type] = attributes[type].map { |slice|
      slice = slice.symbolize_keys
      next if slice[:_destroy]
      slice.delete :_new
      klass = (slice[:type] + '_slice').camelize.constantize
      klass.new(slice)
    }.compact
  end

  super
end

#validate_slicesObject



54
55
56
57
58
59
60
61
# File 'lib/slices/has_slices.rb', line 54

def validate_slices
  embeded_slices do |embed_name|
    _slice_errors = slice_errors_for(embed_name)
    if _slice_errors.any?
      errors.add(embed_name, _slice_errors)
    end
  end
end