Module: Gritano::CLI::Helpers

Included in:
Console::Base
Defined in:
lib/gritano/helpers.rb

Instance Method Summary collapse

Instance Method Details

#create_model(model, params) ⇒ Object



4
5
6
7
8
9
10
11
# File 'lib/gritano/helpers.rb', line 4

def create_model(model, params)
  instance = model.new(params)
  if instance.save
    render_text "#{model.name.split(':')[-1].downcase} was successfully created."
  else
    render_text "an error occurred.", :error
  end
end

#destroy_model(model, params) ⇒ Object



21
22
23
24
25
26
27
28
29
# File 'lib/gritano/helpers.rb', line 21

def destroy_model(model, params)
  instance = model.where(params).first
  if instance
    instance.destroy
    render_text "#{model.name.split(':')[-1].downcase} was successfully destroyed."
  else
    render_text "#{model.name.split(':')[-1].downcase} doens't exist.", :error
  end
end

#try_change_access(user, repo, add_or_rm, read_or_write) ⇒ Object



39
40
41
42
43
44
45
# File 'lib/gritano/helpers.rb', line 39

def try_change_access(user, repo, add_or_rm, read_or_write)
  if user.send("#{add_or_rm}_access", repo, read_or_write)
    render_text "done"
  else
    render_text "an error occurred", :error
  end
end

#update_model(instance, params) ⇒ Object



13
14
15
16
17
18
19
# File 'lib/gritano/helpers.rb', line 13

def update_model(instance, params)
  if instance.update_attributes(params)
    render_text "#{instance.class.name.split(':')[-1].downcase} was successfully updated."
  else
    render_text "an error occurred.", :error
  end
end

#use_if_not_nil(*variables, &block) ⇒ Object



31
32
33
34
35
36
37
# File 'lib/gritano/helpers.rb', line 31

def use_if_not_nil(*variables, &block)
  unless variables.index(nil)
    block.call(*variables)
  else
    render_text "an error occurred", :error
  end
end

#valid_options?(options) ⇒ Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/gritano/helpers.rb', line 47

def valid_options?(options)
  true unless options.empty?
end