Class: Kilt::KiltController

Inherits:
ActionController::Base
  • Object
show all
Defined in:
app/controllers/kilt/kilt_controller.rb

Instance Method Summary collapse

Instance Method Details

#createObject

Post back a new object



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
# File 'app/controllers/kilt/kilt_controller.rb', line 34

def create
  type = params[:types].singularize
  
  # Get the params
  p = params[type]
  
  # Ensure we have a name
  if p['name'] == ''
    flash[:error] = "Name is required"
    redirect_to new_object_path(type.pluralize)
  else
    # Create an object and fill it with the values passed in by the form
    object = Kilt::Object.new(type)
    object.fill(p)
    
    # Call Kilt's update method
    if Kilt.create(object)
      flash[:notice] = "#{type.capitalize} successfully updated"
      redirect_to edit_object_path(type.pluralize, object.slug)
    else
      flash[:error] = "Couldn't save #{type.capitalize}"
      redirect_to new_object_path(type.pluralize)
    end
  end
  
end

#deleteObject

Delete an object



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'app/controllers/kilt/kilt_controller.rb', line 98

def delete
  type = params[:types].singularize
  slug = params[:slug]
  object = Kilt.get(slug)
  
  # Call Kilt's update method
  if Kilt.delete(slug)
    flash[:notice] = "#{type.capitalize} successfully deleted"
  else
    flash[:error] = "Couldn't delete #{type.capitalize}"
  end
  
  # redirect to the list screen
  redirect_to list_path(type.pluralize)
end

#editObject

View/Edit a specific object



62
63
64
65
66
# File 'app/controllers/kilt/kilt_controller.rb', line 62

def edit
  @type = params[:types].singularize
  @slug = params[:slug]
  @object = Kilt.get(@slug)
end

#indexObject

Show all the object types



9
10
11
12
13
14
15
16
17
18
19
# File 'app/controllers/kilt/kilt_controller.rb', line 9

def index
  @types = {}
  type_names = Kilt.types
  type_names.each do |type|
    if Kilt.send(type).name != nil
      @types[type] = Kilt.send(type).name
    else
      @types[type] = type.pluralize.capitalize
    end
  end
end

#listObject

Show the list of items for a specific object type



22
23
24
25
# File 'app/controllers/kilt/kilt_controller.rb', line 22

def list
  @type = params[:types].singularize
  @objects = Kilt.send(@type.pluralize).order('name')
end

#newObject

Create a new object



28
29
30
31
# File 'app/controllers/kilt/kilt_controller.rb', line 28

def new
  @type = params[:types].singularize
  @object = Kilt::Object.new(@type)
end

#updateObject

Update an object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'app/controllers/kilt/kilt_controller.rb', line 69

def update
  type = params[:types].singularize
  slug = params[:slug]
  
  # Get the params
  p = params[type]
  
  # Ensure we have a name
  if p['name'] == ''
    flash[:error] = "Name is required"
    redirect_to edit_object_path(type.pluralize, slug)
  else
    # Create an object and fill it with the values passed in by the form
    object = Kilt.get(slug)
    object.fill(p)
  
    # Call Kilt's update method
    if Kilt.update(slug, object)
      flash[:notice] = "#{type.capitalize} successfully updated"
    else
      flash[:error] = "Couldn't save #{type.capitalize}"
    end
  
    # redirect to the edit screen
    redirect_to edit_object_path(type.pluralize, object.slug)
  end
end