Module: Rtt::InteractiveConfigurator

Defined in:
lib/rtt/interactive_configurator.rb

Instance Method Summary collapse

Instance Method Details

#configure_client(name = nil) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/rtt/interactive_configurator.rb', line 5

def configure_client(name = nil)
  say "Please fill in your Client information"
  say "======================================"
  client = if name.blank?
   if (active_client = Client.where(:active => true).first) && agree_or_enter("Want to modify current")
     active_client
   else
     modify_name_or_create_model(:client, name)
   end
  else
    modify_name_or_create_model(:client, name)
  end
  activate = !Client.current_active? || !client.active && agree_or_enter("Make this client current")
  client.description = name
  if !client.active && activate
    client.activate
  else
    client.save
  end
  client
end

#configure_project(name = nil, client_name = nil) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/rtt/interactive_configurator.rb', line 27

def configure_project(name = nil, client_name = nil)
  say "Please fill in your Project information"
  say "======================================="
  project = if name.blank?
      project_name = ask_or_default('Project name', "Project name:", name, /^\w+$/)
      Project.find_or_create_by_name(project_name)
    else
      modify_name_or_create_model(:project, name)
  end
  rate = ask_or_default('Project rate', "Project rate:", (project.rate if project.present?),/^[\d]+(\.[\d]+){0,1}$/)
  client_found = false
  while !client_found
    client_name=(ask("Client name:") { |q| q.validate = /^\w+$/ }) if client_name.blank?
    client = Client.where(:name => client_name).first
    if client.blank?
      say "A Client with this name is not registered."
      create_client = agree_or_enter("Want to created a Client with that name")
      if create_client
        client = Client.create :name => client_name, :description => client_name
        client_found = true
      else
        say "Please try enter a new client name."
        client_name = nil
      end
    else
      client_found = true
    end
  end
  project.description = project.name
  project.client = client
  project.rate = rate
  project.save
  if !Project.current_active? || !project.active && agree_or_enter("Make this project (and it's client) to be the current one(s)")
    project.activate
    client.activate unless client.active
  end
  project
end

#configure_task(name = nil, conditions = {}) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/rtt/interactive_configurator.rb', line 92

def configure_task(name = nil, conditions = {})
  conditions.merge!(name.blank? ? { :active => true } : { :name => name })
  task = if name.blank?
    Task.where(conditions).first
    else
     Task.where(conditions).first || Task.create(conditions)
  end
  if task.present?
      say "Modify the task information (with name: #{task.name})"
      say "================================"
      name = unless agree_or_enter('Want to keep current name')
              ask("Name:") { |q| q.validate = /^\w+$/ }
             else
              task.name
      end
      rate = ask_or_default('rate', "Rate:", (task.rate if task.present?), /^[\d]+(\.[\d]+){0,1}$/)
      task.rate = rate.to_f
      task.name = name
      date= ask_or_default('Date', "Date [Format: DD-MM-YYYY]:", (task.date.strftime("%d-%m-%Y") if task.present? && task.date.present?), /^\d{2,2}-\d{2,2}-\d{4,4}$/)
      task.date = Date.parse(date) if date.present? && task.date != date
      task.start_at = date
      task.end_at = date
      duration = ask_or_default('duration', "Duration:", (task.duration if task.present?), /^(\d{1,2})[hH]{1,2}(\d{1,2})[mM]{1,2}$/)
      task.duration=(duration) if duration.present?
      project_name = ask_or_default('project', 'Project name:', (task.project.name if task.present? && task.project.present?), /^\w+$/)
      task.project=(build_project_if_not_exists(project_name)) if task.project.blank? || project_name != task.project.name
      user_name = ask_or_default('user', 'User nickname:', (task.user.nickname if task.present? && task.user.present?), /^\w+$/)
      task.user=(build_user_if_not_exists(user_name)) if task.user.blank? || user_name != task.user.nickname
      task.save
      task
  else
      name.blank? ?
      say("There is no active task to configure. Please add the name of task, to this command, to modify it.") :
      say("There is no task with that name. Please check the available tasks with 'rtt list'.")
  end
end

#configure_user(nickname = nil, skip_name = false) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/rtt/interactive_configurator.rb', line 66

def configure_user(nickname = nil, skip_name = false)
  say "Please fill in your personal information"
  say "========================================"
  if !skip_name || nickname.blank? || nickname == User::DEFAULT_NICK
    nickname = ask_or_default('nickname', 'Nickname (Required):', nickname, /^\w+$/)
  end
  existing = User.where(:nickname => nickname).first
  first_name = ask_or_default('first name', "First name:", (existing.first_name if existing.present?))
  last_name = ask_or_default('last name', 'Last name:', (existing.last_name if existing.present?))
  company = ask_or_default('company', 'Company:', (existing.company if existing.present?))
  country = ask_or_default('country', 'Country:', (existing.country if existing.present?))
  city = ask_or_default('city', 'City:', (existing.city if existing.present?))
  address = ask_or_default('address', 'Address:', (existing.address if existing.present?))
  phone = ask_or_default('phone', 'Phone:', (existing.phone if existing.present?))
  email = ask_or_default('email', 'Email:', (existing.email if existing.present?))
  site = ask_or_default('site', 'Site:', (existing.site if existing.present?))
  user_attributes = { :nickname => nickname, :first_name => first_name, :last_name => last_name, :company => company, :email => email, :address => address, :country => country, :city => city, :phone => phone, :site => site, :active => true }
  if existing.present?
    existing.attributes = user_attributes
    existing.save
    existing
  else
    User.create(user_attributes)
  end
end