puffer 0.0.5 → 0.0.6

Sign up to get free protection for your applications and to get access to all the features.
Files changed (105) hide show
  1. data/.rspec +1 -0
  2. data/Gemfile +2 -1
  3. data/Gemfile.lock +12 -0
  4. data/VERSION +1 -1
  5. data/app/cells/puffer/base/additional.html.erb +17 -0
  6. data/app/cells/puffer/base_cell.rb +25 -0
  7. data/app/controllers/admin/dashboard_controller.rb +12 -0
  8. data/app/views/admin/dashboard/index.html.erb +1 -0
  9. data/app/views/layouts/puffer.html.erb +41 -23
  10. data/app/views/puffer/_form.html.erb +3 -11
  11. data/app/views/puffer/edit.html.erb +6 -4
  12. data/app/views/puffer/index.html.erb +11 -38
  13. data/app/views/puffer/new.html.erb +5 -2
  14. data/app/views/puffer/show.html.erb +4 -4
  15. data/config/routes.rb +7 -0
  16. data/lib/generators/puffer/controller/controller_generator.rb +1 -1
  17. data/lib/generators/puffer/install/templates/puffer/javascripts/rails-src.js +399 -0
  18. data/lib/generators/puffer/install/templates/puffer/javascripts/rails.js +14 -175
  19. data/lib/generators/puffer/install/templates/puffer/javascripts/right-calendar-src.js +1461 -0
  20. data/lib/generators/puffer/install/templates/puffer/javascripts/right-calendar.js +36 -0
  21. data/lib/generators/puffer/install/templates/puffer/javascripts/right-calendar.js.gz +0 -0
  22. data/lib/generators/puffer/install/templates/puffer/javascripts/right-in-edit-src.js +369 -0
  23. data/lib/generators/puffer/install/templates/puffer/javascripts/right-in-edit.js +13 -0
  24. data/lib/generators/puffer/install/templates/puffer/javascripts/right-in-edit.js.gz +0 -0
  25. data/lib/generators/puffer/install/templates/puffer/javascripts/right-lightbox-src.js +905 -0
  26. data/lib/generators/puffer/install/templates/puffer/javascripts/right-lightbox.js +24 -0
  27. data/lib/generators/puffer/install/templates/puffer/javascripts/right-lightbox.js.gz +0 -0
  28. data/lib/generators/puffer/install/templates/puffer/javascripts/right-sortable-src.js +428 -0
  29. data/lib/generators/puffer/install/templates/puffer/javascripts/right-sortable.js +17 -0
  30. data/lib/generators/puffer/install/templates/puffer/javascripts/right-sortable.js.gz +0 -0
  31. data/lib/generators/puffer/install/templates/puffer/javascripts/right-src.js +5892 -0
  32. data/lib/generators/puffer/install/templates/puffer/javascripts/right-tabs-src.js +1145 -0
  33. data/lib/generators/puffer/install/templates/puffer/javascripts/right-tabs.js +29 -0
  34. data/lib/generators/puffer/install/templates/puffer/javascripts/right-tabs.js.gz +0 -0
  35. data/lib/generators/puffer/install/templates/puffer/javascripts/right.js +95 -0
  36. data/lib/generators/puffer/install/templates/puffer/javascripts/right.js.gz +0 -0
  37. data/lib/generators/puffer/install/templates/puffer/stylesheets/puffer.css +168 -0
  38. data/lib/generators/puffer/install/templates/puffer/stylesheets/reset.css +60 -0
  39. data/lib/puffer.rb +2 -1
  40. data/lib/puffer/base.rb +15 -14
  41. data/lib/puffer/controller/config.rb +52 -16
  42. data/lib/puffer/controller/dsl.rb +27 -29
  43. data/lib/puffer/controller/helpers.rb +54 -9
  44. data/lib/puffer/controller/mutate.rb +4 -22
  45. data/lib/puffer/engine.rb +9 -0
  46. data/lib/puffer/extensions/controller.rb +5 -3
  47. data/lib/puffer/extensions/form.rb +14 -0
  48. data/lib/puffer/extensions/mapper.rb +36 -0
  49. data/lib/puffer/fields.rb +21 -0
  50. data/lib/puffer/fields/field.rb +8 -3
  51. data/lib/puffer/inputs.rb +23 -0
  52. data/lib/puffer/inputs/association.rb +11 -0
  53. data/lib/puffer/inputs/base.rb +39 -0
  54. data/lib/puffer/inputs/boolean.rb +19 -0
  55. data/lib/puffer/inputs/collection_association.rb +11 -0
  56. data/lib/puffer/inputs/date_time.rb +16 -0
  57. data/lib/puffer/inputs/file.rb +11 -0
  58. data/lib/puffer/inputs/password.rb +11 -0
  59. data/lib/puffer/inputs/select.rb +11 -0
  60. data/lib/puffer/inputs/text.rb +11 -0
  61. data/lib/puffer/path_set.rb +17 -0
  62. data/lib/puffer/resource.rb +14 -14
  63. data/puffer.gemspec +75 -8
  64. data/spec/dummy/app/controllers/admin/categories_controller.rb +4 -0
  65. data/spec/dummy/app/controllers/admin/posts_controller.rb +4 -0
  66. data/spec/dummy/app/controllers/admin/profiles_controller.rb +5 -0
  67. data/spec/dummy/app/controllers/admin/users_controller.rb +4 -0
  68. data/spec/dummy/app/models/friendship.rb +4 -0
  69. data/spec/dummy/app/models/profile.rb +2 -0
  70. data/spec/dummy/app/models/user.rb +7 -0
  71. data/spec/dummy/app/views/admin/users/index.html.erb +1 -0
  72. data/spec/dummy/config/puffer.rb +0 -0
  73. data/spec/dummy/db/migrate/20110107082706_create_friendships.rb +15 -0
  74. data/spec/dummy/db/schema.rb +9 -1
  75. data/spec/dummy/public/puffer/javascripts/rails-src.js +399 -0
  76. data/spec/dummy/public/puffer/javascripts/rails.js +14 -0
  77. data/spec/dummy/public/puffer/javascripts/right-calendar-src.js +1461 -0
  78. data/spec/dummy/public/puffer/javascripts/right-calendar.js +36 -0
  79. data/spec/dummy/public/puffer/javascripts/right-calendar.js.gz +0 -0
  80. data/spec/dummy/public/puffer/javascripts/right-in-edit-src.js +369 -0
  81. data/spec/dummy/public/puffer/javascripts/right-in-edit.js +13 -0
  82. data/spec/dummy/public/puffer/javascripts/right-in-edit.js.gz +0 -0
  83. data/spec/dummy/public/puffer/javascripts/right-lightbox-src.js +905 -0
  84. data/spec/dummy/public/puffer/javascripts/right-lightbox.js +24 -0
  85. data/spec/dummy/public/puffer/javascripts/right-lightbox.js.gz +0 -0
  86. data/spec/dummy/public/puffer/javascripts/right-sortable-src.js +428 -0
  87. data/spec/dummy/public/puffer/javascripts/right-sortable.js +17 -0
  88. data/spec/dummy/public/puffer/javascripts/right-sortable.js.gz +0 -0
  89. data/spec/dummy/public/puffer/javascripts/right-src.js +5892 -0
  90. data/spec/dummy/public/puffer/javascripts/right-tabs-src.js +1145 -0
  91. data/spec/dummy/public/puffer/javascripts/right-tabs.js +29 -0
  92. data/spec/dummy/public/puffer/javascripts/right-tabs.js.gz +0 -0
  93. data/spec/dummy/public/puffer/javascripts/right.js +95 -0
  94. data/spec/dummy/public/puffer/javascripts/right.js.gz +0 -0
  95. data/spec/dummy/public/puffer/stylesheets/puffer.css +168 -0
  96. data/spec/dummy/public/puffer/stylesheets/reset.css +60 -0
  97. data/spec/lib/fields_spec.rb +2 -0
  98. data/spec/lib/params_spec.rb +55 -54
  99. metadata +114 -23
  100. data/lib/generators/puffer/install/templates/puffer/javascripts/application.js +0 -2
  101. data/lib/generators/puffer/install/templates/puffer/javascripts/controls.js +0 -965
  102. data/lib/generators/puffer/install/templates/puffer/javascripts/dragdrop.js +0 -974
  103. data/lib/generators/puffer/install/templates/puffer/javascripts/effects.js +0 -1123
  104. data/lib/generators/puffer/install/templates/puffer/javascripts/prototype.js +0 -6001
  105. data/lib/puffer/railtie.rb +0 -5
@@ -4,59 +4,57 @@ module Puffer
4
4
 
5
5
  def self.included base
6
6
  base.class_eval do
7
- class_attribute :puffer_fields
8
- self.puffer_fields = {}
9
-
10
7
  extend ClassMethods
8
+ include ActionMethods
9
+ extend ActionMethods
11
10
 
12
- helper_method :index_fields, :show_fields, :form_fields, :create_fields, :update_fields
13
- end
14
- end
15
-
16
- [:index, :show, :form, :create, :update].each do |sym|
17
- define_method "#{sym}_fields" do
18
- self.class.send "#{sym}_fields"
11
+ %w(index show form create update).each do |action|
12
+ class_attribute "_#{action}_fields"
13
+ send "_#{action}_fields=", Puffer::Fields.new unless send("_#{action}_fields").present?
14
+ helper_method "#{action}_fields"
15
+ end
19
16
  end
20
17
  end
21
18
 
22
19
  module ClassMethods
23
20
 
24
- def configure &block
25
- block.bind(current_config).call
21
+ %w(index show form create update).each do |action|
22
+ define_method action do |&block|
23
+ @_fields = send "_#{action}_fields"
24
+ @_fields.clear
25
+ block.call if block
26
+ @_fields = nil
27
+ end
26
28
  end
27
29
 
28
- [:index, :show, :form, :create, :update].each do |sym|
29
- define_method sym do
30
- @puffer_option = sym
31
- yield if block_given?
32
- end
30
+ def field name, options = {}
31
+ field = @_fields.field(model, name, options) if @_fields
32
+ #generate_association_actions field if field.association?
33
+ #generate_change_actions field if field.toggable?
33
34
  end
34
35
 
36
+ end
37
+
38
+ module ActionMethods
39
+
35
40
  def index_fields
36
- puffer_fields[:index] || Puffer::Fields.new
41
+ _index_fields
37
42
  end
38
43
 
39
44
  def show_fields
40
- puffer_fields[:show] || puffer_fields[:index] || Puffer::Fields.new
45
+ _show_fields.presence || _index_fields
41
46
  end
42
47
 
43
48
  def form_fields
44
- puffer_fields[:form] || Puffer::Fields.new
49
+ _form_fields
45
50
  end
46
51
 
47
52
  def create_fields
48
- puffer_fields[:create] || puffer_fields[:form] || Puffer::Fields.new
53
+ _create_fields.presence || _form_fields
49
54
  end
50
55
 
51
56
  def update_fields
52
- puffer_fields[:update] || puffer_fields[:form] || Puffer::Fields.new
53
- end
54
-
55
- def field name, options = {}
56
- puffer_fields[@puffer_option] ||= Puffer::Fields.new
57
- field = puffer_fields[@puffer_option].field(current_resource.model, name, options)
58
- #generate_association_actions field if field.association?
59
- #generate_change_actions field if field.toggable?
57
+ _update_fields.presence || _form_fields
60
58
  end
61
59
 
62
60
  end
@@ -4,21 +4,66 @@ module Puffer
4
4
 
5
5
  def self.included base
6
6
  base.class_eval do
7
- helper_method :resource_session, :puffer_navigation
7
+ include InstanceMethods
8
+ extend ClassMethods
9
+
10
+ helper_method :resource_session, :puffer_navigation, :sidebar_puffer_navigation, :resource, :record, :records
8
11
  end
9
12
  end
10
13
 
11
- def puffer_navigation
14
+ module ClassMethods
15
+
16
+ def model_name
17
+ @model_name ||= ((puffer? and configuration.model) || controller_name.singularize).to_s
18
+ end
19
+
20
+ def model
21
+ @model ||= model_name.classify.constantize rescue nil
22
+ end
12
23
 
13
24
  end
14
25
 
15
- def resource_session
16
- postfix = params[:action] =~ /associated_/ ? params[:action] : ''
17
- name = "#{current_resource.model_name}#{postfix}".to_sym
18
- session[:resources] ||= {}
19
- session[:resources][name] ||= {}
20
- session[:resources][name][:boolean] ||= {}
21
- session[:resources][name]
26
+ module InstanceMethods
27
+
28
+ def puffer_navigation
29
+ Rails.application.routes.puffer[resource.prefix].values.map(&:first).each do |controller|
30
+ title = controller.configuration.group.to_s.humanize
31
+ path = send("#{resource.prefix}_#{controller.controller_name}_path")
32
+ current = puffer? && resource.root.controller.configuration.group == controller.configuration.group
33
+ yield title, path, current
34
+ end
35
+ end
36
+
37
+ def sidebar_puffer_navigation
38
+ (Rails.application.routes.puffer[resource.prefix][configuration.group] || []).each do |controller|
39
+ title = controller.model.model_name.human
40
+ path = send("#{resource.prefix}_#{controller.controller_name}_path")
41
+ current = controller.controller_name == resource.controller_name
42
+ yield title, path, current
43
+ end if puffer?
44
+ end
45
+
46
+ def resource_session
47
+ postfix = params[:action] =~ /associated_/ ? params[:action] : ''
48
+ name = "#{resource.model_name}#{postfix}".to_sym
49
+ session[:resources] ||= {}
50
+ session[:resources][name] ||= {}
51
+ session[:resources][name][:boolean] ||= {}
52
+ session[:resources][name]
53
+ end
54
+
55
+ def resource
56
+ @resource ||= Puffer::Resource.new params, request
57
+ end
58
+
59
+ def record
60
+ @record || instance_variable_get("@#{resource.model_name}")
61
+ end
62
+
63
+ def records
64
+ @records || instance_variable_get("@#{resource.model_name.pluralize}")
65
+ end
66
+
22
67
  end
23
68
 
24
69
  end
@@ -4,38 +4,20 @@ module Puffer
4
4
 
5
5
  def self.included base
6
6
  base.class_eval do
7
- class_attribute :current_config
8
- self.current_config = Puffer::Controller::Config.new
9
-
10
7
  extend ClassMethods
11
8
 
12
9
  layout 'puffer'
10
+ helper_method :puffer?
13
11
 
14
- helper_method :current_resource, :current_config, :record, :records
15
-
16
- rescue_from ActionView::MissingTemplate do |exception|
17
- render current_resource.template(exception.path.split('/').last)
18
- end
12
+ self.view_paths = Puffer::PathSet.new view_paths
19
13
  end
20
14
  end
21
15
 
22
- def current_resource
23
- @current_resource ||= Puffer::Resource.new params, request
24
- end
25
-
26
- def record
27
- @record || instance_variable_get("@#{current_resource.model_name}")
28
- end
29
-
30
- def records
31
- @records || instance_variable_get("@#{current_resource.model_name.pluralize}")
32
- end
16
+ def puffer?; true; end
33
17
 
34
18
  module ClassMethods
35
19
 
36
- def puffer?
37
- true
38
- end
20
+ def puffer?; true; end
39
21
 
40
22
  end
41
23
 
@@ -0,0 +1,9 @@
1
+ module Puffer
2
+ class Engine < Rails::Engine
3
+ config.autoload_paths << File.join(root, 'lib')
4
+
5
+ initializer 'puffer.add_cells_paths', :after => :add_view_paths do
6
+ Cell::Base.prepend_view_path(Cells::DEFAULT_VIEW_PATHS.map { |path| File.join(root, path) })
7
+ end
8
+ end
9
+ end
@@ -6,14 +6,16 @@ module Puffer
6
6
  def self.included base
7
7
  base.class_eval do
8
8
  extend ClassMethods
9
+
10
+ helper_method :puffer?
9
11
  end
10
12
  end
11
13
 
14
+ def puffer?; false; end
15
+
12
16
  module ClassMethods
13
17
 
14
- def puffer?
15
- false
16
- end
18
+ def puffer?; false; end
17
19
 
18
20
  end
19
21
 
@@ -0,0 +1,14 @@
1
+ module Puffer
2
+ module Extensions
3
+ module FormBuilder
4
+
5
+ def puffer_field field
6
+ input = Puffer::Inputs.map_field field
7
+ input.new(self, @template, field).render
8
+ end
9
+
10
+ end
11
+ end
12
+ end
13
+
14
+ ActionView::Helpers::FormBuilder.send :include, Puffer::Extensions::FormBuilder
@@ -21,6 +21,14 @@ module Puffer
21
21
  puffer_resources(*resources, &block) || original_resources(*resources, &block)
22
22
  end
23
23
 
24
+ def puffer_controller controller
25
+ puffer = Rails.application.routes.puffer
26
+ prefix = @scope[:module]
27
+ puffer[prefix] ||= ActiveSupport::OrderedHash.new
28
+ puffer[prefix][controller.configuration.group] ||= []
29
+ puffer[prefix][controller.configuration.group] << controller
30
+ end
31
+
24
32
  def puffer_resource(*resources, &block)
25
33
  options = resources.extract_options!
26
34
 
@@ -36,6 +44,8 @@ module Puffer
36
44
  @scope[:ancestors] ||= []
37
45
  @scope[:children] ||= []
38
46
 
47
+ puffer_controller controller if @scope[:ancestors] == []
48
+
39
49
  resource_scope(resource) do
40
50
  siblings = @scope[:children].dup
41
51
  @scope[:children] = []
@@ -83,6 +93,8 @@ module Puffer
83
93
  @scope[:ancestors] ||= []
84
94
  @scope[:children] ||= []
85
95
 
96
+ puffer_controller controller if @scope[:ancestors] == []
97
+
86
98
  resource_scope(resource) do
87
99
  siblings = @scope[:children].dup
88
100
  @scope[:children] = []
@@ -119,7 +131,31 @@ module Puffer
119
131
  end
120
132
 
121
133
  end
134
+
135
+ module RouteSet
136
+
137
+ def self.included base
138
+ base.class_eval do
139
+ alias_method :original_clear!, :clear!
140
+ attr_accessor_with_default :puffer, {}
141
+
142
+ include InstanceMethods
143
+ end
144
+ end
145
+
146
+ module InstanceMethods
147
+
148
+ def clear!
149
+ self.puffer = {}
150
+ original_clear!
151
+ end
152
+
153
+ end
154
+
155
+ end
156
+
122
157
  end
123
158
  end
124
159
 
160
+ ActionDispatch::Routing::RouteSet.send :include, Puffer::Extensions::RouteSet
125
161
  ActionDispatch::Routing::Mapper.send :include, Puffer::Extensions::Mapper
data/lib/puffer/fields.rb CHANGED
@@ -1,6 +1,27 @@
1
1
  module Puffer
2
2
  class Fields < Array
3
3
 
4
+ class_attribute :offers
5
+ self.offers = []
6
+
7
+ def self.offer_type &block
8
+ offers.push block if block_given?
9
+ end
10
+
11
+ def self.offered_type field
12
+ swallow_nil{ offers.detect { |offer| offer.call(field) }.call(field) }
13
+ end
14
+
15
+ offer_type do |field|
16
+ :select if field.options.key?(:select)
17
+ end
18
+ offer_type do |field|
19
+ :password if field.name =~ /password/
20
+ end
21
+ offer_type do |field|
22
+ field.model.reflect_on_association(name.to_sym).macro if field.model.reflect_on_association name.to_sym
23
+ end
24
+
4
25
  def field *args
5
26
  push Field.new(*args)
6
27
  end
@@ -19,7 +19,7 @@ module Puffer
19
19
  end
20
20
 
21
21
  def label
22
- @label ||= options[:label] || model.human_attribute_name(name)
22
+ @label ||= resource.human_attribute_name(field)
23
23
  end
24
24
 
25
25
  def order
@@ -27,18 +27,23 @@ module Puffer
27
27
  end
28
28
 
29
29
  def type
30
- @type ||= options[:type] ? options[:type].to_sym : (column ? column.type : :string)
30
+ @type ||= options[:type] ? options[:type].to_sym : (Puffer::Fields.offered_type(self) || (column ? column.type : :string))
31
31
  end
32
32
 
33
33
  def to_s
34
34
  field
35
35
  end
36
36
 
37
+ def input_options
38
+ {}
39
+ end
40
+
37
41
  def model
38
42
  unless @model
39
43
  @model = resource
40
44
  associations = field.split('.')
41
- while @model.reflect_on_association(association = associations.shift.to_sym) do
45
+ associations.pop
46
+ while @model.reflect_on_association(association = swallow_nil{associations.shift.to_sym}) do
42
47
  @model = @model.reflect_on_association(association).klass
43
48
  end
44
49
  end
@@ -0,0 +1,23 @@
1
+ module Puffer
2
+ module Inputs
3
+
4
+ mattr_accessor :mappings
5
+ self.mappings = {}
6
+
7
+ def self.map_type *args
8
+ to = args.extract_options![:to]
9
+ raise ArgumentError, "You need to give :to as option to map_type" unless to
10
+ args.each { |type| mappings[type] = to }
11
+ end
12
+
13
+ map_type :belongs_to, :has_one, :to => Puffer::Inputs::Association
14
+ map_type :has_many, :has_and_belongs_to_many, :to => Puffer::Inputs::CollectionAssociation
15
+ map_type :date, :time, :datetime, :timestamp, :to => Puffer::Inputs::DateTime
16
+
17
+ def self.map_field field
18
+ mappings[field.type] || (const_defined?(field.type.to_s.classify) ?
19
+ "Puffer::Inputs::#{field.type.to_s.classify}".constantize : Puffer::Inputs::Base)
20
+ end
21
+
22
+ end
23
+ end
@@ -0,0 +1,11 @@
1
+ module Puffer
2
+ module Inputs
3
+ class Association < Puffer::Inputs::Base
4
+
5
+ def input
6
+ field.type
7
+ end
8
+
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,39 @@
1
+ module Puffer
2
+ module Inputs
3
+ class Base
4
+
5
+ attr_accessor :builder, :template, :field
6
+
7
+ def initialize builder, template, field
8
+ @builder = builder
9
+ @field = field
10
+ @template = template
11
+ end
12
+
13
+ def render
14
+ template.html_safe
15
+ end
16
+
17
+ def template
18
+ <<-INPUT
19
+ #{label}
20
+ #{error}
21
+ #{input}
22
+ INPUT
23
+ end
24
+
25
+ def label
26
+ builder.label field
27
+ end
28
+
29
+ def input
30
+ builder.text_field field, field.input_options
31
+ end
32
+
33
+ def error
34
+ builder.object.errors[field.name.to_sym].first
35
+ end
36
+
37
+ end
38
+ end
39
+ end