module MobileOnRails

Constants

VERSION

Public Class Methods

is_rails_root?(dir) click to toggle source
# File lib/mobile_on_rails.rb, line 184
def self.is_rails_root?(dir)
  if not Dir.exist?(dir + "/app/models")
    puts "It looks like the directory 'app/models' doesn't exist here! Is this a valid Rails root directory?"
    return false
  elsif not Dir.exists?(dir + "/app/controllers")
    puts "It looks like the directory 'app/controllers' doesn't exist here! Is this a valid Rails root directory?"
    return false
  end
  
  true
end
make_files() click to toggle source
# File lib/mobile_on_rails.rb, line 4
  def self.make_files
    directory = Dir.getwd
    if is_rails_root?(directory)
      # If the migration folder has not yet been created, create it
      Dir.mkdir(directory + "/db/migrate") unless Dir.exist?(directory + "/db/migrate")
      
      # Write the migration file
      # NOTE: The user will still need to run rake: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})}
      
      # Create the post, tag, and user model files
      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})}
      
      # Create the controller files
      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