-
Notifications
You must be signed in to change notification settings - Fork 115
/
fake_app.rb
357 lines (314 loc) · 7.73 KB
/
fake_app.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
# frozen_string_literal: true
ENV['RAILS_ENV'] ||= 'test'
require 'active_record'
require 'action_controller/railtie'
require 'action_mailer/railtie'
require 'action_view/railtie'
require 'ostruct'
require 'jbuilder' unless ENV['API'] == '1'
# config
ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: ':memory:')
module ActiveDecoratorTestApp
class Application < Rails::Application
config.secret_token = '"confusion" will be my epitaph.'
config.session_store :cookie_store, key: '_myapp_session'
config.active_support.deprecation = :log
config.eager_load = false
if ((Rails::VERSION::MAJOR >= 7) && (Rails::VERSION::MINOR >= 1)) || (Rails::VERSION::MAJOR >= 8)
config.action_dispatch.show_exceptions = :none
else
config.action_dispatch.show_exceptions = false
end
config.root = File.dirname(__FILE__)
config.logger = ActiveSupport::Logger.new($stdout)
config.action_mailer.delivery_method = :test
config.after_initialize do |app|
app.routes_reloader.reload!
end
end
end
ActiveDecoratorTestApp::Application.initialize!
# routes
ActiveDecoratorTestApp::Application.routes.draw do
resources :authors, only: [:index, :show] do
collection do
get :partial
end
resources :books, only: [:index, :show] do
member do
get :errata
get :errata2
get :error
post :purchase
end
end
resources :novels, only: [:index, :show] do
member do
get :error
post :purchase
end
end
end
resources :movies, only: :show
if Rails::VERSION::MAJOR >= 5
namespace :api do
resources :bookstores, only: :show do
collection do
get :error
end
end
end
end
end
# models
class Author < ActiveRecord::Base
has_many :books
has_many :publishers, through: :books
has_many :movies
has_one :profile
has_one :profile_history, through: :profile
has_and_belongs_to_many :magazines
belongs_to :company
end
class Book < ActiveRecord::Base
belongs_to :author
belongs_to :publisher
accepts_nested_attributes_for :publisher
end
class Novel < Book
end
class Movie < ActiveRecord::Base
belongs_to :author
end
class Publisher < ActiveRecord::Base
has_many :books
end
class Profile < ActiveRecord::Base
belongs_to :author
has_one :profile_history
accepts_nested_attributes_for :profile_history
end
class ProfileHistory < ActiveRecord::Base
belongs_to :profile
end
class Magazine < ActiveRecord::Base
has_and_belongs_to_many :authors
end
class Company < ActiveRecord::Base
has_many :authors
end
class Bookstore < ActiveRecord::Base
end
class NilRecord < ActiveRecord::Base
def nil?
true
end
end
# helpers
module ApplicationHelper; end
# decorators
module AuthorDecorator
def reverse_name
name.reverse
end
def capitalized_name
name.capitalize
end
end
module BookDecorator
def reverse_title
title.reverse
end
def upcased_title
title.upcase
end
def link
link_to title, "#{request.protocol}#{request.host_with_port}/assets/sample.png", class: 'title'
end
def cover_image
image_tag 'cover.png'
end
def errata
poof!
end
def errata2
title.boom!
end
def error
"ERROR"
end
end
module PublisherDecorator
def upcased_name
name.upcase
end
def reversed_name
name.reverse
end
end
module ProfileDecorator
def address
"secret"
end
end
module ProfileHistoryDecorator
def update_date
updated_on.strftime('%Y/%m/%d')
end
end
module MagazineDecorator
def upcased_title
title.upcase
end
end
module CompanyDecorator
def reverse_name
name.reverse
end
end
module BookstoreDecorator
def initial
name.first
end
def to_json
{name: name, initial: initial}
end
end
module NilRecordDecorator; end
# decorator fake
class MovieDecorator; end
# Maybe we can decorate `nil` now?
module NilClassDecorator
def do
inspect
end
end
module TrueClassDecorator
def do
inspect
end
end
module FalseClassDecorator
def do
inspect
end
end
# controllers
unless ENV['API'] == '1'
class ApplicationController < ActionController::Base
self.append_view_path File.dirname(__FILE__)
end
class AuthorsController < ApplicationController
def index
# ActiveRecord 4.x
if params[:variable_type] == 'array'
@authors = Author.all.to_a
elsif params[:variable_type] == 'proxy'
@authors = RelationProxy.new(Author.all)
else
@authors = Author.all
end
end
def show
@author = Author.find params[:id]
end
def partial
if params[:pattern] == 'collection'
render partial: 'authors/author', collection: @authors = Author.all
elsif params[:pattern] == 'locals'
render partial: 'authors/author_locals', locals: {a: Author.first}
else
render Author.first
end
end
end
class BooksController < ApplicationController
class CustomError < StandardError; end
rescue_from CustomError do
render :error
end
def index
@author = Author.find params[:author_id]
@books = @author.books
end
def show
@book = Author.find(params[:author_id]).books.find(params[:id])
end
def errata
@book = Author.find(params[:author_id]).books.find(params[:id])
end
def errata2
@book = Author.find(params[:author_id]).books.find(params[:id])
end
def error
@book = Author.find(params[:author_id]).books.find(params[:id])
raise CustomError, "error"
end
def purchase
@book = Author.find(params[:author_id]).books.find(params[:id])
@view_context_before_sending_mail = ActiveDecorator::ViewContext.current
BookMailer.thanks(@book).deliver
raise 'Wrong ViewContext!' if ActiveDecorator::ViewContext.current != @view_context_before_sending_mail
end
end
class MoviesController < ApplicationController
def show
@movie = Movie.find params[:id]
end
end
else
module Api
class BookstoresController < ActionController::API
class CustomError < StandardError; end
rescue_from CustomError do
head :internal_server_error
end
def show
@bookstore = Bookstore.find params[:id]
render json: serialize('bookstore')
end
private def serialize(name)
view_assigns[name].to_json
end
def error
raise CustomError, 'boom!'
end
end
end
end
# mailers
class BookMailer < ActionMailer::Base
def thanks(book)
@book = book
mail from: '[email protected]', to: '[email protected]', subject: 'Thanks'
end
end
# migrations
class CreateAllTables < ActiveRecord::VERSION::MAJOR >= 5 ? ActiveRecord::Migration[5.0] : ActiveRecord::Migration
def self.up
create_table(:authors) {|t| t.string :name; t.references :company}
create_table(:books) {|t| t.string :title; t.string :type; t.references :author; t.references :publisher }
create_table(:profiles) {|t| t.string :address; t.references :author}
create_table(:profile_histories) {|t| t.date :updated_on; t.references :profile}
create_table(:publishers) {|t| t.string :name}
create_table(:movies) {|t| t.string :name; t.references :author}
create_table(:magazines) {|t| t.string :title}
create_table(:authors_magazines) {|t| t.references :author; t.references :magazine}
create_table(:companies) {|t| t.string :name}
create_table(:bookstores) {|t| t.string :name}
create_table(:nil_records) {|t| t.string :name}
end
end
# Proxy for ActiveRecord::Relation
class RelationProxy < BasicObject
attr_accessor :ar_relation
def initialize(ar_relation)
@ar_relation = ar_relation
end
def method_missing(method, *args, &block)
@ar_relation.public_send(method, *args, &block)
end
end
module Foo
class Comic; end
end