Kiss A simpler, smarter web application framework for Ruby

Kiss::Action Subclass of Object

Attributes

actionread-only

Not documented

action_subdirread-only

Not documented

action_uriread-only

Not documented

argsread-only

Not documented

controllerread-only

Not documented

extensionread-only

Not documented

formsread-only

Not documented

outputread-only

Not documented

output_optionsread-only

Not documented

paramsread-only

Not documented

templateread-only

Not documented

Public Class Methods

clear_subclasses() source

Not documented

    # File lib/kiss/action.rb, line 7
      def clear_subclasses
        @subclasses = {}
      end
    
get_subclass(key, path, controller) source

Not documented

    # File lib/kiss/action.rb, line 11
      def get_subclass(key, path, controller)
        # each subdir must get a subclass of the parent dir class;
        # each subclass has its own @subclasses hash of its subdirs
        
        src, updated = controller.file_cache(path, true)
        
        if updated
          klass = Class.new(self)
          klass.class_eval(src,path) if src
          @subclasses[key] = klass
        end
        
        @subclasses[key]
      end
    
inherited(subclass) source

Not documented

    # File lib/kiss/action.rb, line 4
      def inherited(subclass)
        subclass.clear_subclasses
      end
    
new(controller,action,action_uri,action_subdir,extension,args,params = nil) source

Creates a new action instance from controller data.

    # File lib/kiss/action.rb, line 49
    def initialize(controller,action,action_uri,action_subdir,extension,args,params = nil)
      @controller = controller
      @action = @template = action
      @action_uri = action_uri
      @action_subdir = action_subdir
      @extension = extension
      @args = args
      @params = params || {}
      
      @data = {}
      @forms = {}
      @template_dir = @controller.template_dir
    end
    

Public Instance Methods

add_form(attrs = {}) source

Creates and adds form to current action, using specified attributes.

    # File lib/kiss/action.rb, line 238
    def add_form(attrs = {})
      # make sure form not already defined for this request
      @forms ||= {}
      attrs[:name] ||= @action
      form_name = attrs[:name]
      raise "page contains multiple forms named '#{form_name}'" if @forms.has_key?(form_name)
      
      # create form
      uri = app + request.path_info
      @forms[form_name] = @form = Kiss::Form.new({
        :action => uri,
        :request => self
      }.merge(attrs))
      
      # add data from controller to form
      if params['form'] == @form.name
        @form.submitted = true
        @form.params = params
      end
      
      @form
    end
    
app_absolute_path(path) source

Convert specified action path by prefixing current action_subdir, unless specified path starts with slash (/).

    # File lib/kiss/action.rb, line 162
    def app_absolute_path(path)
      path = @action_subdir + path if path !~ /\A\//
      path
    end
    
arg(index) source

Not documented

    # File lib/kiss/action.rb, line 34
    def arg(index)
      @args[index]
    end
    
arg_object(table, index = 0, desired_values = {}) source

Returns database model object from specified table, with id matching argument of specified index (defaults to first argument). Raises error if object not found in database or object data does not match desired_values.

    # File lib/kiss/action.rb, line 93
    def arg_object(table, index = 0, desired_values = {})
      get_object_by_id(table, arg(index).to_i, desired_values)
    end
    
arg_object_or_new(table,index = 0,desired_values = {}) source

Returns arg_object, or new object of specified table if arg_object fails.

    # File lib/kiss/action.rb, line 98
    def arg_object_or_new(table,index = 0,desired_values = {})
      arg_object(table,index,desired_values) rescue dbm[table].new(desired_values)
    end
    
args() source

Not documented

    # File lib/kiss/action.rb, line 38
    def args
      @args
    end
    
authenticate() source

Does nothing by default; override this method in your _action.rb or other action files to specify authentication behavior.

    # File lib/kiss/action.rb, line 222
    def authenticate; end
    
call() source

Placeholder for generic actions that do nothing but render template. render is called from Kiss#call after this method returns.

    # File lib/kiss/action.rb, line 218
    def call; end
    
check_desired_values(object,desired_values) source

Raises error if database model object does not match the key=value data in desired_values hash.

    # File lib/kiss/action.rb, line 72
    def check_desired_values(object,desired_values)
      desired_values.each_pair do |key,value|
        unless object[key] == value
          raise "#{object.table.to_s.singularize} id=#{object.id} does not have #{key} == #{value.inspect}"
        end
      end
      object
    end
    
expand_login() source

Does nothing by default; override this method in your _action.rb or other action files to expand objects from persistent login data.

    # File lib/kiss/action.rb, line 226
    def expand_login; end
    
force_https(options = {}) source

Alias for force_ssl

force_ssl(options = {}) source

Ensure that action requested via SSL; redirects to same action with https protocol if protocol is not https.

    # File lib/kiss/action.rb, line 230
    def force_ssl(options = {})
      unless protocol == 'https'
        redirect_action(@action, options.merge( :protocol => 'https' ))
      end
    end
    
Also aliased as: force_https
form() source

Returns most recently added form. Shorthand for forms[0] when there is only one form.

    # File lib/kiss/action.rb, line 263
    def form
      @form
    end
    
form_action_match() source

Returns true is `form’ param matches current action path.

    # File lib/kiss/action.rb, line 64
    def form_action_match
      form = params['form']
      return false unless form =~ /^\/?\w+(\/\w+)*$/s
      return form == @action || @action =~ /\/#{form}/
    end
    
get_object_by_id(table, id, desired_values = {}) source

Verifies valid id value, selects database model object from specified table with specified id, and checks result to match desired_values.

    # File lib/kiss/action.rb, line 83
    def get_object_by_id(table, id, desired_values = {})
      id = id.to_i
      raise 'bad object id' unless id > 0
      result = dbm[table][id] || (raise "#{table.to_s.singularize} id=#{id} not found")
      check_desired_values(result,desired_values)
    end
    
invoke(path,options = {}) source

Not documented

    # File lib/kiss/action.rb, line 154
    def invoke(path,options = {})
      action_handler = controller.invoke_action(path,options[:params] || {},{:layout => nil})
      action_handler.output
    end
    
Also aliased as: process_action
method_missing(meth) source

Raises custom error message for missing methods, specifying current action more clearly than standard error message.

    # File lib/kiss/action.rb, line 44
    def method_missing(meth)
      raise NoMethodError, "undefined method `#{meth}'"
    end
    
param_object(table, key = nil, desired_values = {}) source

Returns database model object from specified table, with id matching param of specified key (defaults to singular form of table name). Raises error if object not found in database or object data does not match desired_values.

    # File lib/kiss/action.rb, line 105
    def param_object(table, key = nil, desired_values = {})
      key ||= table.to_s.singularize
      get_object_by_id(table, params[key.to_s], desired_values)
    end
    
param_object_or_new(table,key = nil,desired_values = {}) source

Returns param_object, or new object of specified table if param_object fails.

    # File lib/kiss/action.rb, line 111
    def param_object_or_new(table,key = nil,desired_values = {})
      param_object(table,key,desired_values) rescue dbm[table].new(desired_values)
    end
    
process_action(path,options = {}) source

Alias for invoke

redirect_action(action, options = {}) source

Redirects to specified action path, which may also include arguments.

    # File lib/kiss/action.rb, line 146
    def redirect_action(action, options = {})
      redirect_url( app(options) + app_absolute_path(action) + (options[:params] ?
        '?' + options[:params].keys.map do |k|
          "#{Kiss.url_escape k.to_s}=#{Kiss.url_escape options[:params][k].to_s}"
        end.join('&') : '')
      )
    end
    
redirect_url(*args) source

Sends HTTP 302 response to redirect client agent to specified URL.

    # File lib/kiss/action.rb, line 141
    def redirect_url(*args)
      controller.redirect_url(*args)
    end
    
render(options = {}) source

Render and return response to Rack.

    # File lib/kiss/action.rb, line 210
    def render(options = {})
      @output = render_to_string(options)
      @output_options = options
      throw :kiss_action_done
    end
    
render_to_string(options) source

Return string result from rendering specified string or template options.

    # File lib/kiss/action.rb, line 168
    def render_to_string(options)
      if options.is_a?(String)
        return options
      elsif options[:text]
        return options[:text].to_s
      end
      
      @base_url ||= app + action_uri
      
      unless defined?(@layout)
        @layout = options.is_a?(Hash) && options.has_key?(:layout) ? options[:layout] : 
          (extension == 'rhtml' ? Kiss.layout : nil)
      end
      content = options[:content].is_a?(String) ? options[:content] : process({
        :template => template,
        :extension => extension
      }.merge(options))
      
      while @layout
        layout_path = get_template_path(
          :template => @layout,
          :extension => options[:extension] || extension
        ) rescue break
      
        # clear layout
        # (however, layout template is allowed to set @layout to something else)
        @layout = nil
        
        # process layout file
        content = erubis(layout_path,binding,content)
      end
      # add a base tag in case of arguments appended to request URI
      
      base_url = options.has_key?(:base_url) ? options[:base_url] : @base_url
      if (base_url)
        content = Kiss.html_prepend(%Q(<base href="#{base_url}" />),content,'head')
      end
      
      content
    end
    
send_email(options = {}) source

Creates and invokes new Kiss::Mailer instance to send email message via SMTP.

    # File lib/kiss/action.rb, line 133
    def send_email(options = {})
      Kiss::Mailer.new({
        :data => data,
        :controller => controller
      }.merge(options)).send
    end
    
sendmail(options = {}) source

Creates and invokes new Kiss::Mailer instance to send email message via sendmail.

    # File lib/kiss/action.rb, line 126
    def sendmail(options = {})
      send_email({
        :engine => :sendmail
      }.merge(options))
    end
    
validate_arg(index, format, required = false) source

Validates argument of specified index against specified format.

    # File lib/kiss/action.rb, line 121
    def validate_arg(index, format, required = false)
      Kiss.validate_value(@args[index], format, required, "arg #{index}")
    end
    
validate_param(key, format, required = false) source

Validates param of specified key against specified format.

    # File lib/kiss/action.rb, line 116
    def validate_param(key, format, required = false)
      Kiss.validate_value(@params[key], format, required, "param '#{key}'")
    end
    

Disabled; run with $DEBUG to generate this.