Skip to content

Instantly share code, notes, and snippets.

@RallyWithGalli
Last active August 29, 2015 14:22
Show Gist options
  • Save RallyWithGalli/d09f61e09dece645a60b to your computer and use it in GitHub Desktop.
Save RallyWithGalli/d09f61e09dece645a60b to your computer and use it in GitHub Desktop.
How to link_to correct show page from feed?
activities/valuations/_create.html.erb
added <span class="label label-primary">value</span> <br>
&nbsp;&nbsp;&nbsp;<small class="text-muted"><%= activity.created_at.strftime('%B %d at %l:%M%P') %></small><br><br>
<div class="activity">
<% if activity.trackable %>
<%= link_to activity.trackable.name, valuation_path(@valuation) %>
<% else %>
which has since been removed
<% end %>
<br>
</div>
valuations/_form.html.erb
<%= simple_form_for(@valuation) do |f| %>
<%= f.error_notification %>
<div class="add-form-padding">
<form>
<div class="form-group">
<%= f.text_area :name, rows: 4, class: 'form-control', id: "gold-standard", placeholder: 'Enter Value' %>
</div>
<div class="form-group">
<%= f.text_field :tag_list, valuation: @valuation.tag_list.to_s.titleize, class: 'form-control', placeholder: 'Enter Tag(s)' %>
</div>
<div class="center-buttons">
<div class="centered">
<%= button_tag(type: 'submit', class: "btn", id: "gold") do %>
<span class="glyphicon glyphicon-plus"></span> Public
<% end %>
</div>
<div class="centered">
<%= button_tag(type: 'submit', class: "btn") do %>
<% :conceal %><span class="glyphicon glyphicon-plus"></span> Private
<% end %>
</div>
<%= f.submit :conceal, class: "btn" do %>
'<span class="glyphicon glyphicon-thumbs-up"></span> Private'.html_safe
<% end %>
<div class="centered">
<%= link_to @valuation, method: :delete, data: { confirm: 'Are you sure?' }, class: "btn", id: "red" do %>
<span class="glyphicon glyphicon-trash"></span> Delete
<% end %>
</div>
</div>
</form>
</div>
<% end %>
activities/index.html.erb
<center>
<div class="pagination">
<%= will_paginate @activities %>
</div>
</center>
<% @activities.each do |activity| %>
<div class="activities">
<div class="float-feed">
<% if activity.user.uid %>
<%= image_tag activity.user.image %>
<% else %>
<%= gravatar_for activity.user, size: 50 %>
<% end %>
</div>
&nbsp;&nbsp;&nbsp;<b><%= link_to activity.user.name, activity.user %></b>
<%= render "activities/#{activity.trackable_type.underscore}/#{activity.action}", activity: activity %>
</div>
<% end %>
<center>
<div class="pagination">
<%= will_paginate @activities %>
</div>
</center>
<% content_for :jumbotron do %>
<div class="jumbtron">
<div class="container">
<h1><b>Feed</b></h1>
<p>Like, comment, share!<br>
A little inspiration goes a long way.<br>
"Good people bring out the good in people"</p>
</div>
</div>
<% end %>
class ActivitiesController < ApplicationController
def index
@activities = Activity.order("created_at desc").paginate(:page => params[:page])
@valuation = Valuation.find(params[:id])
end
def show
redirect_to(:back)
end
def like
@activity = Activity.find(params[:id])
@activity_like = current_user.activity_likes.build(activity: @activity)
if @activity_like.save
@activity.increment!(:likes)
flash[:success] = 'Thanks for liking!'
else
flash[:error] = 'Two many likes'
end
redirect_to(:back)
end
end
class Activity < ActiveRecord::Base
self.per_page = 20
has_many :notifications
has_many :comment_likes
has_many :likers, through: :comment_likes, class_name: 'User', source: :liker
belongs_to :user
has_many :comments, as: :commentable
belongs_to :trackable, polymorphic: true
def conceal
trackable.conceal
end
def page_number
(index / per_page.to_f).ceil
end
private
def index
Activity.order(created_at: :desc).index self
end
end
Rails.application.routes.draw do
get 'notes/index'
get 'notes/new'
get 'notifications/index'
get 'auth/:provider/callback', to: 'sessions#facebook'
get 'auth/failure', to: redirect('/')
get 'signout', to: 'sessions#destroy', as: 'signout'
get 'password_resets/new'
get 'password_resets/edit'
resources :users do
resources :comments
end
resources :notes
resources :habits do
resources :notes
resources :notifications
resources :comments do
resources :likes
end
resources :likes
member do
post :like
post :notifications
end
resources :levels do
# we'll use this route to increment and decrement the missed days
resources :days_missed, only: [:create, :destroy]
end
end
resources :goals do
resources :notes
resources :comments
member do
post :like
end
end
resources :valuations do
resources :notes
resources :comments
resources :notifications
member do
post :like
post :notifications
end
end
resources :quantifieds do
resources :notes
resources :comments
member do
post :like
end
end
resources :results
resources :users
resources :account_activations, only: [:edit]
resources :activities do
resources :valuations
resources :comments
resources :notifications
member do
post :like
post :notifications
end
end
resources :comments do
resources :comments
member do
post :like
end
end
resources :password_resets, only: [:new, :create, :edit, :update]
resources :relationships, only: [:create, :destroy]
get 'tags/:tag', to: 'pages#home', as: :tag
resources :users do
member do
get :following, :followers
end
end
get 'about' => 'pages#about'
get 'signup' => 'users#new'
get 'login' => 'sessions#new'
post 'login' => 'sessions#create'
delete 'logout' => 'sessions#destroy'
root 'pages#home'
end
valuations/show.html.erb
<%= link_to valuations_path, class: "btn" do %>
<span class="glyphicon glyphicon-list"></span> Values
<% end %>
<%= link_to edit_valuation_path, class: "btn" do %>
<span class="glyphicon glyphicon-edit"></span> Edit
<% end %>
<div class="border-and-padding">
<div class="add-form-padding-three">
<table>
<tr class="valuation">
<td>
<%= link_to edit_valuation_path(@valuation) do %>
<%= @valuation.name %>
<% end %>&nbsp;
<b><%= raw @valuation.tag_list.map { |t| link_to t.titleize, tag_path(t), class: 'label label-primary' }.join(' ') %></b>
</td>
</tr>
</table>
</div>
<%= render "notes/notes" %>
<%= render "notes/form" %>
<br>
</div>
<% @valuation.likers.each do |user| %>
<span class="label label-primary">
<%= user.name %>
</span>&nbsp;
<% end %>
<br>
<div class="center-buttons-feed-2">
<%= link_to like_valuation_path(:id => @valuation.id), class: "btn", method: :post do %>
<span class='glyphicon glyphicon-thumbs-up'></span> Like
<% end %>
</div>
<div class="comment-background">
<%= render "comments/comments" %>
<br>
<%= render "comments/form" %>
</div>
<% content_for :jumbotron do %>
<div class="jumbtron">
<div class="container">
<h1><b>Value</b></h1>
<p>Inspiration and information you strive to emulate and ingrain,</br>
can be subjective. A dream catcher for everything </br>
that doesn't fit into habits, goals, and stats.</p>
</div>
</div>
<% end %>
class User < ActiveRecord::Base
acts_as_tagger
acts_as_taggable
has_many :notifications
has_many :activities
has_many :activity_likes
has_many :liked_activities, through: :activity_likes, class_name: 'Activity', source: :liked_activity
has_many :liked_comments, through: :comment_likes, class_name: 'Comment', source: :liked_comment
has_many :valuation_likes
has_many :habit_likes
has_many :goal_likes
has_many :quantified_likes
has_many :comment_likes
has_many :authentications
has_many :habits, dependent: :destroy
has_many :levels
has_many :valuations, dependent: :destroy
has_many :comments, as: :commentable
has_many :goals, dependent: :destroy
has_many :quantifieds, dependent: :destroy
has_many :results, through: :quantifieds
has_many :notes, through: :habits
accepts_nested_attributes_for :habits, :reject_if => :all_blank, :allow_destroy => true
accepts_nested_attributes_for :notes, :reject_if => :all_blank, :allow_destroy => true
accepts_nested_attributes_for :quantifieds, :reject_if => :all_blank, :allow_destroy => true
accepts_nested_attributes_for :results, :reject_if => :all_blank, :allow_destroy => true
has_many :active_relationships, class_name: "Relationship",
foreign_key: "follower_id",
dependent: :destroy
has_many :passive_relationships, class_name: "Relationship",
foreign_key: "followed_id",
dependent: :destroy
has_many :following, through: :active_relationships, source: :followed
has_many :followers, through: :passive_relationships, source: :follower
attr_accessor :remember_token, :activation_token, :reset_token
before_save :downcase_email
before_create :create_activation_digest
validates :name, presence: true, length: { maximum: 50 }
VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
validates :email, presence: true, length: { maximum: 255 },
format: { with: VALID_EMAIL_REGEX },
uniqueness: { case_sensitive: false }, unless: -> { from_omniauth? }
has_secure_password
validates :password, length: { minimum: 6 }
scope :publish, ->{ where(:conceal => false) }
User.tag_counts_on(:tags)
def count_mastered
@res = habits.reduce(0) do |count, habit|
habit.current_level == 6 ? count + 1 : count
end
end
def count_challenged
@challenged_count = habits.count - @res
end
def self.from_omniauth(auth)
where(provider: auth.provider, uid: auth.uid).first_or_initialize.tap do |user|
user.provider = auth.provider
user.image = auth.info.image
user.uid = auth.uid
user.name = auth.info.name
user.oauth_token = auth.credentials.token
user.oauth_expires_at = Time.at(auth.credentials.expires_at)
user.password = (0...8).map { (65 + rand(26)).chr }.join
user.email = (0...8).map { (65 + rand(26)).chr }.join+"@mailinator.com"
user.save!
end
end
def self.koala(auth)
access_token = auth['token']
facebook = Koala::Facebook::API.new(access_token)
facebook.get_object("me?fields=name,picture")
end
# Returns the hash digest of the given string.
def User.digest(string)
cost = ActiveModel::SecurePassword.min_cost ? BCrypt::Engine::MIN_COST :
BCrypt::Engine.cost
BCrypt::Password.create(string, cost: cost)
end
# Returns a random token.
def User.new_token
SecureRandom.urlsafe_base64
end
# Remembers a user in the database for use in persistent sessions.
def remember
self.remember_token = User.new_token
update_attribute(:remember_digest, User.digest(remember_token))
end
# Forgets a user. NOT SURE IF I REMOVE
def forget
update_attribute(:remember_digest, nil)
end
# Returns true if the given token matches the digest.
def authenticated?(attribute, token)
digest = send("#{attribute}_digest")
return false if digest.nil?
BCrypt::Password.new(digest).is_password?(token)
end
# Activates an account.
def activate
update_attribute(:activated, true)
update_attribute(:activated_at, Time.zone.now)
end
# Sends activation email.
def send_activation_email
UserMailer.account_activation(self).deliver_now
end
def create_reset_digest
self.reset_token = User.new_token
update_attribute(:reset_digest, User.digest(reset_token))
update_attribute(:reset_sent_at, Time.zone.now)
end
# Sends password reset email.
def send_password_reset_email
UserMailer.password_reset(self).deliver_now
end
# Returns true if a password reset has expired.
def password_reset_expired?
reset_sent_at < 2.hours.ago
end
def good_results_count
results.good_count
end
# Follows a user.
def follow(other_user)
active_relationships.create(followed_id: other_user.id)
end
# Unfollows a user.
def unfollow(other_user)
active_relationships.find_by(followed_id: other_user.id).destroy
end
# Returns true if the current user is following the other user.
def following?(other_user)
following.include?(other_user)
end
private
def from_omniauth?
provider && uid
end
# Converts email to all lower-case.
def downcase_email
self.email = email.downcase unless from_omniauth?
end
# Creates and assigns the activation token and digest.
def create_activation_digest
self.activation_token = User.new_token
self.activation_digest = User.digest(activation_token)
end
end
class UsersController < ApplicationController
before_action :logged_in_user, only: [:index, :edit, :update, :destroy,
:following, :followers]
before_action :correct_user, only: [:edit, :update]
before_action :admin_user, only: :destroy
def tag_cloud
@tags = User.tag_counts_on(:tags)
end
def index
@users = User.paginate(page: params[:page])
end
def show
@user = User.find(params[:id])
if current_user == @user
@habits = @user.habits
@valuations = @user.valuations
@accomplished_goals = @user.goals.accomplished
@unaccomplished_goals = @user.goals.unaccomplished
@averaged_quantifieds = @user.quantifieds.averaged
@instance_quantifieds = @user.quantifieds.instance
else
@habits = @user.habits
@valuations = @user.valuations.publish
@accomplished_goals = @user.goals.accomplished
@unaccomplished_goals = @user.goals.unaccomplished
@averaged_quantifieds = @user.quantifieds.averaged
@instance_quantifieds = @user.quantifieds.instance
end
end
def new
@user = User.new
end
def create
@user = User.new(user_params)
if @user.save
@user.send_activation_email
flash[:info] = "Please check your email to activate your account."
redirect_to root_url
else
render 'new'
end
end
def edit
@user = User.find(params[:id])
end
def update
@user = User.find(params[:id])
if @user.update_attributes(user_params)
flash[:success] = "Profile updated"
redirect_to @user
else
render 'edit'
end
end
def destroy
User.find(params[:id]).destroy
flash[:success] = "User deleted"
redirect_to users_url
end
def following
@title = "Following"
@user = User.find(params[:id])
@users = @user.following.paginate(page: params[:page])
render 'show_follow'
end
def followers
@title = "Followers"
@user = User.find(params[:id])
@users = @user.followers.paginate(page: params[:page])
render 'show_follow'
end
private
def user_params
if params[:conceal] = true
params.require(:user).permit(:name, :email, :tag_list, :password, :conceal, :password_confirmation, valuations_attributes: [:name, :tag_list, :conceal], activities_attributes: [:conceal, :action, :trackable_id, :trackable_type])
else
params[:user][:valuations][:conceal] = false
params.require(:user).permit(:name, :image, :tag_list, :email, :password, :password_confirmation, valuations_attributes: [:name, :tag_list], activities_attributes: [:action, :trackable_id, :trackable_type])
end
end
# Before filters
# Confirms a logged-in user.
def logged_in_user
unless logged_in?
store_location
flash[:danger] = "Please log in."
redirect_to login_url
end
end
# Confirms the correct user.
def correct_user
@user = User.find(params[:id])
redirect_to(root_url) unless current_user?(@user)
end
# Confirms an admin user.
def admin_user
redirect_to(root_url) unless current_user.admin?
end
end
class Valuation < ActiveRecord::Base
belongs_to :user
acts_as_taggable
has_many :combine_tags
has_many :notifications
validates :name, presence: true
has_many :notes, as: :notable
scope :publish, ->{ where(:conceal => false) }
has_many :notes, as: :notable
has_many :valuation_likes
has_many :likers, through: :valuation_likes, class_name: 'User', source: :liker
has_many :comment_likes
has_many :comments, as: :commentable
scope :randomize, -> do
order('RANDOM()').
take(1)
end
scope :top_25, -> do
order('RANDOM()').
limit(25)
end
end
class ValuationsController < ApplicationController
before_action :set_valuation, only: [:show, :edit, :update, :destroy, :like]
before_action :logged_in_user, only: [:create, :destroy]
def index
if params[:tag]
@valuations = Valuation.tagged_with(params[:tag])
else
@valuations = Valuation.order('RANDOM()')
end
end
def show
@valuation = Valuation.find(params[:id])
@commentable = @valuation
@comments = @commentable.comments
@comment = Comment.new
@notable = @valuation
@notes = @notable.notes
@note = Note.new
end
def new
@valuation = current_user.valuations.build
end
def edit
end
def create
@valuation = current_user.valuations.build(valuation_params)
if (params[:commit] == 'conceal')
@valuation.conceal = true
@valuation.save
redirect_to @valuation, notice: 'Value was successfully created'
elsif
@valuation.save
track_activity @valuation
redirect_to @valuation, notice: 'Value was successfully created'
else
flash.now[:danger] = 'Required Field: "Enter Value"'
render 'new'
end
end
def update
if @valuation.update(valuation_params)
track_activity @valuation
redirect_to @valuation, notice: 'Value was successfully updated'
else
render action: 'edit'
end
end
def destroy
@valuation.destroy
redirect_to valuations_url
end
def like
@valuation = Valuation.find(params[:id])
@valuation_like = current_user.valuation_likes.build(valuation: @valuation)
if @valuation_like.save
@valuation.increment!(:likes)
flash[:success] = 'Thanks for liking!'
else
flash[:error] = 'Two many likes'
end
redirect_to(:back)
end
private
def set_valuation
@valuation = Valuation.find(params[:id])
end
def correct_user
@valuation = current_user.valuations.find_by(id: params[:id])
redirect_to valuations_path, notice: "Not authorized to edit this valuation" if @valuation.nil?
end
def valuation_params
params.require(:valuation).permit(:name, :conceal, :tag_list, :content, :commentable, :comment, :like)
end
end
valuations/index.html.erb
<table>
<% @valuations.each do |valuation| %>
<tr class="valuation">
<td>
<%= link_to valuation_path(valuation) do %>
<%= valuation.name %>
<% end %>
&nbsp;
<b><%= raw valuation.tag_list.map { |t| link_to t.titleize, tag_path(t), class: 'label label-primary' }.join(' ') %></b>
</td>
</tr>
<% end %>
</table>
</br>
<% content_for :jumbotron do %>
<div class="jumbtron">
<div class="container">
<h1><b>Values</b></h1>
<p>Inspiration and information you strive to emulate and ingrain.</br>
"Your words become your actions, your actions become your habits, </br>
your habits become your values, your values become your destiny.” .</p>
</div>
</div>
<% end %>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment