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
|
# File 'lib/mobile_on_rails.rb', line 4
def self.make_files
directory = Dir.getwd
if is_rails_root?(directory)
Dir.mkdir(directory + "/db/migrate") unless Dir.exist?(directory + "/db/migrate")
time = Time.new
month = "%02d" % time.month
day = "%02d" % time.day
hour = "%02d" % time.hour
minute = "%02d" % time.min
second = "%02d" % time.sec
File.open("#{directory}/db/migrate/#{time.year}#{month}#{day}#{hour}#{minute}#{second}_mobile_on_rails_migration.rb", "w") {|f| f.write(
%Q{class MobileOnRailsMigration < ActiveRecord::Migration
def change
create_table :mor_users do |t|
t.string :user_name
t.string :email
t.string :password_digest
t.timestamps
end
create_table :mor_posts do |t|
t.string :title
t.text :text
t.belongs_to :mor_user
t.timestamps
end
create_table :mor_tags do |t|
t.string :name
t.timestamps
end
create_table :mor_posts_tags do |t|
t.belongs_to :post
t.belongs_to :tag
end
end
end})}
File.open("#{directory}/app/models/mor_post.rb", "w") {|f| f.write(
%Q{class MorPost < ActiveRecord::Base
belongs_to :user
has_and_belongs_to_many :mor_tags
end})}
File.open("#{directory}/app/models/mor_tag.rb", "w") {|f| f.write(
%Q{class MorTag < ActiveRecord::Base
has_and_belongs_to_many :mor_posts
end})}
File.open("#{directory}/app/models/mor_user.rb", "w") {|f| f.write(
%Q{class MorUser < ActiveRecord::Base
has_secure_password
validates_presence_of :password, :on => :create
has_many :mor_posts
end})}
File.open("#{directory}/app/controllers/mor_posts_controller.rb", "w") {|f| f.write(
'class MorPostsController < ApplicationController
protect_from_forgery except: :create
skip_before_filter :verify_authenticity_token
respond_to :json
def index
puts params
@all_posts = MorPost.all
puts @all_posts.to_json
render json: @all_posts
end
def all
@all_posts = MorPost.all
render json: @all_posts
end
def create
@json_data = JSON.parse(JSON.parse(params.keys.to_s).first)
@user = MorUser.find_by_user_name(@json_data.values[1])
@post = MorPost.new
@post.mor_user_id = @user.id
@post.title = @json_data.values[0]
@post.text = @json_data.values[2]
if @post.save
respond_to do |format|
msg = { :status => "ok", :message => "Success!", :html => "<b>...</b>" }
format.json { render :json => msg }
end
else
respond_to do |format|
msg = { :status => "Error", :message => "Failed", :html => "<b>...</b>" }
format.json { render :json => msg }
end
end
end
end')}
File.open("#{directory}/app/controllers/mor_sessions_controller.rb", "w") {|f| f.write(
'class MorSessionsController < ApplicationController
#protect_from_forgery except: :create
skip_before_filter :verify_authenticity_token
respond_to :json
def new
end
def create
@json_data = decode_json(params)
user = MorUser.find_by_user_name(@json_data["username"])
if user && user.authenticate(@json_data["password"])
session[:user_id] = user.id
respond_to do |format|
msg = { :status => "ok", :message => "Logged in!", :user => "#{user.email}" }
format.json { render :json => msg }
end
else
flash.now.alert = "Invalid email or password!"
respond_to do |format|
msg = { :status => "error", :message => "Not Authenticated" }
format.json { render :json => msg }
end
end
end
def destroy
session[:user_id] = nil
redirect_to root_url, :notice => "Logged out!"
end
def decode_json(json)
@json_data = JSON.parse(JSON.parse(json.keys.to_s).first)
key = @json_data.keys[0]
data = @json_data.values[0]
puts data["email"]
return data
end
end')}
File.open("#{directory}/app/controllers/mor_users_controller.rb", "w") {|f| f.write(
'class MorUsersController < ApplicationController
def new
@user = MorUser.new
end
def create
@user = MorUser.new(user_params)
if @user.save
redirect_to root_url, :notice => "Signed up!"
else
render new
end
end
private
def user_params
params.require(:user).permit(:email, :password, :password_confirmation)
end
end')}
File.open("#{directory}/config/routes.rb", "r+") do |f|
firstline = f.readline
f.write(
"
resources :mor_sessions, only: [:new, :create, :destroy]
resources :mor_posts
match '/mor_posts/all', to: 'mor_posts#all', via: 'post'
")
end
puts "Files created! Remember to run rake:db migrate to create your"
puts "database tables, and add BCrypt to your Gemfile (it should be"
puts "there already, just commented out) and run 'bundle install'."
end
end
|