Module: ActiveRestrictors::View

Defined in:
lib/active_restrictors/active_restrictor_views.rb

Instance Method Summary collapse

Instance Method Details

#display_full_restrictors(obj, *args) ⇒ Object

obj

Instance with restrictors enabled

args

argument hash :

:val_join

string to join values with

:include_disabled

includes all restrictors

val_join

String to join restictor values together

Provides array of enabled restrictors in the form of:

[restrictor_name_label, string_of_restriction_values]


11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/active_restrictors/active_restrictor_views.rb', line 11

def display_full_restrictors(obj, *args)
  arg_h = args.last.is_a?(Hash) ? args.last : {}
  res = []
  if(args.size == 1 && args.first.is_a?(String))
    val_join = args.first
  else
    val_join = arg_h[:val_join] || '<br />'
  end
  if(obj.class.respond_to?(:full_restrictors))
    r_args = arg_h[:include_disabled] ? [:include_disabled] : []
    res = obj.class.full_restrictors(*r_args).map do |restrictor|
      [label(obj.class.name.camelize, restrictor[:name]),
        obj.send(restrictor[:name]).map(&restrictor[:views][:value].to_sym).join(val_join).html_safe]
    end
  end
  res
end

#edit_full_restrictors(obj, form, args = {}) ⇒ Object

obj

Instance with restrictors enabled

form

Form object to attach restrictor fields to

args

Argument hash ->

:include_disabled

includes all restrictors

Provides form items for restrictors in an array of format:

[restrictor_name_label, form_selection_string]


35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/active_restrictors/active_restrictor_views.rb', line 35

def edit_full_restrictors(obj, form, args={})
  r_args = args[:include_disabled] ? [:include_disabled] : []
  if(obj.class.respond_to?(:full_restrictors))
    obj.class.full_restrictors(*r_args).map do |restrictor|
      if(restrictor[:views][:user_values_only])
        if(restrictor[:views][:user_values_only].respond_to?(:call))
          user = restrictor[:views][:user_values_only].call
          if(user)
            values = user.send(restrictor[:name].to_sym).find(:all, :order => restrictor[:value])
          else
            values = restrictor[:views][:user_values_only].send(restrictor[:name].to_sym).find(:all, :order => restrictor[:views][:value])
          end
        end
      end
      @_restrictor_inflector_helper ||= Class.send(:include, ActiveSupport::Inflector).new
      values = restrictor[:class].find(:all, :order => restrictor[:views][:value]) unless values
      [
        form.label(restrictor[:name]),
        form.collection_select(
          "#{@_restrictor_inflector_helper.singularize(restrictor[:name])}_ids",
          values,
          restrictor[:views][:id],
          restrictor[:views][:value],
          {
            :include_blank => restrictor[:views][:include_blank],
            :selected => Array(obj.send(restrictor[:name])).map(&restrictor[:views][:id].to_sym)
          },
          :multiple => restrictor[:views][:multiple]
        )
      ]
    end
  else
    []
  end
end