Class: Tracks::AttributeHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/tracks/attribute_handler.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(user, attributes) ⇒ AttributeHandler

Returns a new instance of AttributeHandler.



5
6
7
8
9
# File 'lib/tracks/attribute_handler.rb', line 5

def initialize(user, attributes)
  @user = user
  @orig_attributes = attributes
  @attributes = normalize(attributes)
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



3
4
5
# File 'lib/tracks/attribute_handler.rb', line 3

def attributes
  @attributes
end

Instance Method Details

#[](attribute) ⇒ Object



15
16
17
# File 'lib/tracks/attribute_handler.rb', line 15

def [](attribute)
  get attribute
end

#[]=(attribute, value) ⇒ Object



27
28
29
# File 'lib/tracks/attribute_handler.rb', line 27

def []=(attribute, value)
  set attribute, value
end

#attribute_with_id_of(object_type) ⇒ Object



67
68
69
70
# File 'lib/tracks/attribute_handler.rb', line 67

def attribute_with_id_of(object_type)
  map = { project: 'project_id', context: 'context_id' }
  get map[object_type]
end

#context_nameObject



104
105
106
# File 'lib/tracks/attribute_handler.rb', line 104

def context_name
  get(:context_name).try(:strip)
end

#context_specified_by_name?Boolean

Returns:

  • (Boolean)


94
95
96
97
98
# File 'lib/tracks/attribute_handler.rb', line 94

def context_specified_by_name?
  return false if get(:context_id).present?
  return false if context_name.blank?
  true
end

#except(key) ⇒ Object



31
32
33
# File 'lib/tracks/attribute_handler.rb', line 31

def except(key)
  AttributeHandler.new(@user, @attributes.except(key.to_sym))
end

#find_or_create_by_name(relation, name) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
# File 'lib/tracks/attribute_handler.rb', line 72

def find_or_create_by_name(relation, name)
  new_object_created = false

  object = relation.where(:name => name).first
  unless object
    object = relation.build(:name => name)
    new_object_created = true
  end

  return object, new_object_created
end

#get(attribute) ⇒ Object



11
12
13
# File 'lib/tracks/attribute_handler.rb', line 11

def get(attribute)
  @attributes[attribute.to_sym]
end

#key?(key) ⇒ Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/tracks/attribute_handler.rb', line 35

def key?(key)
  @attributes.key?(key.to_sym)
end

#normalize(attributes) ⇒ Object



108
109
110
111
# File 'lib/tracks/attribute_handler.rb', line 108

def normalize(attributes)
  # make sure the hash keys are all symbols
  attributes.transform_keys(&:to_sym)
end

#object_name(object_type) ⇒ Object



63
64
65
# File 'lib/tracks/attribute_handler.rb', line 63

def object_name(object_type)
  send("#{object_type}_name")
end

#parse_collection(object_type, relation) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/tracks/attribute_handler.rb', line 47

def parse_collection(object_type, relation)
  object = nil
  new_object_created = false

  if specified_by_name?(object_type)
    object, new_object_created = find_or_create_by_name(relation, object_name(object_type))
    # put id of object in @attributes, i.e. set :project_id to project.id
    @attributes["#{object_type.to_s}_id".to_sym] = object.id unless new_object_created
  else
    # find context or project by its id
    object = attribute_with_id_of(object_type).present? ? relation.find(attribute_with_id_of(object_type)) : nil
  end
  @attributes[object_type] = object
  return object, new_object_created
end

#parse_date(date) ⇒ Object



43
44
45
# File 'lib/tracks/attribute_handler.rb', line 43

def parse_date(date)
  set(date, @user.prefs.parse_date(get(date)))
end

#project_nameObject



100
101
102
# File 'lib/tracks/attribute_handler.rb', line 100

def project_name
  get(:project_name).try(:strip)
end

#project_specified_by_name?Boolean

Returns:

  • (Boolean)


88
89
90
91
92
# File 'lib/tracks/attribute_handler.rb', line 88

def project_specified_by_name?
  return false if get(:project_id).present?
  return false if project_name.blank?
  true
end

#safe_attributesObject



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/tracks/attribute_handler.rb', line 113

def safe_attributes
  ActionController::Parameters.new(attributes).permit(
  :context, :project,
  # model attributes
  :context_id, :project_id, :description, :notes, :state, :start_from,
  :ends_on, :end_date, :number_of_occurrences, :occurrences_count, :target,
  :show_from_delta, :recurring_period, :recurrence_selector, :every_other1,
  :every_other2, :every_other3, :every_day, :only_work_days, :every_count,
  :weekday, :show_always, :context_name, :project_name, :tag_list,
  # form attributes
  :recurring_period, :daily_selector, :monthly_selector, :yearly_selector,
  :recurring_target, :daily_every_x_days, :monthly_day_of_week,
  :monthly_every_x_day, :monthly_every_x_month2, :monthly_every_x_month,
  :monthly_every_xth_day, :recurring_show_days_before,
  :recurring_show_always, :weekly_every_x_week, :weekly_return_monday,
  :yearly_day_of_week, :yearly_every_x_day, :yearly_every_xth_day,
  :yearly_month_of_year2, :yearly_month_of_year,
  # derived attributes
  :weekly_return_monday, :weekly_return_tuesday, :weekly_return_wednesday,
  :weekly_return_thursday, :weekly_return_friday, :weekly_return_saturday, :weekly_return_sunday
  )
end

#selector_key_present?(key) ⇒ Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/tracks/attribute_handler.rb', line 39

def selector_key_present?(key)
  key?(key)
end

#set(key, value) ⇒ Object



19
20
21
# File 'lib/tracks/attribute_handler.rb', line 19

def set(key, value)
  @attributes[key.to_sym] = value
end

#set_if_nil(key, value) ⇒ Object



23
24
25
# File 'lib/tracks/attribute_handler.rb', line 23

def set_if_nil(key, value)
  @attributes[key.to_sym] ||= value
end

#specified_by_name?(object_type) ⇒ Boolean

Returns:

  • (Boolean)


84
85
86
# File 'lib/tracks/attribute_handler.rb', line 84

def specified_by_name?(object_type)
  send("#{object_type}_specified_by_name?")
end