Class: HumanRoutes::Context

Inherits:
Object
  • Object
show all
Defined in:
lib/human_routes/context.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(router, controller, options = {}) ⇒ Context

Returns a new instance of Context.



7
8
9
10
11
12
# File 'lib/human_routes/context.rb', line 7

def initialize(router, controller, options = {})
  @router = router
  @controller = controller
  @options = options
  @named_routes = {}
end

Instance Attribute Details

#controllerObject (readonly)

Returns the value of attribute controller.



5
6
7
# File 'lib/human_routes/context.rb', line 5

def controller
  @controller
end

#named_routesObject (readonly)

Returns the value of attribute named_routes.



5
6
7
# File 'lib/human_routes/context.rb', line 5

def named_routes
  @named_routes
end

#optionsObject (readonly)

Returns the value of attribute options.



5
6
7
# File 'lib/human_routes/context.rb', line 5

def options
  @options
end

#routerObject (readonly)

Returns the value of attribute router.



5
6
7
# File 'lib/human_routes/context.rb', line 5

def router
  @router
end

Instance Method Details

#allObject



135
136
137
138
139
140
141
# File 'lib/human_routes/context.rb', line 135

def all
  create
  update
  remove
  show
  list unless resource?
end

#controller_nameObject



14
15
16
# File 'lib/human_routes/context.rb', line 14

def controller_name
  @controller_name ||= options.delete(:name) { controller.to_s }
end

#create(*args) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/human_routes/context.rb', line 38

def create(*args)
  path, name, options = extract_route_args(
    segment: :new,
    default_name: "new_#{singular_controller_name}",
    args: args
  )

  match path, {
    via: :get,
    controller: controller,
    action: :new,
    as: name
  }.merge(options)

  match path, {
    via: :post,
    controller: controller,
    action: :create,
    as: ""
  }.merge(options)
end

#get(action, *args) ⇒ Object



143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/human_routes/context.rb', line 143

def get(action, *args)
  path, name, options = extract_route_args(
    segment: action,
    default_name: action.to_s,
    args: args
  )

  match path, {
    via: :get,
    controller: controller,
    action: action,
    as: name
  }.merge(options)
end

#list(*args) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/human_routes/context.rb', line 104

def list(*args)
  path, name, options = extract_route_args(
    segment: :list,
    default_name: controller_name,
    args: args
  )

  match path, {
    via: :get,
    controller: controller,
    action: :index,
    as: name
  }.merge(options)
end

#path_nameObject



30
31
32
# File 'lib/human_routes/context.rb', line 30

def path_name
  options[:path_name] || controller_name
end

#post(action, *args) ⇒ Object



158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/human_routes/context.rb', line 158

def post(action, *args)
  path, name, options = extract_route_args(
    segment: action,
    default_name: action.to_s,
    args: args
  )

  match path, {
    via: :post,
    controller: controller,
    action: action,
    as: named_routes[path] == name ? "" : name
  }.merge(options)
end

#remove(*args) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/human_routes/context.rb', line 82

def remove(*args)
  path, name, options = extract_route_args(
    segment: :remove,
    default_name: "remove_#{singular_controller_name}",
    args: args
  )

  match path, {
    via: :get,
    controller: controller,
    action: :remove,
    as: name
  }.merge(options)

  match path, {
    via: :post,
    controller: controller,
    action: :destroy,
    as: ""
  }.merge(options)
end

#resource?Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/human_routes/context.rb', line 26

def resource?
  options[:resource] || controller_name == singular_controller_name
end

#routesObject



34
35
36
# File 'lib/human_routes/context.rb', line 34

def routes
  @routes ||= []
end

#show(*args) ⇒ Object



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/human_routes/context.rb', line 119

def show(*args)
  path, name, options = extract_route_args(
    segment: :show,
    default_name: singular_controller_name,
    args: args,
    bare: true
  )

  match path, {
    via: :get,
    controller: controller,
    action: :show,
    as: name
  }.merge(options)
end

#singular_controller_nameObject



18
19
20
21
22
23
24
# File 'lib/human_routes/context.rb', line 18

def singular_controller_name
  @singular_controller_name ||= if options[:resource]
                                  controller_name
                                else
                                  controller_name.singularize
                                end
end

#update(*args) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/human_routes/context.rb', line 60

def update(*args)
  path, name, options = extract_route_args(
    segment: :edit,
    default_name: "edit_#{singular_controller_name}",
    args: args
  )

  match path, {
    via: :get,
    controller: controller,
    action: :edit,
    as: name
  }.merge(options)

  match path, {
    via: :post,
    controller: controller,
    action: :update,
    as: ""
  }.merge(options)
end