casey_jones 0.0.120 → 0.0.121

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,5 +1,15 @@
1
1
  module CaseyJones
2
2
  module ActionControllerClassMethods
3
+ def load_main_menu
4
+ class_eval do
5
+ def main_menu_loader
6
+
7
+ @menu_items = Sitemap.main_menu.children.sorted
8
+ end
9
+ before_filter :main_menu_loader
10
+ end
11
+ end
12
+
3
13
  def resource_attributes(*parameters)
4
14
  for action in [:show, :new, :edit, :create]
5
15
  a = "def #{action};" + ([:new, :create].include?(action) ?
@@ -1,56 +1,59 @@
1
1
  module AjaxLoading
2
2
  module ControllerClassMethods
3
- def ajax_loading
3
+ def ajax_loading(options={})
4
4
  class_eval do
5
5
  include AjaxLoading::ActionControllerExtensions
6
6
  helper_method :container, :ajax_function
7
+ cattr_accessor :ajax_loading_settings
8
+ self.ajax_loading_settings = ajax_loading_defaults.merge(options)
7
9
  end
8
10
  end
11
+
12
+ def ajax_loading_defaults
13
+ {
14
+ :container => {
15
+ :index => "main_container",
16
+ :show => "main_container",
17
+ :new => "main_container",
18
+ :create=> "main_container",
19
+ :edit => proc{|resource| resource.element_id},
20
+ :update=> proc{|resource| resource.element_id},
21
+ :destroy=>proc{|resource| resource.element_id}
22
+ },
23
+ :ajax_function => {
24
+ :index => "html",
25
+ :show => "html",
26
+ :new => "append",
27
+ :create=> "append",
28
+ :edit => "html",
29
+ :update=> "html",
30
+ :destroy=>"remove"
31
+ }
32
+ }
33
+ end
9
34
  end
35
+
10
36
  module ActionControllerExtensions
11
37
  def container
12
- @container ||= (params[:container] || choose_container)
38
+ @container ||= params[:container] || default_container
13
39
  end
40
+
14
41
  def ajax_function
15
- @ajax_function ||= (params[:ajax_function] || choose_ajax_function)
42
+ @ajax_function ||= params[:ajax_function] || default_ajax_function
16
43
  end
17
44
 
18
- def choose_container
19
- case params[:action].to_sym
20
- when :destroy
21
- resource.element_id
22
- when :create
23
- "main_container"
24
- when :new
25
- "main_container"
26
- when :edit
27
- resource.element_id
28
- when :index
29
- "main_container"
30
- when :show
31
- "main_container"
32
- when :update
33
- resource.element_id
34
- end
45
+ def default_ajax_function(action=nil)
46
+ action ||= params[:action]
47
+ action = action.to_sym
48
+ ajax_loading_settings[:ajax_function][action]
35
49
  end
36
50
 
37
- def choose_ajax_function
38
- case params[:action].to_sym
39
- when :destroy
40
- "remove"
41
- when :create
42
- "append"
43
- when :new
44
- "append"
45
- when :edit
46
- "html"
47
- when :show
48
- "html"
49
- when :index
50
- "html"
51
- when :update
52
- "html"
53
- end
51
+ def default_container(action=nil)
52
+ action ||= params[:action]
53
+ action = action.to_sym
54
+ val = ajax_loading_settings[:container][action]
55
+ val = val.call(resource) if val.is_a? Proc
56
+ val
54
57
  end
55
58
  end
56
59
  end
@@ -1,5 +1,20 @@
1
1
  module AjaxLoading
2
2
  module ApplicationHelpers
3
+ def ajax_loading_fields(container, ajax_function)
4
+ action = resource.new_record? ? :create : :update
5
+ container ||= default_container(action)
6
+ ajax_function ||= default_container(action)
7
+ content_tag(:input, nil, :type=>"hidden", :name=>"container", :value=>container) +
8
+ content_tag(:input, nil, :type=>"hidden", :name=>"ajax_function", :value=>ajax_function)
9
+ end
10
+
11
+ def default_ajax_function
12
+ case params[:action]
13
+ when "index"
14
+ end
15
+ end
16
+
17
+
3
18
  def ajax_box_id(object, association)
4
19
  object.element_id(:show, association)
5
20
  end
data/lib/casey_jones.rb CHANGED
@@ -1,6 +1,5 @@
1
- require 'casey_jones/active_record_extensions'
2
- require 'casey_jones/application_helper_methods'
3
- require 'casey_jones/string_reader'
1
+ require 'casey_jones/casey_jones'
2
+
4
3
  require 'ajax_loading/ajax_loading'
5
4
  require 'acts_as_linkable/acts_as_linkable'
6
5
  require 'casey_jones/casey_jones_engine'
@@ -3,7 +3,7 @@ module CaseyJones
3
3
  module ActiveRecord
4
4
  def acts_as_sitemap
5
5
  class_eval do
6
- has_one :sitemap, :conditions=>"resource_class = #{name.underscore}", :foreign_key=>'resource_id'
6
+ has_many :sitemaps, :as=>:resource
7
7
 
8
8
  def menu_text
9
9
  self.sitemap.menu_text if self.sitemap
@@ -12,7 +12,6 @@ module CaseyJones
12
12
  self.sitemap ||= Sitemap.new(:resource=>self)
13
13
  self.sitemap.menu_text = t unless t.empty?
14
14
  end
15
- validates :menu_text, :presence=>true, :length=>{:minimum=>3, :maximum=>50}
16
15
  end
17
16
  end
18
17
 
@@ -0,0 +1,5 @@
1
+ require 'casey_jones/active_record_extensions'
2
+ require 'casey_jones/application_helper_methods'
3
+ require 'casey_jones/string_reader'
4
+ require 'casey_jones/validation/validation'
5
+
@@ -0,0 +1,19 @@
1
+ class EqualToValidator < ActiveModel::EachValidator
2
+ attr_accessor :other
3
+ def initialize(options)
4
+ self.other = options[:other]
5
+ super(options)
6
+ end
7
+
8
+ def validate_each(record,attribute,value)
9
+ if self.other==:password
10
+ puts "checking password. I was given #{value}. User's @pw=#{record.instance_variable_get("@password")}"
11
+ r = (value==record.instance_variable_get("@password"))
12
+ else
13
+ puts "checking #{attribute} not password. I was given #{value}. User's other=#{record.attributes[self.other]}"
14
+ r = (value == record.attributes[self.other])
15
+ end
16
+ record.errors[attribute] << (options[:message] || "must be equal to #{self.other}") unless r
17
+ end
18
+ end
19
+
@@ -0,0 +1,124 @@
1
+ class JQueryValidatingFormBuilder < ActionView::Helpers::FormBuilder
2
+ def initialize(object_name, object, template, options, proc)
3
+ self.form_validations = (object._validators || {})
4
+ super(object_name, object, template, options, proc)
5
+ end
6
+
7
+ attr_accessor :form_validations
8
+ cattr_accessor :validation_methods
9
+
10
+ #Adds error message directly inline to a form label
11
+ #Accepts all the options normall passed to form.label as well as:
12
+ # :hide_errors - true if you don't want errors displayed on this label
13
+ # :additional_text - Will add additional text after the error message or after the label if no errors
14
+ def label(method, text = nil, options = {})
15
+ #Check to see if text for this label has been supplied and humanize the field name if not.
16
+ text = text || method.to_s.humanize
17
+ #Get a reference to the model object
18
+ object = @template.instance_variable_get("@#{@object_name}")
19
+
20
+ #Make sure we have an object and we're not told to hide errors for this label
21
+ unless object.nil? || options[:hide_errors]
22
+ #Check if there are any errors for this field in the model
23
+ errors = object.errors.on(method.to_sym)
24
+ if errors
25
+ #Generate the label using the text as well as the error message wrapped in a span with error class
26
+ text += " <span class=\"error\">#{errors.is_a?(Array) ? errors.first : errors}</span>"
27
+ end
28
+ end
29
+ #Add any additional text that might be needed on the label
30
+ text += " #{options[:additional_text]}" if options[:additional_text]
31
+ #Finally hand off to super to deal with the display of the label
32
+ super(method, text, options)
33
+ end
34
+
35
+ def text_field(method, options = {})
36
+ options = set_validations_for(method, options)
37
+ puts options.inspect
38
+ super(method, options)
39
+ end
40
+
41
+ def password_field(method, options={})
42
+ options = set_validations_for(method, options)
43
+ super(method, options)
44
+ end
45
+
46
+ def hidden_field(method, options={})
47
+ options = set_validations_for(method, options)
48
+ super(method, options)
49
+ end
50
+
51
+ def text_area(method, options={})
52
+ options = set_validations_for(method, options)
53
+ super(method, options)
54
+ end
55
+
56
+ # make a hash of the class name and validator class for each validator
57
+ # that governs this form field, but only the ones that are represented in
58
+ # our local validation_methods hash.
59
+ def set_validations_for(method, options={})
60
+ options.extend(OptionsAddClass)
61
+ my_validations = self.form_validations[method].
62
+ map_hash{|t|{t.class.name.underscore.split('/').last=>t}}.
63
+ symbolize_keys.
64
+ slice(*self.validation_methods.keys)
65
+
66
+ my_validations.each do |class_name, validator_class|
67
+ options = validation_methods[class_name].call(validator_class, options)
68
+ end
69
+ return options
70
+ end
71
+
72
+ def self.add_validator(name, proc)
73
+ self.validation_methods[name] = proc
74
+ end
75
+
76
+ self.validation_methods = {
77
+ :presence_validator => lambda{|validation, options|
78
+ puts "presence_validator #{validation.inspect}"
79
+ options.add_class("required")
80
+ return options
81
+ },
82
+
83
+ :email_validator => lambda{|validation, options|
84
+ puts "presence_validator #{validation.inspect}"
85
+ options.add_class("email")
86
+ return options
87
+ },
88
+
89
+ :length_validator => lambda{|validation, options|
90
+ puts "length_validator #{validation.inspect}"
91
+ options[:minlength] = validation.options[:minimum] if validation.options[:minimum]
92
+ options[:maxlength] = validation.options[:maximum] if validation.options[:maximum]
93
+ return options
94
+ },
95
+
96
+ :password_validator => lambda{|validation, options|
97
+ puts "length_validator #{validation.inspect}"
98
+ options["data-verify-user-path"] = '/verify_password'
99
+ options.add_class("verify_user")
100
+ return options
101
+ },
102
+
103
+ :equal_to_validator => lambda{|validation, options|
104
+ puts "equal_to_validator #{validation.inspect}"
105
+ options[:equalTo] = "##{validation.other}"
106
+ return options
107
+ }
108
+ }
109
+
110
+
111
+ end
112
+
113
+ module OptionsAddClass
114
+ def add_class(name)
115
+ puts "add_class(#{name})"
116
+ str = self[:class]
117
+ if str.empty?
118
+ self[:class] = name
119
+ else
120
+ self[:class] = "#{str} #{name}" unless str.include?(name)
121
+ end
122
+ end
123
+ end
124
+
@@ -0,0 +1,38 @@
1
+ class JQueryValidator
2
+ attr_accessor :validators
3
+
4
+ def initialize
5
+ self.validators = {}
6
+ end
7
+
8
+ def validate(method)
9
+ self.validators[method] = ValidationConfig.new
10
+ return self.validators[method]
11
+ end
12
+
13
+ def [] (method)
14
+ return self.validators[method]
15
+ end
16
+ end
17
+
18
+ class ValidationConfig
19
+ attr_accessor :config
20
+ def initialize; self.config = {}; end;
21
+
22
+ def email
23
+ self.config[:email] = true
24
+ return self
25
+ end
26
+
27
+ def min_length(i)
28
+ self.config[:minlength] = i
29
+ return self
30
+ end
31
+
32
+ def url
33
+ self.config[:url] = true
34
+ return self
35
+ end
36
+
37
+ end
38
+
@@ -0,0 +1,7 @@
1
+ class PasswordValidator < ActiveModel::EachValidator
2
+ def validate_each(record,attribute,value)
3
+ r = record.valid_password? value
4
+ record.errors[attribute] << (options[:message] || "Previous Password is incorrect") unless r
5
+ end
6
+ end
7
+
@@ -0,0 +1,7 @@
1
+ class PasswordValidator < ActiveModel::EachValidator
2
+ def validate_each(record,attribute,value)
3
+ r = record.valid_password? value
4
+ record.errors[attribute] << (options[:message] || "Previous Password is incorrect") unless r
5
+ end
6
+ end
7
+
@@ -0,0 +1,7 @@
1
+ class SitemapResourceValidator < ActiveModel::EachValidator
2
+ def validate_each(record,attribute,value)
3
+ r = record.valid_password? value
4
+ record.errors[attribute] << (options[:message] || "Previous Password is incorrect") unless r
5
+ end
6
+ end
7
+
@@ -0,0 +1,6 @@
1
+ require 'casey_jones/validation/password_validator'
2
+ require 'casey_jones/validation/equal_to_validator'
3
+ require 'casey_jones/validation/j_query_validator'
4
+ require 'casey_jones/validation/sitemap_resource_validator'
5
+ require 'casey_jones/validation/j_query_validating_form_builder'
6
+
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 0
8
- - 120
9
- version: 0.0.120
8
+ - 121
9
+ version: 0.0.121
10
10
  platform: ruby
11
11
  authors:
12
12
  - Tyler Gannon
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-09-03 00:00:00 -07:00
17
+ date: 2010-09-08 00:00:00 -07:00
18
18
  default_executable:
19
19
  dependencies: []
20
20
 
@@ -42,8 +42,16 @@ files:
42
42
  - lib/casey_jones.rb
43
43
  - lib/casey_jones/active_record_extensions.rb
44
44
  - lib/casey_jones/application_helper_methods.rb
45
+ - lib/casey_jones/casey_jones.rb
45
46
  - lib/casey_jones/casey_jones_engine.rb
46
47
  - lib/casey_jones/string_reader.rb
48
+ - lib/casey_jones/validation/equal_to_validator.rb
49
+ - lib/casey_jones/validation/j_query_validating_form_builder.rb
50
+ - lib/casey_jones/validation/j_query_validator.rb
51
+ - lib/casey_jones/validation/password_validator.rb
52
+ - lib/casey_jones/validation/sitemap_parents_validator.rb
53
+ - lib/casey_jones/validation/sitemap_resource_validator.rb
54
+ - lib/casey_jones/validation/validation.rb
47
55
  - lib/generators/anaf_habtm/anaf_habtm_generator.rb
48
56
  - lib/generators/anaf_habtm/assets/nested_attributes.css
49
57
  - lib/generators/anaf_habtm/assets/nested_attributes.js