Sha256: 0e89f7ccc83dc614039a673510cb7ca6703379d7e978fb053116f73b8bdeeef1

Contents?: true

Size: 1.75 KB

Versions: 3

Compression:

Stored size: 1.75 KB

Contents

module ActionView::Helpers::FormHelper
private

  def self.restrict(name)
    module_eval <<-"end_eval", __FILE__, __LINE__
      alias old_#{name} #{name}
      def #{name}(object, method, options = {})
        restricted_input(:old_#{name}, object, method, options) \
      end
    end_eval
  end

  def restricted_input(old_name, object, method, options, *extra, &block)
    o = instance_variable_get("@" + object)
    writable = o.writable?(method)
    if o.readable?(method)
      if writable
	if block
	  return yield(object, method, options, extra)
	else
	  return send(old_name, object, method, options)
	end
      else
        return o.send(method)
      end
    elseif writable
      options[:value] = ''
      if block
	return yield(object, method, options, extra)
      else
	return send(old_name, object, method, options)
      end
    end
  end

  once ('@aliasesDone') { # Evaluating this twice would break it.
    alias old_check_box check_box
    alias old_radio_button radio_button
  }

  SimpleMethods = \
   ['file_field', 'hidden_field', 'password_field', 'text_area', 'text_field']

public

  once ('@simpleMethodsDone') { # because it calls alias.
    SimpleMethods.each { | name | restrict(name) }
  }

  def check_box(object, method, options = {}, checked_value = "1", unchecked_value = "0")
    restricted_input(:old_check_box, object, method, options, checked_value, unchecked_value) {
      | object, method, options, extra |
      old_check_box(object, method, options, extra[0], extra[1])
    } 
  end

  def radio_button(object, method, tag_value, options = {})
    restricted_input(:old_radio_button, object, method, options, tag_value) {
      | object, method, options, extra |
      old_radio_button(object, method, extra[0], options)
    }
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
model_security_generator-0.0.3 templates/helpers/model_security_helper.rb
model_security_generator-0.0.1 templates/model_security_helper.rb
model_security_generator-0.0.2 templates/helpers/model_security_helper.rb