Class: Rodauth::Feature

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

Constant Summary collapse

DEPRECATED_ARGS =

:nocov:

[]
DEFAULT_REDIRECT_BLOCK =
proc{default_redirect}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#allowed_undefined_configuration_methodsObject

Returns the value of attribute allowed_undefined_configuration_methods.



150
151
152
# File 'lib/rodauth.rb', line 150

def allowed_undefined_configuration_methods
  @allowed_undefined_configuration_methods
end

#configurationObject

Returns the value of attribute configuration.



149
150
151
# File 'lib/rodauth.rb', line 149

def configuration
  @configuration
end

#dependenciesObject

Returns the value of attribute dependencies.



147
148
149
# File 'lib/rodauth.rb', line 147

def dependencies
  @dependencies
end

#feature_nameObject

Returns the value of attribute feature_name.



146
147
148
# File 'lib/rodauth.rb', line 146

def feature_name
  @feature_name
end

#instance_variables_usedObject (readonly)

Returns the value of attribute instance_variables_used.



152
153
154
# File 'lib/rodauth.rb', line 152

def instance_variables_used
  @instance_variables_used
end

#internal_request_methodsObject (readonly)

Returns the value of attribute internal_request_methods.



151
152
153
# File 'lib/rodauth.rb', line 151

def internal_request_methods
  @internal_request_methods
end

#routesObject

Returns the value of attribute routes.



148
149
150
# File 'lib/rodauth.rb', line 148

def routes
  @routes
end

Class Method Details

.define(name, constant = nil, &block) ⇒ Object



180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/rodauth.rb', line 180

def self.define(name, constant=nil, &block)
  feature = new
  feature.dependencies = []
  feature.routes = []
  feature.feature_name = name
  configuration = feature.configuration = FeatureConfiguration.new
  feature.module_eval(&block)
  configuration.def_configuration_methods(feature)

  # :nocov:
  if constant
  # :nocov:
    Rodauth.const_set(constant, feature)
    Rodauth::FeatureConfiguration.const_set(constant, configuration)
  end

  FEATURES[name] = feature
end

Instance Method Details

#additional_form_tags(name = feature_name) ⇒ Object



313
314
315
# File 'lib/rodauth.rb', line 313

def additional_form_tags(name=feature_name)
  auth_value_method(:"#{name}_additional_form_tags", nil)
end

#auth_cached_method(meth, iv = :"@#{meth}") ⇒ Object

:nocov:



352
353
354
355
356
357
358
359
360
361
362
363
364
365
# File 'lib/rodauth.rb', line 352

def auth_cached_method(meth, iv=:"@#{meth}") # :nodoc:
  # Non-shape friendly historical method.
  # RODAUTH3: Remove
  umeth = :"_#{meth}"
  define_method(meth) do
    if instance_variable_defined?(iv)
      instance_variable_get(iv)
    else
      instance_variable_set(iv, send(umeth))
    end
  end
  alias_method(meth, meth)
  auth_private_methods(meth)
end

#auth_value_method(meth, value) ⇒ Object



327
328
329
330
# File 'lib/rodauth.rb', line 327

def auth_value_method(meth, value)
  define_method(meth){value}
  auth_value_methods(meth)
end

#cached_auth_method(meth, iv = :"@#{meth}") ⇒ Object

Auth caching method that treats a nil instance variable as not being cached. If nil is a valid value for the instance variable, do not use this method, use a regular auth_method and handle caching manually.



341
342
343
344
345
346
347
348
349
# File 'lib/rodauth.rb', line 341

def cached_auth_method(meth, iv=:"@#{meth}")
  umeth = :"_#{meth}"
  define_method(meth) do
    v = instance_variable_get(iv)
    v.nil? ? instance_variable_set(iv, send(umeth)) : v
  end
  alias_method(meth, meth)
  auth_private_methods(meth)
end

#configuration_module_eval(&block) ⇒ Object



207
208
209
# File 'lib/rodauth.rb', line 207

def configuration_module_eval(&block)
  configuration.module_eval(&block)
end

#def_deprecated_alias(new, old) ⇒ Object

:nocov:



218
219
220
221
222
223
224
225
226
227
228
229
# File 'lib/rodauth.rb', line 218

def def_deprecated_alias(new, old)
  configuration_module_eval do
    define_method(old) do |*a, &block|
      warn("Deprecated #{old} method used during configuration, switch to using #{new}", *DEPRECATED_ARGS)
      send(new, *a, &block)
    end
  end
  define_method(old) do
    warn("Deprecated #{old} method called at runtime, switch to using #{new}", *DEPRECATED_ARGS)
    send(new)
  end
end

#depends(*deps) ⇒ Object



272
273
274
# File 'lib/rodauth.rb', line 272

def depends(*deps)
  dependencies.concat(deps)
end

#email(type, subject, opts = OPTS) ⇒ Object



286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
# File 'lib/rodauth.rb', line 286

def email(type, subject, opts = OPTS)
  subject_method = :"#{type}_email_subject"
  body_method = :"#{type}_email_body"
  create_method = :"create_#{type}_email"
  send_method = :"send_#{type}_email"

  translatable_method subject_method, subject
  auth_methods create_method, send_method

  body_template = "#{type.to_s.tr('_', '-')}-email"
  if opts[:translatable]
    auth_value_methods body_method
    define_method(body_method){translate(body_method, render(body_template))}
  else
    auth_methods body_method
    define_method(body_method){render(body_template)}
  end

  define_method(create_method) do
    create_email(send(subject_method), send(body_method))
  end

  define_method(send_method) do
    send_email(send(create_method))
  end
end

#flash_key(meth, value) ⇒ Object



322
323
324
325
# File 'lib/rodauth.rb', line 322

def flash_key(meth, value)
  define_method(meth){normalize_session_or_flash_key(value)}
  auth_value_methods(meth)
end

#internal_request_method(name = feature_name) ⇒ Object



199
200
201
# File 'lib/rodauth.rb', line 199

def internal_request_method(name=feature_name)
  (@internal_request_methods ||= []) << name
end

#loaded_templates(v) ⇒ Object



265
266
267
268
269
270
# File 'lib/rodauth.rb', line 265

def loaded_templates(v)
  define_method(:loaded_templates) do
    super().concat(v)
  end
  private :loaded_templates
end

#redirect(name = feature_name, &block) ⇒ Object



232
233
234
235
236
237
# File 'lib/rodauth.rb', line 232

def redirect(name=feature_name, &block)
  meth = :"#{name}_redirect"
  block ||= DEFAULT_REDIRECT_BLOCK
  define_method(meth, &block)
  auth_value_methods meth
end

#response(name = feature_name) ⇒ Object



249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
# File 'lib/rodauth.rb', line 249

def response(name=feature_name)
  meth = :"#{name}_response"
  overridable_meth = :"_#{meth}"
  notice_flash_meth = :"#{name}_notice_flash"
  redirect_meth = :"#{name}_redirect"
  define_method(overridable_meth) do
    set_notice_flash send(notice_flash_meth)
    redirect send(redirect_meth)
  end
  define_method(meth) do
    require_response(overridable_meth)
  end
  private overridable_meth, meth
  auth_private_methods meth
end

#route(name = feature_name, default = name.to_s.tr('_', '-'), &block) ⇒ Object



154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/rodauth.rb', line 154

def route(name=feature_name, default=name.to_s.tr('_', '-'), &block)
  route_meth = :"#{name}_route"
  auth_value_method route_meth, default

  define_method(:"#{name}_path"){|opts=OPTS| route_path(send(route_meth), opts) if send(route_meth)}
  define_method(:"#{name}_url"){|opts=OPTS| route_url(send(route_meth), opts) if send(route_meth)}

  handle_meth = :"handle_#{name}"
  internal_handle_meth = :"_#{handle_meth}"
  before route_meth
  define_method(internal_handle_meth, &block)

  define_method(handle_meth) do
    request.is send(route_meth) do
      @current_route = name
      check_csrf if check_csrf?
      _around_rodauth do
        before_rodauth
        send(internal_handle_meth, request)
      end
    end
  end

  routes << handle_meth
end

#session_key(meth, value) ⇒ Object



317
318
319
320
# File 'lib/rodauth.rb', line 317

def session_key(meth, value)
  define_method(meth){convert_session_key(value)}
  auth_value_methods(meth)
end

#translatable_method(meth, value) ⇒ Object



332
333
334
335
# File 'lib/rodauth.rb', line 332

def translatable_method(meth, value)
  define_method(meth){translate(meth, value)}
  auth_value_methods(meth)
end

#uses_instance_variables(*ivs) ⇒ Object



203
204
205
# File 'lib/rodauth.rb', line 203

def uses_instance_variables(*ivs)
  @instance_variables_used = ivs.freeze
end

#view(page, title, name = feature_name) ⇒ Object



239
240
241
242
243
244
245
246
247
# File 'lib/rodauth.rb', line 239

def view(page, title, name=feature_name)
  meth = :"#{name}_view"
  title_meth = :"#{name}_page_title"
  translatable_method(title_meth, title)
  define_method(meth) do
    view(page, send(title_meth))
  end
  auth_methods meth
end