entitled 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/VERSION +1 -1
- data/entitled.gemspec +2 -2
- data/lib/entitled/entitled.rb +230 -0
- data/lib/entitled.rb +2 -229
- metadata +4 -4
- data/init.rb +0 -1
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.1.
|
1
|
+
0.1.1
|
data/entitled.gemspec
CHANGED
@@ -5,7 +5,7 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{entitled}
|
8
|
-
s.version = "0.1.
|
8
|
+
s.version = "0.1.1"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Marc Tauber"]
|
@@ -27,8 +27,8 @@ Gem::Specification.new do |s|
|
|
27
27
|
"Rakefile",
|
28
28
|
"VERSION",
|
29
29
|
"entitled.gemspec",
|
30
|
-
"init.rb",
|
31
30
|
"lib/entitled.rb",
|
31
|
+
"lib/entitled/entitled.rb",
|
32
32
|
"spec/entitled_spec.rb",
|
33
33
|
"spec/spec_helper.rb"
|
34
34
|
]
|
@@ -0,0 +1,230 @@
|
|
1
|
+
module Entitled
|
2
|
+
|
3
|
+
|
4
|
+
def self.included(controller)
|
5
|
+
controller.extend ClassMethods
|
6
|
+
end
|
7
|
+
|
8
|
+
|
9
|
+
module ClassMethods
|
10
|
+
|
11
|
+
def entitled options={}
|
12
|
+
|
13
|
+
helper_method :entitled_generate,
|
14
|
+
:entitled_breadcrumbs,
|
15
|
+
:entitled_headings,
|
16
|
+
:entitled_tag,
|
17
|
+
:entitled_up,
|
18
|
+
:entitled_resources?,
|
19
|
+
:entitled_resource?
|
20
|
+
|
21
|
+
Titles.per_page = options[:per_page] if options[:per_page]
|
22
|
+
Titles.prefix_exceptions = options[:prefix_exceptions] if options[:prefix_exceptions]
|
23
|
+
Titles.site_title = options[:site_title] if options[:site_title]
|
24
|
+
|
25
|
+
send :include, InstanceMethods
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
|
30
|
+
|
31
|
+
module InstanceMethods
|
32
|
+
|
33
|
+
def initialize
|
34
|
+
super
|
35
|
+
@entitled = Titles.new self
|
36
|
+
end
|
37
|
+
|
38
|
+
private
|
39
|
+
|
40
|
+
def method_missing(method, *args, &block)
|
41
|
+
if match_data = method.to_s.match(/^entitled_(.+)$/)
|
42
|
+
@entitled.send(match_data[1], *args, &block)
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
end
|
47
|
+
|
48
|
+
|
49
|
+
|
50
|
+
class Title
|
51
|
+
|
52
|
+
def resource?
|
53
|
+
false
|
54
|
+
end
|
55
|
+
|
56
|
+
def path?
|
57
|
+
path_object.present?
|
58
|
+
end
|
59
|
+
|
60
|
+
end
|
61
|
+
|
62
|
+
|
63
|
+
|
64
|
+
|
65
|
+
|
66
|
+
class ResourceTitle < Title
|
67
|
+
|
68
|
+
attr_reader :text, :resource
|
69
|
+
|
70
|
+
def initialize resource_or_string
|
71
|
+
@resource = resource_or_string unless resource_or_string.class == String
|
72
|
+
@text = resource_or_string.to_s
|
73
|
+
end
|
74
|
+
|
75
|
+
def path_object
|
76
|
+
@resource if navigable?
|
77
|
+
end
|
78
|
+
|
79
|
+
def resource?
|
80
|
+
@resource.present?
|
81
|
+
end
|
82
|
+
|
83
|
+
|
84
|
+
private
|
85
|
+
|
86
|
+
def navigable?
|
87
|
+
resource? && ( has_action?('show') || has_action?('index') )
|
88
|
+
end
|
89
|
+
|
90
|
+
def action_methods
|
91
|
+
@action_methods ||= Object.const_get('Admin').const_get("#{@resource.class.name.pluralize}Controller").action_methods
|
92
|
+
end
|
93
|
+
|
94
|
+
def has_action? action
|
95
|
+
action_methods.include?(action)
|
96
|
+
end
|
97
|
+
|
98
|
+
end
|
99
|
+
|
100
|
+
|
101
|
+
|
102
|
+
|
103
|
+
|
104
|
+
class CollectionTitle < Title
|
105
|
+
|
106
|
+
def initialize class_name, first
|
107
|
+
@class_name = class_name
|
108
|
+
@first = first
|
109
|
+
end
|
110
|
+
|
111
|
+
def path_object
|
112
|
+
table if @first
|
113
|
+
end
|
114
|
+
|
115
|
+
def text
|
116
|
+
table.titleize
|
117
|
+
end
|
118
|
+
|
119
|
+
private
|
120
|
+
|
121
|
+
def table
|
122
|
+
@class_name.tableize
|
123
|
+
end
|
124
|
+
|
125
|
+
end
|
126
|
+
|
127
|
+
|
128
|
+
|
129
|
+
|
130
|
+
|
131
|
+
class Titles
|
132
|
+
|
133
|
+
DEFAULT_TITLES_PER_PAGE = 1
|
134
|
+
|
135
|
+
class << self; attr_accessor :per_page, :prefix_exceptions, :site_title end
|
136
|
+
|
137
|
+
attr_reader :breadcrumbs, :headings, :tag, :up
|
138
|
+
|
139
|
+
|
140
|
+
def initialize controller
|
141
|
+
@controller = controller
|
142
|
+
@titles = []
|
143
|
+
@resources_titles = []
|
144
|
+
end
|
145
|
+
|
146
|
+
def generate view
|
147
|
+
@view = view
|
148
|
+
add_title_for_new_or_create
|
149
|
+
@breadcrumbs = compact_titles(@titles)
|
150
|
+
@headings = compact_titles(page_titles, false)
|
151
|
+
@tag = generate_tag
|
152
|
+
end
|
153
|
+
|
154
|
+
def add_collection class_name, first
|
155
|
+
@titles << CollectionTitle.new(class_name, first)
|
156
|
+
end
|
157
|
+
|
158
|
+
def add_resource resource_or_string
|
159
|
+
resource_title = ResourceTitle.new(resource_or_string)
|
160
|
+
@titles << resource_title
|
161
|
+
@resources_titles << resource_title if resource_title.resource?
|
162
|
+
end
|
163
|
+
|
164
|
+
def add *objects
|
165
|
+
objects.each { |object| @titles << ResourceTitle.new(object) }
|
166
|
+
end
|
167
|
+
|
168
|
+
def resources?
|
169
|
+
@resources_titles.any?
|
170
|
+
end
|
171
|
+
|
172
|
+
def resource? resource
|
173
|
+
@resources_titles.each { |title| return true if title.resource == resource }
|
174
|
+
false
|
175
|
+
end
|
176
|
+
|
177
|
+
|
178
|
+
private
|
179
|
+
|
180
|
+
def titles_per_page
|
181
|
+
self.class.per_page || DEFAULT_TITLES_PER_PAGE
|
182
|
+
end
|
183
|
+
|
184
|
+
def prefix_exceptions
|
185
|
+
self.class.prefix_exceptions || []
|
186
|
+
end
|
187
|
+
|
188
|
+
def add_title_for_new_or_create
|
189
|
+
if @titles.any? && %w( new create ).include?(@controller.action_name)
|
190
|
+
unless prefix_exceptions.include?(@titles.last.text)
|
191
|
+
add_resource('New ' + @titles.last.text.singularize)
|
192
|
+
end
|
193
|
+
end
|
194
|
+
end
|
195
|
+
|
196
|
+
def page_titles
|
197
|
+
@titles.slice(@titles.length - titles_per_page, titles_per_page) || []
|
198
|
+
end
|
199
|
+
|
200
|
+
def path title
|
201
|
+
if title.path?
|
202
|
+
title.path_object.class == String ? @view.send("admin_#{title.path_object}_path") : @view.controller.polymorphic_path([:admin, title.path_object])
|
203
|
+
end
|
204
|
+
end
|
205
|
+
|
206
|
+
def compact_titles titles, activate_links=true
|
207
|
+
titles.enum_with_index.map do |title, i|
|
208
|
+
if title.path? || i == titles.length-1 || (i == titles.length-2 && title.path? && !%w( Edit New ).include?(title.text) && (titles[i+1].nil? || %w( Edit New ).include?(titles[i+1].text)))
|
209
|
+
if activate_links && i < titles.length-1
|
210
|
+
@up = path(title) if title.path?
|
211
|
+
title_to_link(title)
|
212
|
+
else
|
213
|
+
activate_links ? title.text.abbreviate(30) : title.text
|
214
|
+
end
|
215
|
+
end
|
216
|
+
end.compact
|
217
|
+
end
|
218
|
+
|
219
|
+
def title_to_link title
|
220
|
+
title.path? ? @view.link_to(title.text.abbreviate(30), path(title)) : title.text.abbreviate(30)
|
221
|
+
end
|
222
|
+
|
223
|
+
def generate_tag
|
224
|
+
texts = compact_titles(@titles, false)
|
225
|
+
texts.unshift(self.class.site_title) if self.class.site_title
|
226
|
+
texts.join(' | ')
|
227
|
+
end
|
228
|
+
end
|
229
|
+
|
230
|
+
end
|
data/lib/entitled.rb
CHANGED
@@ -1,230 +1,3 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
def self.included(controller)
|
5
|
-
controller.extend ClassMethods
|
6
|
-
end
|
7
|
-
|
8
|
-
|
9
|
-
module ClassMethods
|
10
|
-
|
11
|
-
def entitled options={}
|
12
|
-
|
13
|
-
helper_method :entitled_generate,
|
14
|
-
:entitled_breadcrumbs,
|
15
|
-
:entitled_headings,
|
16
|
-
:entitled_tag,
|
17
|
-
:entitled_up,
|
18
|
-
:entitled_resources?,
|
19
|
-
:entitled_resource?
|
20
|
-
|
21
|
-
Titles.per_page = options[:per_page] if options[:per_page]
|
22
|
-
Titles.prefix_exceptions = options[:prefix_exceptions] if options[:prefix_exceptions]
|
23
|
-
Titles.site_title = options[:site_title] if options[:site_title]
|
1
|
+
require 'entitled/entitled'
|
24
2
|
|
25
|
-
|
26
|
-
end
|
27
|
-
end
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
module InstanceMethods
|
32
|
-
|
33
|
-
def initialize
|
34
|
-
super
|
35
|
-
@entitled = Titles.new self
|
36
|
-
end
|
37
|
-
|
38
|
-
private
|
39
|
-
|
40
|
-
def method_missing(method, *args, &block)
|
41
|
-
if match_data = method.to_s.match(/^entitled_(.+)$/)
|
42
|
-
@entitled.send(match_data[1], *args, &block)
|
43
|
-
end
|
44
|
-
end
|
45
|
-
|
46
|
-
end
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
class Title
|
51
|
-
|
52
|
-
def resource?
|
53
|
-
false
|
54
|
-
end
|
55
|
-
|
56
|
-
def path?
|
57
|
-
path_object.present?
|
58
|
-
end
|
59
|
-
|
60
|
-
end
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
class ResourceTitle < Title
|
67
|
-
|
68
|
-
attr_reader :text, :resource
|
69
|
-
|
70
|
-
def initialize resource_or_string
|
71
|
-
@resource = resource_or_string unless resource_or_string.class == String
|
72
|
-
@text = resource_or_string.to_s
|
73
|
-
end
|
74
|
-
|
75
|
-
def path_object
|
76
|
-
@resource if navigable?
|
77
|
-
end
|
78
|
-
|
79
|
-
def resource?
|
80
|
-
@resource.present?
|
81
|
-
end
|
82
|
-
|
83
|
-
|
84
|
-
private
|
85
|
-
|
86
|
-
def navigable?
|
87
|
-
resource? && ( has_action?('show') || has_action?('index') )
|
88
|
-
end
|
89
|
-
|
90
|
-
def action_methods
|
91
|
-
@action_methods ||= Object.const_get('Admin').const_get("#{@resource.class.name.pluralize}Controller").action_methods
|
92
|
-
end
|
93
|
-
|
94
|
-
def has_action? action
|
95
|
-
action_methods.include?(action)
|
96
|
-
end
|
97
|
-
|
98
|
-
end
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
class CollectionTitle < Title
|
105
|
-
|
106
|
-
def initialize class_name, first
|
107
|
-
@class_name = class_name
|
108
|
-
@first = first
|
109
|
-
end
|
110
|
-
|
111
|
-
def path_object
|
112
|
-
table if @first
|
113
|
-
end
|
114
|
-
|
115
|
-
def text
|
116
|
-
table.titleize
|
117
|
-
end
|
118
|
-
|
119
|
-
private
|
120
|
-
|
121
|
-
def table
|
122
|
-
@class_name.tableize
|
123
|
-
end
|
124
|
-
|
125
|
-
end
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
class Titles
|
132
|
-
|
133
|
-
DEFAULT_TITLES_PER_PAGE = 1
|
134
|
-
|
135
|
-
class << self; attr_accessor :per_page, :prefix_exceptions, :site_title end
|
136
|
-
|
137
|
-
attr_reader :breadcrumbs, :headings, :tag, :up
|
138
|
-
|
139
|
-
|
140
|
-
def initialize controller
|
141
|
-
@controller = controller
|
142
|
-
@titles = []
|
143
|
-
@resources_titles = []
|
144
|
-
end
|
145
|
-
|
146
|
-
def generate view
|
147
|
-
@view = view
|
148
|
-
add_title_for_new_or_create
|
149
|
-
@breadcrumbs = compact_titles(@titles)
|
150
|
-
@headings = compact_titles(page_titles, false)
|
151
|
-
@tag = generate_tag
|
152
|
-
end
|
153
|
-
|
154
|
-
def add_collection class_name, first
|
155
|
-
@titles << CollectionTitle.new(class_name, first)
|
156
|
-
end
|
157
|
-
|
158
|
-
def add_resource resource_or_string
|
159
|
-
resource_title = ResourceTitle.new(resource_or_string)
|
160
|
-
@titles << resource_title
|
161
|
-
@resources_titles << resource_title if resource_title.resource?
|
162
|
-
end
|
163
|
-
|
164
|
-
def add *objects
|
165
|
-
objects.each { |object| @titles << ResourceTitle.new(object) }
|
166
|
-
end
|
167
|
-
|
168
|
-
def resources?
|
169
|
-
@resources_titles.any?
|
170
|
-
end
|
171
|
-
|
172
|
-
def resource? resource
|
173
|
-
@resources_titles.each { |title| return true if title.resource == resource }
|
174
|
-
false
|
175
|
-
end
|
176
|
-
|
177
|
-
|
178
|
-
private
|
179
|
-
|
180
|
-
def titles_per_page
|
181
|
-
self.class.per_page || DEFAULT_TITLES_PER_PAGE
|
182
|
-
end
|
183
|
-
|
184
|
-
def prefix_exceptions
|
185
|
-
self.class.prefix_exceptions || []
|
186
|
-
end
|
187
|
-
|
188
|
-
def add_title_for_new_or_create
|
189
|
-
if @titles.any? && %w( new create ).include?(@controller.action_name)
|
190
|
-
unless prefix_exceptions.include?(@titles.last.text)
|
191
|
-
add_resource('New ' + @titles.last.text.singularize)
|
192
|
-
end
|
193
|
-
end
|
194
|
-
end
|
195
|
-
|
196
|
-
def page_titles
|
197
|
-
@titles.slice(@titles.length - titles_per_page, titles_per_page) || []
|
198
|
-
end
|
199
|
-
|
200
|
-
def path title
|
201
|
-
if title.path?
|
202
|
-
title.path_object.class == String ? @view.send("admin_#{title.path_object}_path") : @view.controller.polymorphic_path([:admin, title.path_object])
|
203
|
-
end
|
204
|
-
end
|
205
|
-
|
206
|
-
def compact_titles titles, activate_links=true
|
207
|
-
titles.enum_with_index.map do |title, i|
|
208
|
-
if title.path? || i == titles.length-1 || (i == titles.length-2 && title.path? && !%w( Edit New ).include?(title.text) && (titles[i+1].nil? || %w( Edit New ).include?(titles[i+1].text)))
|
209
|
-
if activate_links && i < titles.length-1
|
210
|
-
@up = path(title) if title.path?
|
211
|
-
title_to_link(title)
|
212
|
-
else
|
213
|
-
activate_links ? title.text.abbreviate(30) : title.text
|
214
|
-
end
|
215
|
-
end
|
216
|
-
end.compact
|
217
|
-
end
|
218
|
-
|
219
|
-
def title_to_link title
|
220
|
-
title.path? ? @view.link_to(title.text.abbreviate(30), path(title)) : title.text.abbreviate(30)
|
221
|
-
end
|
222
|
-
|
223
|
-
def generate_tag
|
224
|
-
texts = compact_titles(@titles, false)
|
225
|
-
texts.unshift(self.class.site_title) if self.class.site_title
|
226
|
-
texts.join(' | ')
|
227
|
-
end
|
228
|
-
end
|
229
|
-
|
230
|
-
end
|
3
|
+
ActionController::Base.send :include, Entitled
|
metadata
CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 0
|
7
7
|
- 1
|
8
|
-
-
|
9
|
-
version: 0.1.
|
8
|
+
- 1
|
9
|
+
version: 0.1.1
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Marc Tauber
|
@@ -112,8 +112,8 @@ files:
|
|
112
112
|
- Rakefile
|
113
113
|
- VERSION
|
114
114
|
- entitled.gemspec
|
115
|
-
- init.rb
|
116
115
|
- lib/entitled.rb
|
116
|
+
- lib/entitled/entitled.rb
|
117
117
|
- spec/entitled_spec.rb
|
118
118
|
- spec/spec_helper.rb
|
119
119
|
has_rdoc: true
|
@@ -130,7 +130,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
130
130
|
requirements:
|
131
131
|
- - ">="
|
132
132
|
- !ruby/object:Gem::Version
|
133
|
-
hash:
|
133
|
+
hash: 9515795
|
134
134
|
segments:
|
135
135
|
- 0
|
136
136
|
version: "0"
|
data/init.rb
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
ActionController::Base.send :include, Entitled
|