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.


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

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

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

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

Instance Attribute Details

#mapped_attributesObject (readonly)

Returns the value of attribute mapped_attributes


3
4
5
# File 'app/models/recurring_todos/abstract_recurring_todos_builder.rb', line 3

def mapped_attributes
  @mapped_attributes
end

#patternObject (readonly)

Returns the value of attribute pattern


3
4
5
# File 'app/models/recurring_todos/abstract_recurring_todos_builder.rb', line 3

def pattern
  @pattern
end

Instance Method Details

#attributesObject


47
48
49
# File 'app/models/recurring_todos/abstract_recurring_todos_builder.rb', line 47

def attributes
  @pattern.attributes
end

#attributes_to_filterObject

Raises:

  • (Exception.new)

55
56
57
# File 'app/models/recurring_todos/abstract_recurring_todos_builder.rb', line 55

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


19
20
21
# File 'app/models/recurring_todos/abstract_recurring_todos_builder.rb', line 19

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

#errorsObject


51
52
53
# File 'app/models/recurring_todos/abstract_recurring_todos_builder.rb', line 51

def errors
  @recurring_todo.try(:errors)
end

#filter_attributes(attributes) ⇒ Object


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

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

  filtered_attributes
end

#filter_generic_attributes(attributes) ⇒ Object


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

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_occurrences: attributes[:number_of_occurrences],
    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)

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

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


95
96
97
98
# File 'app/models/recurring_todos/abstract_recurring_todos_builder.rb', line 95

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

#map_attributesObject


88
89
90
91
# File 'app/models/recurring_todos/abstract_recurring_todos_builder.rb', line 88

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

#saveObject


28
29
30
31
# File 'app/models/recurring_todos/abstract_recurring_todos_builder.rb', line 28

def save
  build
  save_recurring_todo
end

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


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

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


37
38
39
# File 'app/models/recurring_todos/abstract_recurring_todos_builder.rb', line 37

def save_context
  save_collection(:context, :context_id)
end

#save_projectObject


33
34
35
# File 'app/models/recurring_todos/abstract_recurring_todos_builder.rb', line 33

def save_project
  save_collection(:project, :project_id)
end

#save_recurring_todoObject (private)


123
124
125
126
127
# File 'app/models/recurring_todos/abstract_recurring_todos_builder.rb', line 123

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

#save_tagsObject (private)


129
130
131
132
# File 'app/models/recurring_todos/abstract_recurring_todos_builder.rb', line 129

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

#saved_recurring_todoObject

Raises:

  • (Exception.new)

41
42
43
44
45
# File 'app/models/recurring_todos/abstract_recurring_todos_builder.rb', line 41

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)

101
102
103
# File 'app/models/recurring_todos/abstract_recurring_todos_builder.rb', line 101

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

#tag_list_or_empty_string(attributes) ⇒ Object (private)


142
143
144
145
# File 'app/models/recurring_todos/abstract_recurring_todos_builder.rb', line 142

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

#update(recurring_todo) ⇒ Object


23
24
25
26
# File 'app/models/recurring_todos/abstract_recurring_todos_builder.rb', line 23

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)

117
118
119
# File 'app/models/recurring_todos/abstract_recurring_todos_builder.rb', line 117

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