Class: Fencepost::Fencepost

Inherits:
Object
  • Object
show all
Defined in:
lib/fencepost/fencepost.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params) ⇒ Fencepost

Returns a new instance of Fencepost.



5
6
7
8
9
10
11
12
13
# 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

Instance Attribute Details

#gateObject (readonly)

Returns the value of attribute gate.



3
4
5
# File 'lib/fencepost/fencepost.rb', line 3

def gate
  @gate
end

Class Method Details

.generate_model_listObject



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# 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

.modelsObject



15
16
17
# File 'lib/fencepost/fencepost.rb', line 15

def self.models
  ActiveRecord::Base.descendants - [ActiveRecord::SchemaMigration]
end

Instance Method Details

#allow(elements) ⇒ Object



45
46
47
48
# File 'lib/fencepost/fencepost.rb', line 45

def allow(elements)
  gate.allow(elements)
  self
end

#deny(elements) ⇒ Object



50
51
52
53
# File 'lib/fencepost/fencepost.rb', line 50

def deny(elements)
  gate.deny(elements)
  self
end

#ensure_modelsObject



37
38
39
40
41
42
43
# 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