Class: CanBe::Processor::Instance

Inherits:
Object
  • Object
show all
Defined in:
lib/can_be/processor/instance.rb

Instance Method Summary collapse

Constructor Details

#initialize(model) ⇒ Instance

Returns a new instance of Instance.



4
5
6
7
8
9
10
11
12
# File 'lib/can_be/processor/instance.rb', line 4

def initialize(model)
  @model = model
  @config = model.class.can_be_config
  @field_name = @config.field_name
  @details_name = @config.details_name.to_sym
  @details_id = "#{@details_name}_id".to_sym
  @details_type = "#{@details_name}_type".to_sym
  set_cleaning_defaults
end

Instance Method Details

#boolean_eval(t) ⇒ Object



14
15
16
# File 'lib/can_be/processor/instance.rb', line 14

def boolean_eval(t)
  field_value.to_s == t.to_s
end

#clean_detailsObject



53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/can_be/processor/instance.rb', line 53

def clean_details
  if @original_details && @original_details.class != @model.send(@details_name).class
    if @config.keeps_history?
      if @force_history_removal
        @original_details.destroy
        destroy_history(@original_details.class.name.underscore)
      end
    else
      @original_details.destroy
    end
  end

  set_cleaning_defaults
end

#destroy_detailsObject



93
94
95
# File 'lib/can_be/processor/instance.rb', line 93

def destroy_details
  @model.send(@details_name).destroy
end

#destroy_historiesObject



82
83
84
85
86
87
# File 'lib/can_be/processor/instance.rb', line 82

def destroy_histories
  histories = history_model_class.where(can_be_model_id: @model.id)

  destroy_details_history(histories)
  histories.destroy_all
end

#destroy_history(details_type) ⇒ Object



89
90
91
# File 'lib/can_be/processor/instance.rb', line 89

def destroy_history(details_type)
  history_model_class.where(can_be_model_id: @model.id, can_be_details_type: details_type).destroy_all
end

#field_valueObject



41
42
43
# File 'lib/can_be/processor/instance.rb', line 41

def field_value
  @model.read_attribute(@field_name)
end

#field_value=(t) ⇒ Object



36
37
38
39
# File 'lib/can_be/processor/instance.rb', line 36

def field_value=(t)
  set_details(t)
  @model.send(:write_attribute, @field_name, t)
end

#initialize_detailsObject



49
50
51
# File 'lib/can_be/processor/instance.rb', line 49

def initialize_details
  set_details(field_value.to_sym) if has_details? && !@model.send(@details_id)
end

#save_historyObject



73
74
75
76
77
78
79
80
# File 'lib/can_be/processor/instance.rb', line 73

def save_history
  history_model_class.create({
    can_be_model_id: @model.id,
    can_be_type: field_value,
    can_be_details_id: @model.send(@details_name).id,
    can_be_details_type: details_class_name(field_value)
  }) unless history_model_for(field_value)
end

#set_cleaning_defaultsObject



68
69
70
71
# File 'lib/can_be/processor/instance.rb', line 68

def set_cleaning_defaults
  @original_details = nil
  @force_history_removal = false
end

#set_default_field_valueObject



45
46
47
# File 'lib/can_be/processor/instance.rb', line 45

def set_default_field_value
  self.field_value = @config.default_type if self.field_value.nil?
end

#update_field(t, options = {}) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/can_be/processor/instance.rb', line 18

def update_field(t, options = {})
  @original_details = @model.send(@details_name)
  @force_history_removal = options[:force_history_removal] if options.has_key?(:force_history_removal)

  save = options.has_key?(:save) ? options[:save] : false

  if save
    @model.update_attributes(@field_name => t)
  else
    self.field_value = t
  end

  if block_given?
    yield(@model.send(@details_name))
    @model.send(@details_name).save if save
  end
end