From 5c6fe6678168235d25951110a90ddf24ee858b15 Mon Sep 17 00:00:00 2001 From: Rian Rainey Date: Tue, 19 Mar 2013 21:43:16 -0500 Subject: [PATCH] Having app to a working state. Entries work. Working on leaderboard associations --- Gemfile | 4 + Gemfile.lock | 10 + app/assets/javascripts/script.js | 5 +- app/controllers/application_controller.rb | 2 +- app/controllers/entries_controller.rb | 90 + app/controllers/users_controller.rb | 5 +- app/controllers/workouts_controller.rb | 29 +- app/helpers/entries_helper.rb | 2 + app/models/entry.rb | 8 + app/models/user.rb | 7 + app/models/workout.rb | 3 + app/views/entries/_form.html.erb | 27 + app/views/entries/edit.html.erb | 5 + app/views/entries/index.html.erb | 37 + app/views/entries/new.html.erb | 5 + app/views/entries/show.html.erb | 5 + app/views/layouts/_header.html.erb | 4 +- app/views/layouts/admin.html.erb | 1 + app/views/layouts/application.html.erb | 2 + app/views/workouts/index.html.erb | 18 +- .../workouts/individual_workouts.html.erb | 29 + app/views/workouts/leaderboard.html.erb | 24 + config/routes.rb | 17 +- db/migrate/20130112213530_create_entries.rb | 8 + .../20130125204354_add_fields_to_entries.rb | 11 + .../20130130020524_add_roles_to_table.rb | 13 + db/schema.rb | 14 +- logfile | 2113 +++++++++++++++++ 28 files changed, 2473 insertions(+), 25 deletions(-) create mode 100644 app/controllers/entries_controller.rb create mode 100644 app/helpers/entries_helper.rb create mode 100644 app/models/entry.rb create mode 100644 app/views/entries/_form.html.erb create mode 100644 app/views/entries/edit.html.erb create mode 100644 app/views/entries/index.html.erb create mode 100644 app/views/entries/new.html.erb create mode 100644 app/views/entries/show.html.erb create mode 100644 app/views/workouts/individual_workouts.html.erb create mode 100644 app/views/workouts/leaderboard.html.erb create mode 100644 db/migrate/20130112213530_create_entries.rb create mode 100644 db/migrate/20130125204354_add_fields_to_entries.rb create mode 100644 db/migrate/20130130020524_add_roles_to_table.rb diff --git a/Gemfile b/Gemfile index cc5a1de..f8c183f 100644 --- a/Gemfile +++ b/Gemfile @@ -10,12 +10,16 @@ gem 'bootstrap-wysihtml5-rails' gem 'devise' group :development, :test do + gem 'pry' + gem 'pry-rails' gem 'rspec-rails' gem 'annotate' # Automatic model documentation gem 'guard-rspec' # Automate testing gem 'factory_girl_rails' # Uses test data for tests gem 'capybara' # Helps imitate a browser gem 'faker' # Generates fake test data + gem 'awesome_print' + gem 'better_errors' end group :test do diff --git a/Gemfile.lock b/Gemfile.lock index bfc96d0..661b2b3 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -32,7 +32,11 @@ GEM annotate (2.5.0) rake arel (3.0.2) + awesome_print (1.1.0) bcrypt-ruby (3.0.1) + better_errors (0.7.2) + coderay (>= 1.0.0) + erubis (>= 2.6.6) bootstrap-sass (2.0.4.0) bootstrap-wysihtml5-rails (0.3.1.10) railties (>= 3.0) @@ -104,6 +108,8 @@ GEM coderay (~> 1.0.5) method_source (~> 0.8) slop (~> 3.3.1) + pry-rails (0.2.2) + pry (>= 0.9.10) rack (1.4.1) rack-cache (1.2) rack (>= 0.4) @@ -177,6 +183,8 @@ PLATFORMS DEPENDENCIES annotate + awesome_print + better_errors bootstrap-sass (= 2.0.4) bootstrap-wysihtml5-rails capybara @@ -187,6 +195,8 @@ DEPENDENCIES guard-rspec jquery-rails pg + pry + pry-rails rails (= 3.2.8) rspec-rails sass-rails (~> 3.2.3) diff --git a/app/assets/javascripts/script.js b/app/assets/javascripts/script.js index e4c15e9..c1d8ec5 100644 --- a/app/assets/javascripts/script.js +++ b/app/assets/javascripts/script.js @@ -1,6 +1,9 @@ $(document).ready(function(){ wysihtmlfive(); + + $("#entry_date_performed").datepicker({ + dateFormat: 'yy-mm-dd'}); }); // Apply wysihtml5 to appropriate text areas @@ -8,4 +11,4 @@ function wysihtmlfive() { $('.wysihtml5').each(function(i, elem) { $(elem).wysihtml5(); }); -} \ No newline at end of file +} diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 50d0a21..b2acfaa 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -2,7 +2,7 @@ class ApplicationController < ActionController::Base protect_from_forgery def require_admin - unless user_signed_in? and current_user.has_role?(["Super Admin", "Admin"]) + unless user_signed_in? and current_user.is_admin? flash[:error] = "You must be an admin to access this section." redirect_to root_path # halts request cycle end diff --git a/app/controllers/entries_controller.rb b/app/controllers/entries_controller.rb new file mode 100644 index 0000000..f1d6765 --- /dev/null +++ b/app/controllers/entries_controller.rb @@ -0,0 +1,90 @@ +class EntriesController < ApplicationController + # GET /entries + # GET /entries.json + def index + @entries = Entry.find_all_by_workout_id(params[:workout_id]) + @workout = Workout.find(params[:workout_id]) + + respond_to do |format| + format.html # index.html.erb + format.json { render json: @entries } + end + end + + # GET /entries/1 + # GET /entries/1.json + def show + @entry = Entry.find(params[:id]) + + respond_to do |format| + format.html # show.html.erb + format.json { render json: @entry } + end + end + + # GET /entries/new + # GET /entries/new.json + def new + @entry = Entry.new + @workout = Workout.find(params[:workout_id]) + + respond_to do |format| + format.html # new.html.erb + format.json { render json: @entry } + end + end + + # GET /entries/1/edit + def edit + #binding.pry + @entry = Entry.find(params[:id]) + @workout = Workout.find(params[:workout_id]) + end + + # POST /entries + # POST /entries.json + def create + @entry = Entry.new(params[:entry]) + @entry.workout_id = params[:workout_id] + @entry.user_id = current_user.id + #@entry.date_performed(params[:entry][:date_performed]) + + respond_to do |format| + if @entry.save + format.html { redirect_to workout_entries_path(params[:workout_id]), notice: 'Entry was successfully created.' } + format.json { render json: @entry, status: :created, location: @entry } + else + format.html { render action: "new" } + format.json { render json: @entry.errors, status: :unprocessable_entity } + end + end + end + + # PUT /entries/1 + # PUT /entries/1.json + def update + @entry = Entry.find(params[:id]) + + respond_to do |format| + if @entry.update_attributes(params[:entry]) + format.html { redirect_to workout_entries_path, notice: 'Entry was successfully updated.' } + format.json { head :no_content } + else + format.html { render action: "edit" } + format.json { render json: @entry.errors, status: :unprocessable_entity } + end + end + end + + # DELETE /entries/1 + # DELETE /entries/1.json + def destroy + @entry = Entry.find(params[:id]) + @entry.destroy + + respond_to do |format| + format.html { redirect_to workout_entries_path, :notice => "Entry successfully deleted." } + format.json { head :no_content } + end + end +end diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index f46a85f..557fbc4 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -3,7 +3,10 @@ class UsersController < ApplicationController # GET /users/1 # GET /users/1.json def show - @user = User.find(params[:id]) + #@user = User.find(params[:id]) + puts current_user.id + puts params[:id] + @user = User.find(1) #current_user respond_to do |format| format.html # show.html.erb diff --git a/app/controllers/workouts_controller.rb b/app/controllers/workouts_controller.rb index 4871886..4e873ca 100644 --- a/app/controllers/workouts_controller.rb +++ b/app/controllers/workouts_controller.rb @@ -1,10 +1,29 @@ class WorkoutsController < ApplicationController def index - @workouts = Workout.all + @workouts = Workout.all - respond_to do |format| - format.html # index.html.erb - format.json { render json: @workouts } - end + respond_to do |format| + format.html # index.html.erb + format.json { render json: @workouts } + end + end + + def individual_workouts + @entries = Entry.find_all_by_user_id(params[:user_id]) + @workout = Workout.find_by_id(@entries.first.workout_id) + + respond_to do |format| + format.html + format.json { render json: @workouts } + end + end + + def leaderboard + @entries = Entry.find_all_by_workout_id(params[:workout_id]).first(20) + @workout = Workout.find_by_id(params[:workout_id]) + + respond_to do |format| + format.html + end end end diff --git a/app/helpers/entries_helper.rb b/app/helpers/entries_helper.rb new file mode 100644 index 0000000..ed8c595 --- /dev/null +++ b/app/helpers/entries_helper.rb @@ -0,0 +1,2 @@ +module EntriesHelper +end diff --git a/app/models/entry.rb b/app/models/entry.rb new file mode 100644 index 0000000..c384f1d --- /dev/null +++ b/app/models/entry.rb @@ -0,0 +1,8 @@ +class Entry < ActiveRecord::Base + attr_accessible :id, :body, :user_id, :workout_id, :time, :rounds, :repetitions, :date_performed + + #accepts_nested_attributes_for :workout, :user + + belongs_to :workout + belongs_to :user +end diff --git a/app/models/user.rb b/app/models/user.rb index 406bd3a..68fc55f 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -23,11 +23,18 @@ class User < ActiveRecord::Base has_and_belongs_to_many :roles accepts_nested_attributes_for :roles + has_many :entries + has_many :workouts, :through => :entries + # Check if user has a given role def has_role?(*role_names) self.roles.where(:name => role_names).present? end + def is_admin? + admin_roles = ["Super Admin", "Admin"] + self.roles.where(:name => admin_roles).present? + end # Join first and last names of user def name [firstName, lastName].join(" ") diff --git a/app/models/workout.rb b/app/models/workout.rb index a09794f..dd86461 100644 --- a/app/models/workout.rb +++ b/app/models/workout.rb @@ -1,3 +1,6 @@ class Workout < ActiveRecord::Base attr_accessible :body, :title + + has_many :workouts + has_many :users, :through => :workouts end diff --git a/app/views/entries/_form.html.erb b/app/views/entries/_form.html.erb new file mode 100644 index 0000000..c5ca528 --- /dev/null +++ b/app/views/entries/_form.html.erb @@ -0,0 +1,27 @@ +<%= form_for([@workout, @entry]) do |f| %> +

Workout Entry for <%= @workout.title %>

+ +
+ <%= f.label :Rounds %> + <%= f.text_field :rounds, :class => "input-medium" %> +
+ +
+ <%= f.label :Repetitions %> + <%= f.text_field :repetitions, :class => "input-medium" %> +
+ +
+ <%= f.text_area :body, :placeholder => "Enter workout entry description here...", :class => "wysihtml5", :style => "width: 50%;", :rows => 5 %> +
+ +
+ <%= f.label :date_performed, "Date Performed" %> + <%= f.text_field :date_performed, :class => "input-medium" %> +
+ + +
+ <%= f.submit :class => "btn btn-primary" %> +
+<% end %> diff --git a/app/views/entries/edit.html.erb b/app/views/entries/edit.html.erb new file mode 100644 index 0000000..586f03e --- /dev/null +++ b/app/views/entries/edit.html.erb @@ -0,0 +1,5 @@ +

Editing entry

+ +<%= render 'form' %> + +<%= link_to 'Back', workout_entries_path %> diff --git a/app/views/entries/index.html.erb b/app/views/entries/index.html.erb new file mode 100644 index 0000000..3f66db6 --- /dev/null +++ b/app/views/entries/index.html.erb @@ -0,0 +1,37 @@ +

Listing Workout Entries For <%= @workout.title %>

+ + + + + + + + + + + +<% @entries.each do |entry| %> + + + + + + + + +<% end %> +
DateDescriptionRoundsRepetitionsTimeActions
<%= entry.date_performed.strftime("%m/%d/%y") %><%= entry.body %><%= entry.rounds %><%= entry.repetitions %><%= entry.time %> +
+ Action + +
+
+ +
+<%= link_to 'Add Entry', new_workout_entry_path %> | +<%= link_to 'Back', workouts_path %> + diff --git a/app/views/entries/new.html.erb b/app/views/entries/new.html.erb new file mode 100644 index 0000000..b83aa07 --- /dev/null +++ b/app/views/entries/new.html.erb @@ -0,0 +1,5 @@ +

New entry

+ +<%= render 'form' %> + +<%= link_to 'Back', workout_entries_path(2) %> diff --git a/app/views/entries/show.html.erb b/app/views/entries/show.html.erb new file mode 100644 index 0000000..d09398c --- /dev/null +++ b/app/views/entries/show.html.erb @@ -0,0 +1,5 @@ +

<%= notice %>

+ + +<%= link_to 'Edit', edit_entry_path(@entry) %> | +<%= link_to 'Back' %> diff --git a/app/views/layouts/_header.html.erb b/app/views/layouts/_header.html.erb index e071a10..bf79a64 100644 --- a/app/views/layouts/_header.html.erb +++ b/app/views/layouts/_header.html.erb @@ -9,7 +9,7 @@