Class: Perus::Server::Action

Inherits:
Sequel::Model
  • Object
show all
Defined in:
lib/perus/server/models/action.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.add(system_id, params) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/perus/server/models/action.rb', line 71

def self.add(system_id, params)
    action = Action.new
    action.system_id = system_id

    if params['script_id']
        action.script_id = params['script_id']
    else
        command_config = CommandConfig.create_with_params(params)
        action.command_config_id = command_config.id
    end

    begin
        action.save
    rescue
        if action.command_config_id
            CommandConfig.with_pk!(action.command_config_id).destroy
        end
    end
end

Instance Method Details

#after_destroyObject



59
60
61
62
63
64
65
66
67
68
69
# File 'lib/perus/server/models/action.rb', line 59

def after_destroy
    super
    
    if command_config_id
        command_config.destroy
    end

    if file
        File.unlink(file_path)
    end
end

#command_nameObject



24
25
26
27
28
29
30
# File 'lib/perus/server/models/action.rb', line 24

def command_name
    if script_id
        script.name
    else
        command_config.command
    end
end

#config_hashObject



12
13
14
15
16
17
18
19
20
21
22
# File 'lib/perus/server/models/action.rb', line 12

def config_hash
    if command_config_id
        hash = command_config.config_hash
    else
        hash = script.config_hash
    end

    # replace the command config/script id with the action's id
    hash['id'] = id
    hash
end

#file_nameObject



40
41
42
# File 'lib/perus/server/models/action.rb', line 40

def file_name
    file['original_name']
end

#file_pathObject



50
51
52
# File 'lib/perus/server/models/action.rb', line 50

def file_path
    File.join(system.uploads_dir, file['filename'])
end

#file_urlObject



44
45
46
47
48
# File 'lib/perus/server/models/action.rb', line 44

def file_url
    prefix = URI(Server.options.uploads_url)
    path = File.join(system_id.to_s, file['filename'])
    (prefix + path).to_s
end

#optionsObject



32
33
34
35
36
37
38
# File 'lib/perus/server/models/action.rb', line 32

def options
    if script_id
        {}
    else
        command_config.options
    end
end

#validateObject



54
55
56
57
# File 'lib/perus/server/models/action.rb', line 54

def validate
    super
    validates_presence :system_id
end