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
|
# File 'lib/generators/happy_seed/api/api_generator.rb', line 12
def install_device_invitable
return if already_installed
require_generator DeviseGenerator
gem 'apitome'
gem 'rspec_api_documentation', :groups => [:development, :test]
Bundler.with_clean_env do
run "bundle install --without production"
end
generate "model user_token user:belongs_to:index token installation_identifier:index push_token locked:boolean form_factor os"
generate "migration add_user_tokens_count_to_users user_tokens_count:integer"
directory '.'
route " scope module: :api, defaults: {format: :json} do
%w(v1).each do |version|
namespace version.to_sym do
resource :configuration, only: %w(show)
resource :user_token, path: :token, only: %w(create destroy update)
resources :users, only: %w(create update show) do
resources :questions, only: %w(index)
collection do
post :forgot_password
put :reset_password
end
end
end
end
end
"
inject_into_class "app/models/user.rb", "User", " has_many :user_tokens, dependent: :destroy\n"
gsub_file "app/models/user_token.rb", /belongs_to :user\n/, " validates :user, presence: true
validates :token, presence: true, uniqueness: {case_sensitive: false}
validates :installation_identifier, presence: true, uniqueness: {case_sensitive: false, scope: %w(user_id)}
validates :push_token, allow_blank: true, uniqueness: {case_sensitive: false}
validates :form_factor, allow_blank: true, inclusion: {in: %w(smartphone tablet10 tablet7 desktop)}
validates :os, allow_blank: true, inclusion: {in: %w(ios android bb wp7)}
scope :with_push_token, -> { where.not push_token: nil }
belongs_to :user, counter_cache: true
before_validation :set_token
private
def set_token
self.token ||= loop do
token = Devise.friendly_token.downcase
break token unless self.class.where(token: token).first.present?
end
end
"
prepend_to_file 'spec/spec_helper.rb', "require 'rspec_api_documentation'\n"
append_to_file 'spec/spec_helper.rb', "\nRspecApiDocumentation.configure do |config|
config.format = :json
config.docs_dir = Pathname( 'docs/api' )
config.request_headers_to_include = %w(Authorization)
config.response_headers_to_include = %w()
end"
append_to_file 'spec/factories/users.rb', "\nFactoryGirl.define do
factory :user_with_token, parent: :user do
after :build do |user, evaluator|
user.user_tokens.build installation_identifier: Faker::Lorem.characters(10), push_token: Faker::Lorem.characters(10),
form_factor: %w(smartphone tablet10 tablet7 desktop).sample, os: %w(ios android bb wp7).sample
end
end
end"
end
|