Class: Deface::Override

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

Constant Summary collapse

@@actions =
[:remove, :replace, :insert_after, :insert_before]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ Override

Initializes new override, you must supply only one Target, Action & Source parameter for each override (and any number of Optional parameters).

Target

  • :file_path - The relative file path of the template / partial where the override should take effect eg: “shared/_person”, “admin/posts/new” this will apply to all controller actions that use the specified template

  • :virtual_path - The controller and action name where the override should take effect eg: “controller/action” or “posts/index” will apply to all layouts, templates and partials that are used in the rendering of the specified action.

Action

  • :remove - Removes all elements that match the supplied selector

  • :replace - Replaces all elements that match the supplied selector

  • :insert_after - Inserts after all elements that match the supplied selector

  • :insert_before - Inserts before all elements that match the supplied selector

Source

  • :text - String containing markup

  • :partial - Relative path to partial

  • :template - Relative path to template

Optional

  • :name - Unique name for override so it can be identified and modified later. This needs to be unique within the same :virtual_path or :file_path



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/deface/override.rb', line 41

def initialize(args)
  @args = args

  if args.key?(:virtual_path)
    key = args[:virtual_path].to_sym

    @@virtual[key] ||= {}
    @@virtual[key][args[:name].to_s.parameterize] = self
  elsif args.key?(:file_path)
    key = args[:file_path]

    @@file[key] ||= {}
    @@file[key][args[:name].to_s.parameterize] = self
  end
end

Instance Attribute Details

#argsObject

Returns the value of attribute args.



4
5
6
# File 'lib/deface/override.rb', line 4

def args
  @args
end

Class Method Details

.find(details) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/deface/override.rb', line 77

def self.find(details)
  return [] unless self.virtual || self.file

  result = []

  virtual_path = details[:virtual_path]
  result << @@virtual[virtual_path.to_sym].try(:values) if virtual_path

  file_path = details[:file_path]
  result << @@file.map { |key,overrides| overrides.try(:values) if file_path =~ /#{key}/ } if file_path

  result.flatten.compact
end

Instance Method Details

#actionObject



61
62
63
# File 'lib/deface/override.rb', line 61

def action
  (@@actions & @args.keys).first
end

#selectorObject



57
58
59
# File 'lib/deface/override.rb', line 57

def selector
  @args[self.action]
end

#sourceObject



65
66
67
68
69
70
71
72
73
74
75
# File 'lib/deface/override.rb', line 65

def source
  erb = if @args.key? :partial
    load_template_source(@args[:partial], true)
  elsif @args.key? :template
    load_template_source(@args[:template], false)
  elsif @args.key? :text
    @args[:text]
  end

  Deface::Parser::ERB.compile(erb)
end