Module: Perus::Server::Admin

Included in:
App
Defined in:
lib/perus/server/admin.rb

Instance Method Summary collapse

Instance Method Details

#admin(type, redirect_to_record = false) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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
65
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/perus/server/admin.rb', line 3

def admin(type, redirect_to_record = false)
    plural   = type.to_s.pluralize
    singular = plural.singularize
    title    = singular.titleize
    klass    = title

    class_eval "
        use Sinatra.new {
            helpers Helpers

            before do
                @singular = '#{singular}'
                @plural = '#{plural}'
                @title = '#{title}'
                load_site_information
            end

            # list
            get '/admin/#{plural}' do
                protected!
                @records = #{klass}.dataset.order_by(:name).all
                erb :'admin/index'
            end

            # new form
            get '/admin/#{plural}/new' do
                protected!
                @record = #{klass}.new
                @form = Form.new(@record)
                erb :'admin/new'
            end

            # create
            post '/admin/#{plural}' do
                protected!
                @record = #{klass}.new(params[:record])
                if @record.valid?
                    begin
                        @record.save
                        if #{redirect_to_record}
                            redirect url_prefix + 'admin/#{plural}/' + @record.id.to_s
                        else
                            redirect url_prefix + 'admin/#{plural}'
                        end
                        return
                    rescue
                    end
                end

                @form = Form.new(@record)
                erb :'admin/new'
            end

            # edit
            get '/admin/#{plural}/:id' do
                protected!
                @record = #{klass}.with_pk!(params['id'])
                @form = Form.new(@record)
                erb :'admin/edit'
            end

            # update
            put '/admin/#{plural}/:id' do
                protected!
                @record = #{klass}.with_pk!(params['id'])
                if @record.valid?
                    begin
                        @record.update(params[:record])
                        redirect url_prefix + 'admin/#{plural}'
                        return
                    rescue
                    end
                end
                
                @form = Form.new(@record)
                erb :'admin/edit'
            end

            # delete
            delete '/admin/#{plural}/:id' do
                protected!
                @record = #{klass}.with_pk!(params['id'])
                @record.destroy
                redirect url_prefix + 'admin/#{plural}'
            end
        }
    "
end