Class: ActionCrud::Helpers::Route

Inherits:
Object
  • Object
show all
Defined in:
lib/action_crud/helpers/route.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(context, record = nil, action = nil, *options) ⇒ Route

Intialize url finder



7
8
9
10
11
12
13
14
# File 'lib/action_crud/helpers/route.rb', line 7

def initialize(context, record=nil, action=nil, *options)
  @context = context
  @record  = record || @context.try(:current_record)
  @action  = action
  @options = options
  @path    = route_uri :path
  @url     = route_uri :url
end

Instance Attribute Details

#actionObject

Returns the value of attribute action.



4
5
6
# File 'lib/action_crud/helpers/route.rb', line 4

def action
  @action
end

#optionsObject

Returns the value of attribute options.



4
5
6
# File 'lib/action_crud/helpers/route.rb', line 4

def options
  @options
end

#pathObject

Returns the value of attribute path.



4
5
6
# File 'lib/action_crud/helpers/route.rb', line 4

def path
  @path
end

#recordObject

Returns the value of attribute record.



4
5
6
# File 'lib/action_crud/helpers/route.rb', line 4

def record
  @record
end

#urlObject

Returns the value of attribute url.



4
5
6
# File 'lib/action_crud/helpers/route.rb', line 4

def url
  @url
end

Instance Method Details

#index?Boolean

Is index action

Returns:

  • (Boolean)


22
23
24
# File 'lib/action_crud/helpers/route.rb', line 22

def index?
  action == :index
end

#prefix?Boolean

Should prefix method

Returns:

  • (Boolean)


27
28
29
# File 'lib/action_crud/helpers/route.rb', line 27

def prefix?
  action.in? [:new, :edit]
end

#record?Boolean

Should include record

Returns:

  • (Boolean)


32
33
34
# File 'lib/action_crud/helpers/route.rb', line 32

def record?
  action.in? [:show, :edit, :delete, :destroy]
end

#route_keyObject

Find route key



37
38
39
40
# File 'lib/action_crud/helpers/route.rb', line 37

def route_key
  singular = 'singular_' unless index?
  record.model_name.try :"#{singular}route_key"
end

#route_uri(type) ⇒ Object

Find route method



43
44
45
46
47
48
49
50
# File 'lib/action_crud/helpers/route.rb', line 43

def route_uri(type)
  args   = [*options]
  args   = [record, *options] if record?
  method = "#{route_key}_#{type}"
  method = "#{action}_#{method}" if prefix?

  @context.try :"#{method}", *args
end

#to_s(type = :path) ⇒ Object

To string



17
18
19
# File 'lib/action_crud/helpers/route.rb', line 17

def to_s(type=:path)
  instance_variable_get("@#{type}").to_s
end