Class: RecurringTodos::AbstractRecurringTodosBuilder

Inherits:
Object
  • Object
show all
Defined in:
app/models/recurring_todos/abstract_recurring_todos_builder.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(user, attributes, pattern_class) ⇒ AbstractRecurringTodosBuilder

Returns a new instance of AbstractRecurringTodosBuilder


7
8
9
10
11
12
13
14
15
16
17
18
# File 'app/models/recurring_todos/abstract_recurring_todos_builder.rb', line 7

def initialize(user, attributes, pattern_class)
  @user  = user
  @saved = false

  @attributes           = attributes
  @selector             = get_selector(selector_key)
  @filterred_attributes = filter_attributes(@attributes)
  @mapped_attributes    = map_attributes(@filterred_attributes)

  @pattern              = pattern_class.new(user)
  @pattern.attributes   = @mapped_attributes
end

Instance Attribute Details

#mapped_attributesObject (readonly)

Returns the value of attribute mapped_attributes


5
6
7
# File 'app/models/recurring_todos/abstract_recurring_todos_builder.rb', line 5

def mapped_attributes
  @mapped_attributes
end

#patternObject (readonly)

Returns the value of attribute pattern


5
6
7
# File 'app/models/recurring_todos/abstract_recurring_todos_builder.rb', line 5

def pattern
  @pattern
end

Instance Method Details

#attributesObject


49
50
51
# File 'app/models/recurring_todos/abstract_recurring_todos_builder.rb', line 49

def attributes
  @pattern.attributes
end

#attributes_to_filterObject

Raises:

  • (Exception.new)

57
58
59
# File 'app/models/recurring_todos/abstract_recurring_todos_builder.rb', line 57

def attributes_to_filter
  raise Exception.new, "attributes_to_filter should be overridden"
end

#buildObject

build does not add tags. For tags, the recurring todos needs to be saved


21
22
23
# File 'app/models/recurring_todos/abstract_recurring_todos_builder.rb', line 21

def build
  @recurring_todo = @pattern.build_recurring_todo(@mapped_attributes)
end

#errorsObject


53
54
55
# File 'app/models/recurring_todos/abstract_recurring_todos_builder.rb', line 53

def errors
  @recurring_todo.try(:errors)
end

#filter_attributes(attributes) ⇒ Object


61
62
63
64
65
66
67
68
# File 'app/models/recurring_todos/abstract_recurring_todos_builder.rb', line 61

def filter_attributes(attributes)
  # get pattern independend attributes
  filterred_attributes = filter_generic_attributes(attributes)
  # append pattern specific attributes
  attributes_to_filter.each{|key| filterred_attributes[key]= attributes[key] if attributes.key?(key)}

  filterred_attributes
end

#filter_generic_attributes(attributes) ⇒ Object


70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'app/models/recurring_todos/abstract_recurring_todos_builder.rb', line 70

def filter_generic_attributes(attributes)
  return Tracks::AttributeHandler.new(@user, {
    recurring_period:     attributes[:recurring_period],
    description:          attributes[:description],
    notes:                attributes[:notes],
    tag_list:             tag_list_or_empty_string(attributes),
    start_from:           attributes[:start_from],
    end_date:             attributes[:end_date],
    ends_on:              attributes[:ends_on],
    number_of_occurences: attributes[:number_of_occurences],
    project:              attributes[:project],
    context:              attributes[:context],
    project_id:           attributes[:project_id],
    context_id:           attributes[:context_id],
    target:               attributes[:recurring_target],
    show_from_delta:      attributes[:recurring_show_days_before],
    show_always:          attributes[:recurring_show_always]
  })
end

#get_selector(key) ⇒ Object

Raises:

  • (Exception.new)

107
108
109
110
111
112
113
114
115
116
117
# File 'app/models/recurring_todos/abstract_recurring_todos_builder.rb', line 107

def get_selector(key)
  return nil if key.nil?

  raise Exception.new, "recurrence selector pattern (#{key}) not given" unless @attributes.selector_key_present?(key)
  selector = @attributes[key]

  raise Exception.new, "unknown recurrence selector pattern: '#{selector}'" unless valid_selector?(selector)

  @attributes = @attributes.except(key)
  return selector
end

#map(mapping, key, source_key) ⇒ Object

helper method to be used in mapped_attributes in subclasses changes name of key from source_key to key


97
98
99
100
# File 'app/models/recurring_todos/abstract_recurring_todos_builder.rb', line 97

def map(mapping, key, source_key)
  mapping[key] = mapping[source_key]
  mapping.except(source_key)
end

#map_attributesObject


90
91
92
93
# File 'app/models/recurring_todos/abstract_recurring_todos_builder.rb', line 90

def map_attributes
  # should be overwritten by subclasses to map attributes to activerecord model attributes
  @filterred_attributes
end

#saveObject


30
31
32
33
# File 'app/models/recurring_todos/abstract_recurring_todos_builder.rb', line 30

def save
  build
  save_recurring_todo
end

#save_collection(collection, collection_id) ⇒ Object (private)


136
137
138
139
140
141
142
# File 'app/models/recurring_todos/abstract_recurring_todos_builder.rb', line 136

def save_collection(collection, collection_id)
  # save object (project or context) and add its id to @mapped_attributes and remove the object from the attributes
  object = @mapped_attributes[collection]
  object.save
  @mapped_attributes[collection_id] = object.id
  @mapped_attributes.except(collection)
end

#save_contextObject


39
40
41
# File 'app/models/recurring_todos/abstract_recurring_todos_builder.rb', line 39

def save_context
  save_collection(:context, :context_id)
end

#save_projectObject


35
36
37
# File 'app/models/recurring_todos/abstract_recurring_todos_builder.rb', line 35

def save_project
  save_collection(:project, :project_id)
end

#save_recurring_todoObject (private)


125
126
127
128
129
# File 'app/models/recurring_todos/abstract_recurring_todos_builder.rb', line 125

def save_recurring_todo
  @saved = @recurring_todo.save
  save_tags if @saved
  return @saved
end

#save_tagsObject (private)


131
132
133
134
# File 'app/models/recurring_todos/abstract_recurring_todos_builder.rb', line 131

def save_tags
  @recurring_todo.tag_with(@filterred_attributes[:tag_list]) if @filterred_attributes[:tag_list].present?
  @recurring_todo.reload
end

#saved_recurring_todoObject

Raises:

  • (Exception.new)

43
44
45
46
47
# File 'app/models/recurring_todos/abstract_recurring_todos_builder.rb', line 43

def saved_recurring_todo
  raise(Exception.new, @recurring_todo.valid? ? "Recurring todo was not saved yet" : "Recurring todos was not saved because of validation errors") unless @saved

  @recurring_todo
end

#selector_keyObject

should return period specific selector like yearly_selector or daily_selector

Raises:

  • (Exception.new)

103
104
105
# File 'app/models/recurring_todos/abstract_recurring_todos_builder.rb', line 103

def selector_key
  raise Exception.new, "selector_key should be overridden in subclass of AbstractRecurringTodosBuilder"
end

#tag_list_or_empty_string(attributes) ⇒ Object (private)


144
145
146
147
# File 'app/models/recurring_todos/abstract_recurring_todos_builder.rb', line 144

def tag_list_or_empty_string(attributes)
  # avoid nil
  attributes[:tag_list].blank? ? "" : attributes[:tag_list].strip
end

#update(recurring_todo) ⇒ Object


25
26
27
28
# File 'app/models/recurring_todos/abstract_recurring_todos_builder.rb', line 25

def update(recurring_todo)
  @recurring_todo = @pattern.update_recurring_todo(recurring_todo, @mapped_attributes)
  save_recurring_todo
end

#valid_selector?(selector) ⇒ Boolean

Returns:

  • (Boolean)

Raises:

  • (Exception.new)

119
120
121
# File 'app/models/recurring_todos/abstract_recurring_todos_builder.rb', line 119

def valid_selector?(selector)
  raise Exception.new, "valid_selector? should be overridden in subclass of AbstractRecurringTodosBuilder"
end