Skip to content

Instantly share code, notes, and snippets.

@kul1
Last active January 24, 2018 12:50
Show Gist options
  • Save kul1/c5bb17955ec133243597a40ce4e92198 to your computer and use it in GitHub Desktop.
Save kul1/c5bb17955ec133243597a40ce4e92198 to your computer and use it in GitHub Desktop.
Hash method to controller from https://github.com/kul1/t8-241514-rspec
# encoding: utf-8
class Article
include Mongoid::Document
include Mongoid::Attributes::Dynamic
# jinda begin
include Mongoid::Timestamps
field :title, :type => String
field :text, :type => String
belongs_to :user
has_many :comments
#validates :title, :text, :user_id, presence: true
field :body, :type => String
field :keywords, :type => String
# jinda end
end
require 'rails_helper'
# This spec was generated by rspec-rails when you ran the scaffold generator.
# It demonstrates how one might use RSpec to specify the controller code that
# was generated by Rails when you ran the scaffold generator.
#
# It assumes that the implementation code is generated by the rails scaffold
# generator. If you are using any extension libraries to generate different
# controller code, this generated spec may or may not pass.
#
# It only uses APIs available in rails and/or rspec-rails. There are a number
# of tools you can use to make these specs even more expressive, but we're
# sticking to rails and rspec-rails APIs to keep things simple and stable.
#
# Compared to earlier versions of this generator, there is very limited use of
# stubs and message expectations in this spec. Stubs are only used when there
# is no simpler way to get a handle on the object needed for the example.
# Message expectations are only used when there is no simpler way to specify
# that an instance is receiving a specific message.
#
# Also compared to earlier versions of this generator, there are no longer any
# expectations of assigns and templates rendered. These features have been
# removed from Rails core in Rails 5, but can be added back in via the
# `rails-controller-testing` gem.
RSpec.describe ArticlesController, type: :controller do
# This should return the minimal set of attributes required to create a valid
# Article. As you add validations to Article, be sure to
# adjust the attributes here as well.
let(:valid_input) {
{:title => "My Title", :text => "My Item", :user_id => "admin"}
}
let(:invalid_input) {
{:title => "", :text => "", :user_id => ""}
}
class Hash
def role
"a,m"
end
end
user = {}
user["uid"] = "admin"
user["provider"] = "Facebook"
user["email"] = "1.0@kul.asia"
user["role"] = "a,m"
current_ma_user = user
$xvars = {}
$xvars["form_article"] = {}
$xvars["form_article"]["title"] = "AAAAAA"
$xvars["form_article"]["text"] = "AAAAAA"
$xvars["form_article"]["body"] = "AAAAA"
$xvars["user_id"] = user
$xvars["select_article"] = {}
$xvars["select_article"] = nil
$xvars["p"] = {}
$nxvars = {}
$xvars["edit_article"] = {}
$xvars["edit_article"]["article"] = {}
$xvars["edit_article"]["article"]["title"] = "BBBBB"
$xvars["edit_article"]["article"]["text"] = "BBBBB"
$xvars["edit_article"]["article"]["keywords"] = "BBBBB"
$xvars["edit_article"]["article"]["body"] = "BBBBB"
let(:valid_attributes) {
$xvars
}
let(:invalid_attributes) {
{}
}
# This should return the minimal set of values that should be in the session
# in order to pass any filters (e.g. authentication) defined in
# ArticlesController. Be sure to keep this updated too.
let(:valid_session) { {} }
describe "GET #index" do
it "returns a success response" do
article = Article.create! valid_input
get :index, params: {}, session: valid_session
expect(response).to be_success
end
end
describe "GET #show" do
it "returns a success response" do
article = Article.create! valid_input
get :show, params: {id: article.to_param}, session: valid_session
expect(response).to be_success
end
end
describe "GET #edit" do
it "returns a success response" do
article = Article.create! valid_input
get :edit, params: {id: article.to_param}, session: valid_session
expect(response).to be_success
end
end
describe "POST #create" do
context "with valid params" do
it "creates a new Article" do
expect {
post :create, params: {article: valid_attributes}, session: valid_session
}.to change(Article, :count).by(1)
end
it "redirects to the created article" do
post :create, params: {article: valid_attributes}, session: valid_session
expect(response).to redirect_to(Article.last)
end
end
context "with invalid params" do
it "returns a success response (i.e. to display the 'new' template)" do
post :create, params: {article: invalid_attributes}, session: valid_session
expect(response).not_to be_success
end
end
end
describe "PUT #update" do
context "with valid params" do
let(:new_attributes) {
$xvars
}
it "updates the requested article" do
article = Article.create! valid_attributes
$xvars["p"]["article_id"] = article.to_param
put :update, params: {id: article.to_param, article: new_attributes}, session: valid_session
article.reload
expect(article.title).to eq "BBBBB"
end
it "redirects to the article" do
article = Article.create! valid_attributes
$xvars["p"]["article_id"] = article.to_param
put :update, params: {id: article.to_param, article: new_attributes}, session: valid_session
expect(response).to redirect_to(article)
end
end
context "with invalid params" do
it "returns a success response (i.e. to display the 'edit' template)" do
article = Article.create! valid_attributes
$xvars["p"]["article_id"] = article.to_param
put :update, params: {id: article.to_param, article: invalid_attributes}, session: valid_session
expect(response).not_to be_success
end
end
end
describe "DELETE #destroy" do
it "destroys the requested article" do
article = Article.create! valid_attributes
expect {
delete :destroy, params: {id: article.to_param}, session: valid_session
}.to change(Article, :count).by(-1)
end
it "redirects to the articles list" do
article = Article.create! valid_attributes
delete :destroy, params: {id: article.to_param}, session: valid_session
expect(response).to redirect_to(articles_url)
end
end
end
class ArticlesController < ApplicationController
before_action :load_article, only: [:show, :destroy]
before_action :load_comments, only: :show
def index
@articles = Article.desc(:created_at).page(params[:page]).per(10)
end
def show
prepare_meta_tags(title: @article.title,
description: @article.text,
keywords: @article.keywords)
end
def edit
@article = Article.find(params[:id])
@page_title = 'Member Login'
end
def create
@article = Article.new(
title: $xvars["form_article"]["title"],
text: $xvars["form_article"]["text"],
keywords: $xvars["form_article"]["keywords"],
body: $xvars["form_article"]["body"],
user_id: $xvars["user_id"])
@article.save!
# if @article.save!
# format.html { redirect_to @article, notice: 'Sample was successfully created.' }
# format.json { render :show, status: :created, location: @article }
# else
# format.html { render :new }
# format.json { render json: @article.errors, status: :unprocessable_entity }
# end
redirect_to @article
end
def my
@articles = Article.where(user_id: current_ma_user).desc(:created_at).page(params[:page]).per(10)
@page_title = 'Member Login'
end
def update
# $xvars["select_article"] and $xvars["edit_article"]
# These are variables.
# They contain everything that we get their forms select_article and edit_article
article_id = $xvars["select_article"] ? $xvars["select_article"]["title"] : $xvars["p"]["article_id"]
@article = Article.find(article_id)
@article.update(title: $xvars["edit_article"]["article"]["title"],
text: $xvars["edit_article"]["article"]["text"],
keywords: $xvars["edit_article"]["article"]["keywords"],
body: $xvars["edit_article"]["article"]["body"])
redirect_to @article
end
def destroy
if current_ma_user.role.upcase.split(',').include?("A") || current_ma_user == @article.user
#if current_ma_user == @article.user #Green
@article.destroy
end
redirect_to :action=>'index'
end
private
def load_article
@article = Article.find(params[:id])
end
def load_comments
@comments = @article.comments.find_all
end
end
# -*- encoding : utf-8 -*-
class UsersController < ApplicationController
def index
@today = Date.today
@xmains = current_ma_user.xmains.in(status:['R','I']).asc(:created_at)
end
# jinda methods
def update_user
# can't use session, current_ma_user inside jinda methods
$user.update_attribute :email, $xvars["enter_user"]["user"]["email"]
end
def change_password
# check if old password correct
identity = Identity.find_by :code=> $user.code
if identity.authenticate($xvars["enter"]["epass"])
identity.password = $xvars["enter"]["npass"]
identity.password_confirmation = $xvars["enter"]["npass_confirm"]
identity.save
ma_log "Password changed"
else
ma_log "Unauthorized access"
end
end
def send_password_reset
generate_token(:password_reset_token)
self.password_reset_sent_at = Time.zone.now
save!
UserMailer.password_reset(self).deliver
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment