Sha256: cd2f71a9b9e46b44652124f727fae07b668681b9380661250f3c9390b91521e1

Contents?: true

Size: 1.91 KB

Versions: 3

Compression:

Stored size: 1.91 KB

Contents

module Formic
  class Form < Base
    default_template 'formic/default/form'

    attr_reader :model, :action

    def _initialize options={}, &block
      super options, &block
      self.options[:method] ||= 'POST'
    end

    def put options={}, &block
      self.options[:method ] = 'POST'
      self.options[:_method] = 'PUT'
      self.content = block if block_given?
      return self
    end

    def create model, options={}, &block
      options[:title] ||= "New #{model.class.to_s.underscore}".gsub('_', ' ').titleize
      @action = :create

      return _initialize_with_model(
        model,
        options[:action] || self.page.url_for(model),
        "create_#{model.class.to_s.downcase}_form",
        options,
        &block
      )
    end

    def edit model, options={}, &block
      self.put

      @action = :edit
      options[:title] ||= "Edit #{model.class.to_s.underscore}".gsub('_', ' ').titleize
      return _initialize_with_model(
        model,
        options[:action] || self.page.url_for(model),
        "edit_#{model.class.to_s.downcase}_form",
        options,
        &block
      )
    end

    def method_missing action, model, options={}, &block
      @action = action
      options[:title] ||= "#{action} #{model.class.to_s.underscore}".gsub('_', ' ').titleize
      return _initialize_with_model(
        model,
        options[:action] || "#{self.page.url_for(model)}/#{action}",
        "#{action}_#{model.class.to_s.downcase}_form",
        options,
        &block
      )
    end

    def _initialize_with_model model, action, classname, options={}, &block
      options[:title] ||= "#{action} #{model.class.to_s.underscore}".gsub('_', ' ').titleize
      @model = model
      self.merge_options(options)
      self.options[:method] ||= 'POST'
      self.options[:action] ||= action
      self.options[:class].push classname
      self.content = block if block_given?
      return self
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
formic-0.2.5 lib/formic/form.rb
formic-0.2.1 lib/formic/form.rb
formic-0.1.0 lib/formic/form.rb