class ActionAdmin::Shortcode

Public Class Methods

new(params, attribs={}) click to toggle source

Initializes the decorator

# File lib/action_admin/shortcode.rb, line 4
def initialize(params, attribs={})
  attribs = Hash[attribs.map { |k, _v| [k, nil] }].symbolize_keys
  string  = URI.decode(params[:shortcode])

  @field  = params[:id]
  @object = parse_shortcode_attr(string, attribs)
end

Public Instance Methods

has_attribute?(attribute) click to toggle source

Check if attribute exists

# File lib/action_admin/shortcode.rb, line 27
def has_attribute?(attribute)
  @object.key? attribute
end
method_missing(method, *args, &block) click to toggle source

Delegates to the wrapped object

# File lib/action_admin/shortcode.rb, line 18
def method_missing(method, *args, &block)
  if @object.key? method
    @object[method]
  elsif @object.respond_to? method
    @object.send(method, *args, &block)
  end
end
model_name() click to toggle source

Sets the model name

# File lib/action_admin/shortcode.rb, line 13
def model_name
  ActiveModel::Name.new(self.class, nil, @field)
end

Private Instance Methods

parse_shortcode_attr(string, attribs) click to toggle source

Parses shortcode string and returns attributes

# File lib/action_admin/shortcode.rb, line 45
def parse_shortcode_attr(string, attribs)
  found = Hash["#{string}".scan(/(\w+)="([^"]+)"/)].symbolize_keys
  valid = found.select { |k, _v| k.in? attribs.keys }
  valid = Hash[valid.map { |k, v| k == :ids ? [k, v.split(',').map(&:strip)] : [k, v] }]

  valid_shortcode?(string) ? attribs.merge(valid) : attribs
end
parse_shortcode_field(string) click to toggle source

Parses shortcode string and returns field name

# File lib/action_admin/shortcode.rb, line 40
def parse_shortcode_field(string)
  "#{string}".match(/^\[(\w+) /).captures.first
end
valid_shortcode?(string) click to toggle source

Checks if shortcode string is valid

# File lib/action_admin/shortcode.rb, line 34
def valid_shortcode?(string)
  "#{string}".strip.match(/^\[(\w+) (.+?)\]$/).present? and
  parse_shortcode_field(string) == "#{@field}"
end