Permalink
Browse files

overhaul settings library and click_hide 'monkey patch'

patch is not necessary, we can just do data: {click_hide: true} and use UJS to create the same effect
  • Loading branch information...
1 parent aa31d24 commit 5aa596d29bc5462ca6d6b31b2024a78d4aeb7ecf @agrobbin committed Sep 21, 2012
View
@@ -1,3 +1,2 @@
--colour
--format doc
-
@@ -1,9 +1,14 @@
-$(document).on "focus", "input.click-hide", ->
- $(@).val "" if $(@).val() is @defaultValue
+click_hide = ->
+ $(@).val($(@).data('click-hide')).css "color", "#aaa" if $(@).val() is ""
+
+$ ->
+ $("input[data-click-hide]").each click_hide
+
+$(document).on "focus", "input[data-click-hide]", ->
+ $(@).val "" if $(@).val() is $(@).data('click-hide')
$(@).css "color", "#000"
-$(document).on "blur", "input.click-hide", ->
- $(@).val(@defaultValue).css "color", "#aaa" if $(@).val() is ""
+$(document).on "blur", "input[data-click-hide]", click_hide
$(document).on "click", "a.drawer", (e) ->
e.preventDefault()
@@ -9,16 +9,16 @@ $prefixes: ("", "-moz-", "-ms-", "-o-", "-webkit-");
}
@mixin border-radius($size: 4) {
- @include css3-attribute(border-radius, #{$size}px);
- border: {
- color: #e5e5e5 #dbdbdb #d2d2d2;
- width: 1px;
- style: solid;
- }
+ @include css3-attribute(border-radius, #{$size}px);
+ border: {
+ color: #e5e5e5 #dbdbdb #d2d2d2;
+ width: 1px;
+ style: solid;
+ }
}
@mixin box-shadow($opacity: 0.5, $offset: 1, $size: 5) {
- @include css3-attribute(box-shadow, rgba(0, 0, 0, $opacity) 0 #{$offset}px #{$size}px);
+ @include css3-attribute(box-shadow, rgba(0, 0, 0, $opacity) 0 #{$offset}px #{$size}px);
}
@mixin gradient($start: #fff, $end: #000, $start_pct: 0%, $end_pct: 100%, $direction: top) {
@@ -1,20 +1,16 @@
class Professor < User
- SETTINGS = {
- widget_order: {
- name: 'Order of boxes on the homepage',
- type: 'sort',
- options: %w(activity gradebook courses events),
- default: %w(activity gradebook courses events),
- editable: false
- },
- grading_system: {
- name: 'What do you want your default grading system to be',
- type: 'radio',
- options: [['Point System', 'point'], ['Weighting System', 'weight']],
- default: 'point'
- }
- }
+ setting :widget_order,
+ name: 'Order of boxes on the homepage',
+ type: 'sort',
+ options: %w(activity gradebook courses events),
+ default: %w(activity gradebook courses events)
+ setting :grading_system,
+ name: 'What do you want your default grading system to be',
+ type: 'radio',
+ options: [['Point System', 'point'], ['Weighting System', 'weight']],
+ default: 'point',
+ editable: true
has_many :sections, include: :course
has_many :events, through: :sections
View
@@ -1,14 +1,11 @@
class Section < ActiveRecord::Base
- include Settings
- SETTINGS = {
- grading_system: {
- name: 'What kind of grading system do you use for this section',
- type: 'radio',
- options: [['Point System', 'point'], ['Weighting System', 'weight']],
- default: 'point'
- }
- }
+ setting :grading_system,
+ name: 'What kind of grading system do you use for this section',
+ type: 'radio',
+ options: [['Point System', 'point'], ['Weighting System', 'weight']],
+ default: 'point',
+ editable: true
belongs_to :course
belongs_to :semester
@@ -1,14 +1,11 @@
class Sectionship < ActiveRecord::Base
- include Settings
- SETTINGS = {
- send_notifications: {
- name: "Be notified of changes to this section's information",
- type: 'radio',
- options: [['Yes', true], ['No', false]],
- default: true
- }
- }
+ setting :send_notifications,
+ name: "Be notified of changes to this section's information",
+ type: 'radio',
+ options: [['Yes', true], ['No', false]],
+ default: true,
+ editable: true
belongs_to :section, counter_cache: :students_count
belongs_to :student
@@ -1,8 +1,6 @@
class Setting < ActiveRecord::Base
include ActionView::Helpers::FormTagHelper
- scope :editable, where('editable = 1')
-
belongs_to :resource, polymorphic: true
serialize :value
@@ -15,7 +13,7 @@ def key
def build_field!(form)
str = ''
- info = resource_type.constantize::SETTINGS[key]
+ info = self.class.available_settings[key]
case info[:type]
when 'select'
str += form.label :value, info[:name]
@@ -39,7 +37,7 @@ def build_field!(form)
protected
def typecast_value
- case self.value
+ case value
when String
self.value = value.to_i if value == value.to_i.to_s
self.value = value == 'true' ? true : value == 'false' ? false : value
View
@@ -1,20 +1,16 @@
class Student < User
- SETTINGS = {
- widget_order: {
- name: 'Order of boxes on the homepage',
- type: 'sort',
- options: %w(activity gradebook courses events),
- default: %w(activity gradebook courses events),
- editable: false
- },
- calendar_reminders: {
- name: 'When should you be reminded of calendar events',
- type: 'checkbox',
- options: [['1 week before', 168], ['3 days before', 72], ['1 day before', 24], ['12 hours before', 12]],
- default: [168, 72, 24] # hours before
- }
- }
+ setting :widget_order,
+ name: 'Order of boxes on the homepage',
+ type: 'sort',
+ options: %w(activity gradebook courses events),
+ default: %w(activity gradebook courses events)
+ setting :calendar_reminders,
+ name: 'When should you be reminded of calendar events',
+ type: 'checkbox',
+ options: [['1 week before', 168], ['3 days before', 72], ['1 day before', 24], ['12 hours before', 12]],
+ default: [168, 72, 24], # hours before
+ editable: true
has_many :sectionships
has_many :sections, through: :sectionships, include: :course
View
@@ -1,5 +1,4 @@
class User < ActiveRecord::Base
- include Settings
self.abstract_class = true
@@ -2,7 +2,7 @@
%h2 Edit this Course Section
#modal-inner
= form_for @sectionship || @section, url: course_path(@section), remote: true do |f|
- = f.fields_for :settings, (@sectionship || @section).settings.editable do |s|
+ = f.fields_for :settings, (@sectionship || @section).editable_settings do |s|
= s.object.build_field!(s)
%br
%br
@@ -4,7 +4,7 @@
= render 'heading'
#roster
%h2
- %span.right= text_field_tag 'search', nil, class: 'text small', click_hide: 'Search Roster', autocomplete: 'off'
+ %span.right= text_field_tag 'search', nil, class: 'text small', autocomplete: 'off', data: {click_hide: 'Search Roster'}
There #{@section.students_count == 1 ? 'is' : 'are'} #{pluralize @section.students_count, 'student'} enrolled in this course section
- @students.each do |student|
.student.left
@@ -1,72 +0,0 @@
-# This 'monkey patch' adds a :click_hide option to all textual input fields
-#
-# Alex Robbin
-# rev 001 . 2010.08.16
-# rev 002 . 2010.09.09 - Alex Robbin: minor bug fix within click_hide method
-# rev 003 . 2010.09.26 - Alex Robbin: added capability to text_field_tag method
-# rev 004 . 2010.11.08 - Alex Robbin: added capability to text_area_tag method
-# rev 005 . 2011.05.14 - Alex Robbin: updated to match Rails 3 methods; DRYed up the code
-
-module ActionView
- module Helpers
-
- module FormTagHelper
- def text_field_tag(name, value = nil, options = {})
- if value.blank? && click_hide = options.delete(:click_hide)
- options[:class] ||= ''
- options[:class] += ' click-hide'
- options[:value] = click_hide == true ? name.to_s.humanize.titleize : click_hide
- end
- tag :input, { "type" => "text", "name" => name, "id" => sanitize_to_id(name), "value" => value }.update(options.stringify_keys)
- end
-
- def text_area_tag(name, content = nil, options = {})
- options.stringify_keys!
-
- if content.blank? && click_hide = options.delete("click_hide")
- options["class"] ||= ''
- options["class"] += ' click-hide'
- content = click_hide == true ? name.to_s.humanize.titleize : click_hide
- end
-
- if size = options.delete("size")
- options["cols"], options["rows"] = size.split("x") if size.respond_to?(:split)
- end
-
- escape = options.key?("escape") ? options.delete("escape") : true
- content = ERB::Util.html_escape(content) if escape
-
- content_tag :textarea, content.to_s.html_safe, { "name" => name, "id" => sanitize_to_id(name) }.update(options)
- end
- end
-
- def text_field(object_name, method, options = {})
- click_hide object_name, method, options do
- InstanceTag.new(object_name, method, self, options.delete(:object)).to_input_field_tag("text", options)
- end
- end
-
- def password_field(object_name, method, options = {})
- click_hide object_name, method, options do
- InstanceTag.new(object_name, method, self, options.delete(:object)).to_input_field_tag("password", { value: nil }.merge!(options))
- end
- end
-
- def text_area(object_name, method, options = {})
- click_hide object_name, method, options do
- InstanceTag.new(object_name, method, self, options.delete(:object)).to_text_area_tag(options)
- end
- end
-
- private
- def click_hide(object_name, method, options, &block)
- if options[:object].send(method).blank? && click_hide = options.delete(:click_hide)
- options[:class] ||= ''
- options[:class] += ' click-hide'
- options[:value] = click_hide == true ? options[:object].class.human_attribute_name(method) : click_hide
- end
- yield block
- end
-
- end
-end
@@ -1,5 +1,5 @@
require 'calendar'
-require 'email_case_check'
require 'routing'
require 'settings'
require 'uploads'
+require 'validation_display_order'
@@ -84,7 +84,6 @@ def change
create_table :settings do |t|
t.references :resource, polymorphic: true
t.string :key, :value
- t.boolean :editable
t.timestamps
end
View
@@ -154,7 +154,6 @@
t.string "resource_type"
t.string "key"
t.string "value"
- t.boolean "editable"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
@@ -1,33 +0,0 @@
-# This does a case check of your email address before the object is saved.
-# It forces a downcase of the @domain.com part of the email address.
-
-module EmailCaseCheck
-
- def self.included(klass)
- klass.class_eval do
- extend ModelClassMethods
-
- before_save :check_email_case
-
- private
- def check_email_case
- array = self.email_address.split('@')
- self.email_address = "#{array.first}@#{array.last.downcase}"
- end
- end
- end
-
- module ModelClassMethods
-
- HUMANIZED_ATTRIBUTES = {
- email_address: "Email address",
- email_address_confirmation: "Email address confirmation"
- }
-
- def human_attribute_name(attr)
- HUMANIZED_ATTRIBUTES[attr.to_sym] || super
- end
-
- end
-
-end
Oops, something went wrong.

0 comments on commit 5aa596d

Please sign in to comment.