Skip to content

Commit

Permalink
Context-related changes
Browse files Browse the repository at this point in the history
  • Loading branch information
sachac committed Jan 16, 2012
1 parent b924d77 commit 9361293
Show file tree
Hide file tree
Showing 43 changed files with 447 additions and 230 deletions.
6 changes: 6 additions & 0 deletions app/assets/stylesheets/style.css.sass
Expand Up @@ -32,6 +32,8 @@ li.input
form .clear-fix
margin-bottom: 10px
zoom: 1
.row
margin-left: 0px
.actions .btn
margin-right: 10px
.category
Expand Down Expand Up @@ -123,3 +125,7 @@ input.imagebtn
color: #BFBFBF
a[data-method].btn
color: #000
a[data-method].btn.primary
color: #fff
.input table
width: auto
9 changes: 8 additions & 1 deletion app/controllers/application_controller.rb
Expand Up @@ -23,6 +23,9 @@ class ApplicationController < ActionController::Base
def before_awesome
notice_layout!
@account = current_account
# Set the timezone
Time.zone = @account.settings.timezone if @account.settings.timezone

true
end
def notice_layout!
Expand All @@ -45,7 +48,11 @@ def current_account
end

def after_sign_in_path_for(resource)
params[:destination] || stored_location_for(resource) || root_path
if !params[:destination].blank?
params[:destination]
else
stored_location_for(resource) || root_path
end
end

def filter_sortable_column_order(list)
Expand Down
13 changes: 13 additions & 0 deletions app/controllers/clothing_controller.rb
Expand Up @@ -271,4 +271,17 @@ def save_color

end


def download_thumbnail
@clothing = current_account.clothing.find(params[:id])
authorize! :view, @clothing
response.headers['X-Accel-Redirect'] = '/protected/' + @clothing.image.url
response.headers['Content-Type'] = @clothing.image_content_type
response.headers['Content-Disposition'] = "inline; filename=#{@clothing.image_file_name}"
#Make sure we don't render anything
render :nothing => true
# send_file @clothing.image.path, :type => @clothing.image_content_type, :disposition => 'inline'

end

end
56 changes: 36 additions & 20 deletions app/controllers/contexts_controller.rb
Expand Up @@ -25,6 +25,7 @@ def show
def new
authorize! :create, Context
@context = Context.new
5.times do @context.context_rules.build end
respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => @context }
Expand All @@ -34,15 +35,25 @@ def new
# GET /contexts/1/edit
def edit
@context = current_account.contexts.find(params[:id])
5.times do @context.context_rules.build end
authorize! :update, @context
end

# POST /contexts
# POST /contexts.xml
def create
authorize! :create, Context
@context = Context.new(params[:context])
@context.user = current_account
# Change context_rules_attributes to stuff and locations
rules = params[:context].delete :context_rules_attributes if params[:context]
@context = current_account.contexts.new(params[:context])
if rules
rules.each do |k, v|
next if v['stuff'].blank? or v['location'].blank?
stuff = @account.stuff.find_or_create_by_name(v['stuff'])
location = @account.get_location(v['location'])
@context.context_rules.build(:stuff => stuff, :location => location)
end
end
respond_to do |format|
if @context.save
format.html { redirect_to(@context, :notice => 'Context was successfully created.') }
Expand All @@ -59,6 +70,23 @@ def create
def update
@context = current_account.contexts.find(params[:id])
authorize! :update, @context
rules = params[:context][:context_rules_attributes] if params[:context]
if rules
rules.each do |k, v|
if v['stuff'].blank? or v['location'].blank?
params[:context][:context_rules_attributes][k]['_destroy'] = 1
else
location = @account.get_location(v['location'])
stuff = @account.stuff.find_by_name(v['stuff'])
if stuff.nil?
stuff = @account.stuff.create(:name => v['stuff'], :location => location, :home_location => location, :status => 'active', :stuff_type => 'stuff')
end
params[:context][:context_rules_attributes][k]['stuff'] = stuff
params[:context][:context_rules_attributes][k]['location'] = location
end
end
end
logger.info "New context rules attributes " + params[:context][:context_rules_attributes].inspect
respond_to do |format|
if @context.update_attributes(params[:context])
format.html { redirect_to(@context, :notice => 'Context was successfully updated.') }
Expand Down Expand Up @@ -87,28 +115,16 @@ def start
# Parse the list of items in this context
@context = current_account.contexts.find(params[:id])
authorize! :start, @context
@stuff = @context.stuff_rules
@in_place = Array.new
@out_of_place = Array.new
@stuff.each do |name, val|
if val[:in_place]
@in_place << name
else
@out_of_place << name
end
end
@in_place = @context.context_rules.in_place
@out_of_place = @context.context_rules.out_of_place
end

def complete
authorize! :manage_account, current_account
@context = current_account.contexts.find(params[:id])
authorize! :start, @context
@stuff = @context.stuff_rules
@stuff.each do |key, stuff|
unless stuff[:in_place]
stuff[:stuff].location = current_account.get_location(stuff[:destination])
stuff[:stuff].save!
end
@context.context_rules.out_of_place.each do |r|
r.stuff.update_attributes(:location => r.location)
end
redirect_to start_context_path(@context)
go_to stuff_index_path, :notice => "Context marked complete."
end
end
21 changes: 12 additions & 9 deletions app/controllers/stuff_controller.rb
@@ -1,4 +1,6 @@
class StuffController < ApplicationController
autocomplete :stuff, :name, :full => true
skip_authorization_check :only => [:autocomplete_stuff_name]
load_and_authorize_resource
handles_sortable_columns
# GET /stuff
Expand All @@ -15,7 +17,7 @@ def index
"name DESC"
end
end
@stuff = current_account.stuff.order(order).includes(:location)
@stuff = current_account.stuff.order(order).includes(:location).where(:stuff_type => 'stuff')
if params[:status] and params[:status] != 'all'
@stuff = @stuff.where('status=?', 'stuff')
else
Expand All @@ -30,17 +32,13 @@ def index
end

def log
@stuff = Stuff.find(:first, :conditions => [ 'lower(name) = ?', params[:stuff_name].strip.downcase ])
@stuff = current_account.stuff.find(:first, :conditions => [ 'lower(name) = ?', params[:stuff_name].strip.downcase ])
unless @stuff
@stuff = Stuff.new(:name => params[:stuff_name].strip, :status => 'active')
@stuff = current_account.stuff.new(:name => params[:stuff_name].strip, :status => 'active', :stuff_type => 'stuff', :location => @location)
@stuff.user = current_account
end
@location = current_account.get_location(params[:location_name])
@stuff.location = @location
logger.info "Stuff: #{@stuff.id}"
logger.info "Location: #{@location.id}"
logger.info "New location: #{@stuff.location.id}"
logger.info "New location: #{@stuff.location.id}"
result = @stuff.save!
redirect_to params[:destination] || stuff_index_path, :notice => (result ? 'Logged' : 'Problem?') and return
end
Expand All @@ -58,7 +56,7 @@ def show
# GET /stuff/new
# GET /stuff/new.xml
def new
@stuff = Stuff.new
@stuff = current_account.stuff.new

respond_to do |format|
format.html # new.html.erb
Expand All @@ -79,7 +77,7 @@ def create
loc = current_account.get_location(params[:stuff][:home_location_id])
params[:stuff].delete(:home_location_id)
end
@stuff = Stuff.new(params[:stuff])
@stuff = current_account.stuff.new(params[:stuff])
@stuff.home_location = loc
@stuff.location = @stuff.home_location
@stuff.user = current_account
Expand Down Expand Up @@ -131,4 +129,9 @@ def destroy
format.xml { head :ok }
end
end

def get_autocomplete_items(parameters)
super(parameters).where(:user_id => current_account.id)
end

end
11 changes: 10 additions & 1 deletion app/controllers/users_controller.rb
Expand Up @@ -65,9 +65,18 @@ def update
params[:user].delete(:password)
params[:user].delete(:password_confirmation)
end
if params[:user][:settings]
settings = params[:user][:settings]
@user.settings.timezone = settings['time_zone']
logger.info "Trying to set timezone? " + @user.settings.timezone
params[:user].delete(:settings)
end
respond_to do |format|
if @user.update_attributes(params[:user])
format.html { redirect_to(@user, :notice => 'User was successfully updated.') }
format.html {
logger.info "Did we keep the timezone? " + @user.settings.timezone

redirect_to(root_path, :notice => 'User was successfully updated.') }
format.xml { head :ok }
else
format.html { render :action => "edit" }
Expand Down
7 changes: 6 additions & 1 deletion app/helpers/application_helper.rb
Expand Up @@ -135,7 +135,12 @@ def actions(o)
if can? :manage_account, current_account
actions << link_to('Edit', edit_record_path(o))
actions << link_to('Clone', clone_record_path(o), :method => :post)
actions << link_to('Destroy', o, :confirm => 'Are you sure?', :method => :delete)
actions << link_to('Delete', o, :confirm => 'Are you sure?', :method => :delete)
end
elsif o.is_a? Context
if managing?
actions << link_to('Edit', edit_context_path(o))
actions << link_to('Start', start_context_path(o))
end
end
actions
Expand Down
28 changes: 28 additions & 0 deletions app/helpers/stuff_helper.rb
@@ -1,2 +1,30 @@
module StuffHelper
def name_and_location(stuff)
s = stuff.name
if stuff.location
s += ' < ' + stuff.hierarchy.map {|l| link_to h(l.name), l}.join(h(' < ')).html_safe
end
if managing?
if stuff.stuff_type == 'stuff' and stuff.home_location and stuff.location != stuff.home_location
s += ' ' + link_to("(return to #{stuff.home_location.name})", log_stuff_path(:stuff_name => stuff.name, :location_name => stuff.home_location.name), :method => :post)
end
end
s
end

def move_link_to(stuff, location)
if managing?
s = link_to location.name, log_stuff_path(:stuff_name => stuff.name, :location_name => location.name), :method => :post
s += ' ('
s += link_to 'view', location
s += ')'
s
else
link_to location.name, location
end
end

def location_list(stuff, list)
list.map { |location| move_link_to(stuff, location) }.join(', ').html_safe
end
end
20 changes: 8 additions & 12 deletions app/models/ability.rb
Expand Up @@ -6,30 +6,26 @@ def initialize(user)
if user.role == 'admin'
can :manage, :all
elsif !user.id.blank?
[Clothing, ClothingLog, ClothingMatch, CsaFood, Day, DecisionLog, Decision, Food, LibraryItem, LocationHistory, Location, MeasurementLog, Measurement, Stuff, TimeRecord, TorontoLibrary, Context, Memory, TapLogRecord, RecordCategory].each do |item|
[Clothing, ClothingLog, Context, ClothingMatch, CsaFood, Day, DecisionLog, Decision, Food, LibraryItem, LocationHistory, Location, MeasurementLog, Measurement, Stuff, TimeRecord, TorontoLibrary, Memory, TapLogRecord, RecordCategory].each do |item|
can :manage, item, :user_id => user.id
can :create, item
end
can :manage_account, User, :id => user.id
can :view_library_items, User
can :view_contexts, User
can :view_tap_log_records, User, :id => user.id
can :view_time, User
can :send_feedback, User
else # Not logged in
can :view, LibraryItem, :public => true
can :view, Memory, :access => 'public'
can :view_contexts, User
end
[:view_locations, :view_dashboard, :view_clothing, :view_time, :view_library_items, :view_memories, :view_tap_log_records].each do |sym|
can :view, LibraryItem do |o| o.public? and o.user.demo? end
can :view, Memory do |o| o.public? and o.user.demo? end
[:view_contexts, :view_locations, :view_dashboard, :view_clothing, :view_time, :view_library_items, :view_memories, :view_tap_log_records].each do |sym|
can sym, User do |u|
u.id == 1 || u.id == user.id
end
end
can :view, Clothing
can :view, TapLogRecord
can :view_note, TapLogRecord do |u|
(u.user_id == 1 && !u.private?) || u.user_id = user.id
can :view, Clothing do |o| o.user.demo? end
can :view, TapLogRecord do |o| o.user.demo? end
can :view_note, TapLogRecord do |o|
(!o.private? and o.user.demo?) || (o.user_id == user.id)
end
can :view_site, User
can :send_feedback, User
Expand Down
2 changes: 1 addition & 1 deletion app/models/clothing.rb
Expand Up @@ -3,7 +3,7 @@ class Clothing < ActiveRecord::Base
acts_as_taggable_on :tags
has_many :clothing_logs
has_many :clothing_matches, :foreign_key => :clothing_a_id
has_attached_file :image, :styles => { :large => "400x400", :medium => "x90", :small => "x40" }, :default_url => '/images/clothing/:style/missing.jpg'
has_attached_file :image, :styles => { :large => "400x400", :medium => "x90", :small => "x40" }, :default_url => '/images/clothing/:style/missing.jpg', :path => ':rails_root/files/clothing/:user_id/:id/:style/:basename.:extension', :url => '/files/clothing/:user_id/:id/:style/:basename.:extension'
before_save :update_hsl

def autocomplete_view
Expand Down
25 changes: 6 additions & 19 deletions app/models/context.rb
@@ -1,24 +1,11 @@
class Context < ActiveRecord::Base
belongs_to :user
def rules_array
self.rules.split /[\r\n]+/
end
has_many :context_rules
accepts_nested_attributes_for :context_rules, :allow_destroy => true
validates_presence_of :name
before_save :update_rules

def stuff_rules
rules = self.rules_array
stuff = Hash.new { |h,k| h[k] = Hash.new }
rules.each do |r|
matches = r.match(/^stuff:[ \t]*([^,]+),[ \t]*(.*)/)
if matches
stuff[matches[1]][:destination] = matches[2]
end
end
stuff_hash = self.user.stuff.includes(:location).where('name in (?)', stuff.keys)
stuff_hash.each do |s|
stuff[s.name][:stuff] = s
stuff[s.name][:in_place] = (s.location and s.location.name == stuff[s.name][:destination])
end
stuff
def update_rules
self.rules = self.context_rules.includes(:stuff).order('LOWER(stuff.name)').map { |x| x.stuff.name }.join(', ')
end

end
7 changes: 7 additions & 0 deletions app/models/context_rule.rb
@@ -0,0 +1,7 @@
class ContextRule < ActiveRecord::Base
belongs_to :stuff
belongs_to :location, :class_name => 'Stuff'
belongs_to :context
scope :out_of_place, includes(:stuff).where('(stuff.location_id IS NULL OR stuff.location_id != context_rules.location_id)')
scope :in_place, includes(:stuff).where('stuff.location_id = context_rules.location_id')
end
2 changes: 1 addition & 1 deletion app/models/location_history.rb
@@ -1,5 +1,5 @@
class LocationHistory < ActiveRecord::Base
belongs_to :user
belongs_to :location, :polymorphic => true
belongs_to :location, :class_name => 'Stuff'
belongs_to :stuff
end
4 changes: 4 additions & 0 deletions app/models/memory.rb
Expand Up @@ -7,4 +7,8 @@ class Memory < ActiveRecord::Base
def private?
self.access != 'public'
end

def public?
self.access == 'public'
end
end

0 comments on commit 9361293

Please sign in to comment.