Class: MagicResource::Container

Inherits:
Object
  • Object
show all
Defined in:
lib/magic-resource/container.rb

Constant Summary collapse

CONTEXT_PATTERNS =
{
  route: '#',
  translation: '.',
  template: '/',
  content: ':'
}.inject({}){ |mem, (type, separator)|
  mem[type] = {
    separator: separator,
    regexp: /^(?<name>\w+)?#{'(?:::(?<context>\w+))?' unless type == :content}(?:#{Regexp.quote(separator)}(?<target>\w+#{'\??' if type == :translation}))$/
  }; mem
}.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#collectionObject Also known as: resources

Returns the value of attribute collection.



16
17
18
# File 'lib/magic-resource/container.rb', line 16

def collection
  @collection
end

#contextObject

Returns the value of attribute context.



16
17
18
# File 'lib/magic-resource/container.rb', line 16

def context
  @context
end

#itemObject Also known as: resource

Returns the value of attribute item.



16
17
18
# File 'lib/magic-resource/container.rb', line 16

def item
  @item
end

#nameObject

Returns the value of attribute name.



16
17
18
# File 'lib/magic-resource/container.rb', line 16

def name
  @name
end

Class Method Details

._launch(helper, *args) ⇒ Object



171
172
173
174
175
176
# File 'lib/magic-resource/container.rb', line 171

def self._launch(helper, *args)
  args.present? ?
    self.new(args.first)._launch(helper, true) :
    # Let's try to keep resource_container to be private..
    helper.send(:resource_container)._launch(helper)
end

Instance Method Details

#_launch(helper, changed = false) ⇒ Object



165
166
167
168
169
# File 'lib/magic-resource/container.rb', line 165

def _launch(helper, changed = false)
  self.helper = helper
  self.changed = changed
  self
end

#content(index) ⇒ Object



154
155
156
157
158
159
160
161
162
163
# File 'lib/magic-resource/container.rb', line 154

def content(index)
  MagicResource.assert_restriction(
    :disable_content_for_helper,
    "`#content helper is too much magical...: #{index}"
  )

  index = extract_context(index, :content)

  helper.resource_contents[rs].try(:[], index)
end

#content_for(index, *args, &block) ⇒ Object



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/magic-resource/container.rb', line 134

def content_for(index, *args, &block)
  MagicResource.assert_restriction(
    :disable_content_for_helper,
    "#content_for helper is too much magical...: #{index}"
  )

  content = if block_given?
    raise ArgumentError.new("Both content param and block specified: #{index}") if args.present?
    helper.capture(&block)
  else
    raise ArgumentError.new("Need to specify the content by second param or block: #{index}") unless args.present?
    args.first
  end

  index = extract_context(index, :content)

  helper.resource_contents[rs] ||= {}
  helper.resource_contents[rs][index] = content
end

#context?(*args) ⇒ Boolean

Returns:

  • (Boolean)


20
21
22
# File 'lib/magic-resource/container.rb', line 20

def context?(*args)
  args.include?(context)
end

#form_for(action, *args, &block) ⇒ Object



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/magic-resource/container.rb', line 118

def form_for(action, *args, &block)
  options = args.extract_options!
  form_type = args.shift

  action, ct, route_args = extract_route_context(action)
  with_context(ct) do
    url, method = route_to(action, route_args)

    form_type ||= MagicResource.default_form_type
    form_type, obj = form_type.kind_of?(Ransack::Search) ?
      [:search, form_type] :
      [form_type, item]
    helper.public_send(:"#{form_type}_form_for", obj, {url: url, method: method}.merge(options), &block)
  end
end


68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/magic-resource/container.rb', line 68

def link_to(action, *args, &block)
  options = args.extract_options!

  action, ct, route_args = extract_route_context(action)
  with_context(ct) do
    url, method = route_to(action, route_args)
    link_options = {}

    link_options[:method] = method unless method == :get

    confirm = options.delete(:confirm){method === :delete}
    confirm = :confirm_destroy if confirm === true
    confirm = t(confirm) if confirm.kind_of?(Symbol)
    link_options[:data] = {confirm: confirm} if confirm

    label = label_for_link(action, args.first, &block)

    helper.link_to(label, url, link_options.deep_merge(options))
  end
end


102
103
104
105
106
107
108
109
110
111
112
# File 'lib/magic-resource/container.rb', line 102

def link_to_if(condition, action, *args, &block)
  if condition
    link_to(action, *args, &block)
  else
    action, ct = extract_route_context(action)
    args.extract_options!
    with_context(ct) do
      label_for_link(action, args.first, &block)
    end
  end
end


114
115
116
# File 'lib/magic-resource/container.rb', line 114

def link_to_unless(condition, *args, &block)
  link_to_if(!condition, *args, &block)
end

#path_to(action) ⇒ Object

Used in controller and view



90
91
92
93
94
95
# File 'lib/magic-resource/container.rb', line 90

def path_to(action)
  action, ct, route_args = extract_route_context(action)
  with_context(ct) do
    route_to(action, route_args).first
  end
end

#redirect_to(action, options = {}) ⇒ Object

Used in controller



98
99
100
# File 'lib/magic-resource/container.rb', line 98

def redirect_to(action, options = {})
  helper.redirect_to path_to(action), options
end

#render(partial, *args) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/magic-resource/container.rb', line 24

def render(partial, *args)
  options = args.extract_options!
  if args.present? && form = args.shift
    raise 'Form object containing wrong resource' unless form.object == rs
    options[:f] = form unless options.has_key?(:f)
  end

  partial, ct = extract_context(partial, :template)

  lookup_templates = [name, :resources].
    flat_map{|lc| ["#{lc}/#{ct}", lc] }.
    map{|l| "#{l}/#{partial}"}

  template = lookup_templates.find{|t| helper.lookup_context.template_exists?(t, [], true)}
  raise("Can't find #{lookup_templates[0]} template for #{name} resource") unless template

  with_context(ct){helper.with_resource_container(self){helper.render(template, options)}}
end

#render_collection(partial, options = {}) ⇒ Object



43
44
45
46
# File 'lib/magic-resource/container.rb', line 43

def render_collection(partial, options = {})
  partial, ct = extract_context(partial, :template)
  collection.map{|r| helper.r(r).render("#{name}::#{ct}/#{partial}", options)}.join.html_safe
end

#render_collection_build(partial, options = {}) ⇒ Object



63
64
65
66
# File 'lib/magic-resource/container.rb', line 63

def render_collection_build(partial, options = {})
  partial, ct = extract_context(partial, :template)
  helper.r(resources.soft_build).render("#{name}::#{ct}/#{partial}", options)
end

#t(key, options = {}) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/magic-resource/container.rb', line 48

def t(key, options = {})
  key, ct = extract_context(key, :translation)

  lookup_keys = [name, :resources].
    flat_map{|lc| ["#{lc}.#{ct}", lc] }.
    map{|l| :"#{l}.#{key}"}

  helper.t(
    lookup_keys[0],
    options.merge(
      default: lookup_keys[1..-1] + Array.wrap(options[:default])
    )
  )
end