class Fencepost::Fencepost

Attributes

gate[R]

Public Class Methods

generate_model_list() click to toggle source
# File lib/fencepost/fencepost.rb, line 19
def self.generate_model_list
  model_list = {}
  models.each do |model|
    model_list[self.param_key(model)] =
    {
      model: model,
      attributes: self.attribute_keys(model),
      demodulized_name: self.demodulized_name(model),
      nested_collection_name: self.nested_collection_name(model),
      nested_attributes_name: self.nested_attributes_name(model),
      nested_attributes_options: self.nested_attributes_options(model),
      nested_singular_name: self.nested_singular_attributes_name(model)
    }
  end

  model_list
end
models() click to toggle source
# File lib/fencepost/fencepost.rb, line 15
def self.models
  ActiveRecord::Base.descendants - [ActiveRecord::SchemaMigration]
end
new(params) click to toggle source
# File lib/fencepost/fencepost.rb, line 5
def initialize(params)
  ensure_models
  @gate = Gate.new(self.class.model_list, params)
  self.class.models.each do |model|
    define_singleton_method self.class.method_name(model) do
      gate.open(params, self.class.param_key(model), model)
    end
  end
end

Private Class Methods

always_forbidden_attributes() click to toggle source
# File lib/fencepost/fencepost.rb, line 83
def self.always_forbidden_attributes
  [:created_at, :updated_at]
end
attribute_keys(model) click to toggle source
# File lib/fencepost/fencepost.rb, line 87
def self.attribute_keys(model)
  begin
    a = model.new.attributes.keys.map {|k| k.to_sym} unless model.nil? || model.abstract_class
    (a || []) - self.always_forbidden_attributes
  rescue
    []
  end
end
demodulized_name(model) click to toggle source
# File lib/fencepost/fencepost.rb, line 65
def self.demodulized_name(model)
  "#{model.name.demodulize.underscore.gsub("/", "_")}".to_sym
end
method_name(model) click to toggle source
# File lib/fencepost/fencepost.rb, line 57
def self.method_name(model)
  "#{model.name.underscore.gsub("/", "_")}_params".to_sym
end
nested_attributes_name(model) click to toggle source
# File lib/fencepost/fencepost.rb, line 73
def self.nested_attributes_name(model)
  a ="#{model.name.pluralize.demodulize.underscore.gsub("/", "_")}_attributes"
  a.to_sym
end
nested_attributes_options(model) click to toggle source
# File lib/fencepost/fencepost.rb, line 96
def self.nested_attributes_options(model)
  model.nested_attributes_options
end
nested_collection_name(model) click to toggle source
# File lib/fencepost/fencepost.rb, line 69
def self.nested_collection_name(model)
  "#{model.name.pluralize.demodulize.underscore.gsub("/", "_")}".to_sym
end
nested_singular_attributes_name(model) click to toggle source
# File lib/fencepost/fencepost.rb, line 78
def self.nested_singular_attributes_name(model)
  a ="#{model.name.demodulize.underscore.gsub("/", "_")}_attributes"
  a.to_sym
end
param_key(model) click to toggle source
# File lib/fencepost/fencepost.rb, line 61
def self.param_key(model)
  "#{model.name.underscore.gsub("/", "_")}".to_sym
end

Public Instance Methods

allow(elements) click to toggle source
# File lib/fencepost/fencepost.rb, line 45
def allow(elements)
  gate.allow(elements)
  self
end
deny(elements) click to toggle source
# File lib/fencepost/fencepost.rb, line 50
def deny(elements)
  gate.deny(elements)
  self
end
ensure_models() click to toggle source
# File lib/fencepost/fencepost.rb, line 37
def ensure_models
  klass = self.class
  if ::Fencepost.configuration.dev_mode
    Rails.application.eager_load!
    klass.model_list = klass.generate_model_list
  end
end