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



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

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

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes



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

def attributes
  @attributes
end

Instance Method Details

#[](attribute) ⇒ Object



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

def [](attribute)
  get attribute
end

#[]=(attribute, value) ⇒ Object



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

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

#attribute_with_id_of(object_type) ⇒ Object



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

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

#context_nameObject



106
107
108
# File 'lib/tracks/attribute_handler.rb', line 106

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

#context_specified_by_name?Boolean

Returns:

  • (Boolean)


96
97
98
99
100
# File 'lib/tracks/attribute_handler.rb', line 96

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

#except(key) ⇒ Object



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

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

#find_or_create_by_name(relation, name) ⇒ Object



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

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



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

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

#key?(key) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#normalize(attributes) ⇒ Object



110
111
112
113
# File 'lib/tracks/attribute_handler.rb', line 110

def normalize(attributes)
  # make sure the hash keys are all symbols
  Hash[attributes.map{|k,v| [k.to_sym,v]}]
end

#object_name(object_type) ⇒ Object



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

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

#parse_collection(object_type, relation) ⇒ Object



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

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



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

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

#project_nameObject



102
103
104
# File 'lib/tracks/attribute_handler.rb', line 102

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

#project_specified_by_name?Boolean

Returns:

  • (Boolean)


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

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

#safe_attributesObject



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

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_occurences, :occurences_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)


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

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

#set(key, value) ⇒ Object



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

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

#set_if_nil(key, value) ⇒ Object



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

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

#specified_by_name?(object_type) ⇒ Boolean

Returns:

  • (Boolean)


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

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