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



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

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] = "#{Kilt::Formatting.singular_name_of(type)} successfully updated"
      redirect_to edit_object_path(type.pluralize, object.slug)
    else
      flash[:error] = "Couldn't save #{Kilt::Formatting.singular_name_of(type)}"
      redirect_to new_object_path(type.pluralize)
    end
  end
  
end

#deleteObject

Delete an object



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

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

#editObject

View/Edit a specific object



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

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

#indexObject

Show all the object types



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

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



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

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

#newObject

Create a new object



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

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

#updateObject

Update an object



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

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] = "#{Kilt::Formatting.singular_name_of(type)} successfully updated"
    else
      flash[:error] = "Couldn't save #{Kilt::Formatting.singular_name_of(type)}"
    end
  
    # redirect to the edit screen
    redirect_to edit_object_path(type.pluralize, object.slug)
  end
end