formic 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (67) hide show
  1. data/Gemfile +9 -0
  2. data/Gemfile.lock +40 -0
  3. data/README.rdoc +54 -0
  4. data/Rakefile +35 -0
  5. data/VERSION +1 -0
  6. data/javascripts/jquery-1.6.2.min.js +18 -0
  7. data/javascripts/jquery-ui-1.8.16.custom.min.js +791 -0
  8. data/lib/formic/base.rb +65 -0
  9. data/lib/formic/button.rb +22 -0
  10. data/lib/formic/checkbox.rb +9 -0
  11. data/lib/formic/file.rb +8 -0
  12. data/lib/formic/form.rb +69 -0
  13. data/lib/formic/helper.rb +39 -0
  14. data/lib/formic/hidden.rb +10 -0
  15. data/lib/formic/input.rb +41 -0
  16. data/lib/formic/list.rb +5 -0
  17. data/lib/formic/list_item.rb +13 -0
  18. data/lib/formic/password.rb +10 -0
  19. data/lib/formic/railtie.rb +12 -0
  20. data/lib/formic/selection.rb +5 -0
  21. data/lib/formic/tab.rb +23 -0
  22. data/lib/formic/tab_panel.rb +12 -0
  23. data/lib/formic/textarea.rb +5 -0
  24. data/lib/formic.rb +17 -0
  25. data/spec/formic/base_spec.rb +148 -0
  26. data/spec/formic/button_spec.rb +55 -0
  27. data/spec/formic/checkbox_spec.rb +30 -0
  28. data/spec/formic/file_spec.rb +30 -0
  29. data/spec/formic/form_spec.rb +136 -0
  30. data/spec/formic/helper_spec.rb +60 -0
  31. data/spec/formic/hidden_spec.rb +30 -0
  32. data/spec/formic/input_spec.rb +131 -0
  33. data/spec/formic/list_item_spec.rb +25 -0
  34. data/spec/formic/list_spec.rb +22 -0
  35. data/spec/formic/password_spec.rb +37 -0
  36. data/spec/formic/selection_spec.rb +35 -0
  37. data/spec/formic/tab_panel_spec.rb +26 -0
  38. data/spec/formic/tab_spec.rb +69 -0
  39. data/spec/formic/textarea_spec.rb +35 -0
  40. data/spec/formic_spec_helper.rb +23 -0
  41. data/spec/spec_helper.rb +9 -0
  42. data/stylesheets/jquery-ui/ui-lightness/images/ui-bg_diagonals-thick_18_b81900_40x40.png +0 -0
  43. data/stylesheets/jquery-ui/ui-lightness/images/ui-bg_diagonals-thick_20_666666_40x40.png +0 -0
  44. data/stylesheets/jquery-ui/ui-lightness/images/ui-bg_flat_10_000000_40x100.png +0 -0
  45. data/stylesheets/jquery-ui/ui-lightness/images/ui-bg_glass_100_f6f6f6_1x400.png +0 -0
  46. data/stylesheets/jquery-ui/ui-lightness/images/ui-bg_glass_100_fdf5ce_1x400.png +0 -0
  47. data/stylesheets/jquery-ui/ui-lightness/images/ui-bg_glass_65_ffffff_1x400.png +0 -0
  48. data/stylesheets/jquery-ui/ui-lightness/images/ui-bg_gloss-wave_35_f6a828_500x100.png +0 -0
  49. data/stylesheets/jquery-ui/ui-lightness/images/ui-bg_highlight-soft_100_eeeeee_1x100.png +0 -0
  50. data/stylesheets/jquery-ui/ui-lightness/images/ui-bg_highlight-soft_75_ffe45c_1x100.png +0 -0
  51. data/stylesheets/jquery-ui/ui-lightness/images/ui-icons_222222_256x240.png +0 -0
  52. data/stylesheets/jquery-ui/ui-lightness/images/ui-icons_228ef1_256x240.png +0 -0
  53. data/stylesheets/jquery-ui/ui-lightness/images/ui-icons_ef8c08_256x240.png +0 -0
  54. data/stylesheets/jquery-ui/ui-lightness/images/ui-icons_ffd27a_256x240.png +0 -0
  55. data/stylesheets/jquery-ui/ui-lightness/images/ui-icons_ffffff_256x240.png +0 -0
  56. data/stylesheets/jquery-ui/ui-lightness/jquery-ui-1.8.16.custom.css +568 -0
  57. data/views/formic/default/_button.html.haml +1 -0
  58. data/views/formic/default/_form.html.haml +12 -0
  59. data/views/formic/default/_hidden.html.haml +1 -0
  60. data/views/formic/default/_input.html.haml +4 -0
  61. data/views/formic/default/_list.html.haml +3 -0
  62. data/views/formic/default/_list_item.html.haml +1 -0
  63. data/views/formic/default/_selection.html.haml +13 -0
  64. data/views/formic/default/_tab.html.haml +3 -0
  65. data/views/formic/default/_tab_panel.html.haml +9 -0
  66. data/views/formic/default/_textarea.html.haml +4 -0
  67. metadata +216 -0
@@ -0,0 +1,65 @@
1
+ module Formic
2
+ class Base
3
+ attr_reader :page, :options
4
+ attr_accessor :template, :content
5
+
6
+ def self.default_template given_default_template
7
+ @template = given_default_template
8
+ end
9
+
10
+ def self.template
11
+ @template || self.superclass.template
12
+ end
13
+
14
+ def template
15
+ return @template || self.class.template
16
+ end
17
+
18
+ def initialize page, *args, &block
19
+ @page = page
20
+ @options = {:class => []}
21
+
22
+ _initialize *args, &block
23
+ @options[:class].unshift( self.class.to_s.split('::').last.underscore )
24
+ self.formics.unshift self
25
+ end
26
+
27
+ def to_s
28
+ rendered = self.page.render :partial => self.template, :locals => {:element => self}
29
+ self.formics.shift
30
+ return rendered
31
+ end
32
+
33
+ def method_missing method, *args, &block
34
+ _initialize *args, &block
35
+ self.options[:class].push method.to_s
36
+ return self
37
+ end
38
+
39
+ def _initialize options={}, &block
40
+ self.merge_options options
41
+ @content = block if block_given?
42
+ return self
43
+ end
44
+
45
+ def has_class? classname
46
+ return self.options[:class].include? classname
47
+ end
48
+
49
+ def formics
50
+ @page.instance_variable_set(:@formics, []) unless @page.instance_variable_get(:@formics)
51
+ @page.instance_variable_get(:@formics)
52
+ end
53
+
54
+ def merge_options new_options
55
+ classes = [@options[:class], new_options[:class]].reject {|element| element.nil?}
56
+ classes = classes.flatten.join(' ').split(' ').uniq
57
+ @options.merge!(new_options).merge!(:class => classes)
58
+ end
59
+
60
+ def options
61
+ @options[:class] = @options[:class].uniq
62
+ return @options
63
+ end
64
+ end
65
+ end
@@ -0,0 +1,22 @@
1
+ module Formic
2
+ class Button < Base
3
+ default_template 'formic/default/button'
4
+
5
+ attr_reader :label
6
+ def _initialize label=nil, options={}, &block
7
+ if label.instance_of? Hash
8
+ options = label
9
+ label = nil
10
+ end
11
+
12
+ super options, &block
13
+ @label = label
14
+ end
15
+
16
+ def method_missing method, label=nil, options={}, &block
17
+ super method, label, options{}, &block
18
+ @label ||= method.to_s.underscore.titleize
19
+ return self
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,9 @@
1
+ module Formic
2
+ class Checkbox < Input
3
+ def _initialize name=nil, options={}, &block
4
+ super
5
+ self.options[:type] = 'checkbox'
6
+ return self
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,8 @@
1
+ module Formic
2
+ class File < Input
3
+ def initialize page, name=nil, options={}, &block
4
+ super
5
+ self.options[:type] = 'file'
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,69 @@
1
+ module Formic
2
+ class Form < Base
3
+ default_template 'formic/default/form'
4
+
5
+ attr_reader :model, :action
6
+
7
+ def _initialize options={}, &block
8
+ super options, &block
9
+ self.options[:method] ||= 'POST'
10
+ end
11
+
12
+ def put options={}, &block
13
+ self.options[:method ] = 'POST'
14
+ self.options[:_method] = 'PUT'
15
+ self.content = block if block_given?
16
+ return self
17
+ end
18
+
19
+ def create model, options={}, &block
20
+ options[:title] ||= "New #{model.class.to_s.underscore}".gsub('_', ' ').titleize
21
+ @action = :create
22
+
23
+ return _initialize_with_model(
24
+ model,
25
+ options[:action] || self.page.url_for(model),
26
+ "create_#{model.class.to_s.downcase}_form",
27
+ options,
28
+ &block
29
+ )
30
+ end
31
+
32
+ def edit model, options={}, &block
33
+ self.put
34
+
35
+ @action = :edit
36
+ options[:title] ||= "Edit #{model.class.to_s.underscore}".gsub('_', ' ').titleize
37
+ return _initialize_with_model(
38
+ model,
39
+ options[:action] || self.page.url_for(model),
40
+ "edit_#{model.class.to_s.downcase}_form",
41
+ options,
42
+ &block
43
+ )
44
+ end
45
+
46
+ def method_missing action, model, options={}, &block
47
+ @action = action
48
+ options[:title] ||= "#{action} #{model.class.to_s.underscore}".gsub('_', ' ').titleize
49
+ return _initialize_with_model(
50
+ model,
51
+ options[:action] || "#{self.page.url_for(model)}/#{action}",
52
+ "#{action}_#{model.class.to_s.downcase}_form",
53
+ options,
54
+ &block
55
+ )
56
+ end
57
+
58
+ def _initialize_with_model model, action, classname, options={}, &block
59
+ options[:title] ||= "#{action} #{model.class.to_s.underscore}".gsub('_', ' ').titleize
60
+ @model = model
61
+ self.merge_options(options)
62
+ self.options[:method] ||= 'POST'
63
+ self.options[:action] ||= action
64
+ self.options[:class].push classname
65
+ self.content = block if block_given?
66
+ return self
67
+ end
68
+ end
69
+ end
@@ -0,0 +1,39 @@
1
+ require 'formic'
2
+
3
+ module Formic
4
+ module Helper
5
+ def self.formic_helper *names
6
+ names.each do |name|
7
+ define_method(name) { |*args, &block|
8
+ name = :menu if name == :menu_bar
9
+ klass = get_class(name)
10
+ object = klass.new(self, *args, &block)
11
+ return object
12
+ }
13
+ end
14
+ end
15
+
16
+ formic_helper(
17
+ :selection,
18
+ :input,
19
+ :password,
20
+ :checkbox,
21
+ :list,
22
+ :list_item,
23
+ :tab,
24
+ :form,
25
+ :tab_panel,
26
+ :button,
27
+ :textarea,
28
+ )
29
+
30
+ def get_class symbol
31
+ classname = ''
32
+ elements = symbol.to_s.split('_')
33
+ elements.each do |element|
34
+ classname += element.capitalize
35
+ end
36
+ return "Formic::#{classname}".constantize
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,10 @@
1
+ module Formic
2
+ class Hidden < Input
3
+ default_template 'formic/default/hidden'
4
+ def _initialize name=nil, options={}, &block
5
+ super
6
+ self.options[:type] = 'hidden'
7
+ return self
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,41 @@
1
+ module Formic
2
+ class Input < Base
3
+ default_template 'formic/default/input'
4
+
5
+ attr_reader :field
6
+
7
+ def label
8
+ return self.options[:label] if self.options[:label]
9
+ return @field.underscore.gsub('_', ' ').titleize
10
+ end
11
+
12
+ def _initialize name=nil, options={}, &block
13
+ if name.instance_of? Hash
14
+ options = name
15
+ name = nil
16
+ end
17
+
18
+ super options, &block
19
+ @field = name
20
+
21
+ if !options[:name]
22
+ if @field && self.form && self.form.model
23
+ self.options[:name] = "#{self.form.model.class.to_s.downcase}[#{name}]"
24
+ self.options[:value] ||= self.form.model.send(@field.to_sym)
25
+ elsif
26
+ self.options[:name] = name
27
+ end
28
+ end
29
+
30
+ return self
31
+ end
32
+
33
+ def form
34
+ self.formics.each do |element|
35
+ return element if element.instance_of? Formic::Form
36
+ end
37
+
38
+ return nil
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,5 @@
1
+ module Formic
2
+ class List < Base
3
+ default_template 'formic/default/list'
4
+ end
5
+ end
@@ -0,0 +1,13 @@
1
+ module Formic
2
+ class ListItem < Base
3
+ default_template 'formic/default/list_item'
4
+
5
+ attr_reader :text
6
+
7
+ def _initialize text='', options={}, &block
8
+ super options, &block
9
+ @text = text
10
+ return self
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,10 @@
1
+ module Formic
2
+ class Password < Input
3
+ def _initialize name=nil, options={}, &block
4
+ super
5
+ @options[:value] = ''
6
+ @options[:type] = 'password'
7
+ return self
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,12 @@
1
+ require 'formic/helper'
2
+ module Formic
3
+ class Railtie < Rails::Railtie
4
+ initializer "formic.helper" do
5
+ ActionView::Base.send :include, Formic::Helper
6
+ end
7
+
8
+ initializer "formic.template dir" do
9
+ ActionController::Base.append_view_path "#{::File.expand_path(::File.dirname(__FILE__))}/../../views"
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,5 @@
1
+ module Formic
2
+ class Selection < Input
3
+ default_template 'formic/default/selection'
4
+ end
5
+ end
data/lib/formic/tab.rb ADDED
@@ -0,0 +1,23 @@
1
+ module Formic
2
+ class Tab < Base
3
+ default_template 'formic/default/tab'
4
+
5
+ attr_reader :title
6
+
7
+ def _initialize title='', options={}, &block
8
+ super options, &block
9
+ @title = title
10
+ self.tab_panel.tabs.push self
11
+ self.options[:id] ||= "#{title.downcase.gsub(' ', '_')}_tab"
12
+ return self
13
+ end
14
+
15
+ def tab_panel
16
+ self.formics.each do |element|
17
+ return element if element.instance_of? Formic::TabPanel
18
+ end
19
+
20
+ return nil
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,12 @@
1
+ module Formic
2
+ class TabPanel < Base
3
+ default_template 'formic/default/tab_panel'
4
+ attr_accessor :tabs
5
+
6
+ def _initialize options={}, &block
7
+ super
8
+ @tabs = []
9
+ return self
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,5 @@
1
+ module Formic
2
+ class Textarea < Input
3
+ default_template 'formic/default/textarea'
4
+ end
5
+ end
data/lib/formic.rb ADDED
@@ -0,0 +1,17 @@
1
+ require 'active_support'
2
+ require 'active_support/all'
3
+ require 'formic/base'
4
+ require 'formic/form'
5
+ require 'formic/input'
6
+ require 'formic/file'
7
+ require 'formic/hidden'
8
+ require 'formic/checkbox'
9
+ require 'formic/password'
10
+ require 'formic/textarea'
11
+ require 'formic/selection'
12
+ require 'formic/button'
13
+ require 'formic/tab_panel'
14
+ require 'formic/tab'
15
+ require 'formic/list'
16
+ require 'formic/list_item'
17
+ require 'formic/railtie' if defined?(Rails)
@@ -0,0 +1,148 @@
1
+ require 'spec_helper'
2
+
3
+ describe Formic::Base do
4
+ before(:each) do
5
+ @page = Object.new
6
+ @options = {:key => 'value'}
7
+ @block = Proc.new {}
8
+ end
9
+
10
+ context "#initilize" do
11
+ it "should be initialize with a options and a block" do
12
+ base = Formic::Base.new @page, @options, &@block
13
+ base.options.should == @options.merge(:class => ['base'])
14
+ base.content.should == @block
15
+ end
16
+
17
+ it "should not have content if block is not given" do
18
+ base = Formic::Base.new @options
19
+ base.content.should be_nil
20
+ end
21
+
22
+ it "register form to formics" do
23
+ base = Formic::Base.new @page, @options, &@block
24
+ base.formics.first.should == base
25
+ end
26
+ end
27
+
28
+ context "#default_template" do
29
+ before(:each) do
30
+ class Child < Formic::Base
31
+ default_template 'formic/child'
32
+ end
33
+
34
+ @child = Child.new @page, @options
35
+ end
36
+
37
+ it "child class should be able to define default template" do
38
+ @child.template.should == 'formic/child'
39
+ end
40
+
41
+ it "object can have its own template" do
42
+ @child.template = 'form/child_different'
43
+ @child.template.should == 'form/child_different'
44
+ end
45
+
46
+ it "each child class should be able to define its own template" do
47
+ class AnotherChild < Formic::Base
48
+ default_template 'formic/anotherchild'
49
+ end
50
+
51
+ @child.template.should == 'formic/child'
52
+ another_child = AnotherChild.new @options
53
+ another_child.template.should == 'formic/anotherchild'
54
+ end
55
+
56
+ it "should have a class with the element class name" do
57
+ @child.options[:class].include?('child').should be_true
58
+ end
59
+ end
60
+
61
+ context "#to_s" do
62
+ before(:each) do
63
+ Formic::Base.default_template 'template'
64
+ @base = Formic::Base.new @page, @options, &@block
65
+
66
+ mock(@page).render(
67
+ :partial => 'template',
68
+ :locals => {:element => @base}
69
+ ) {'page_rendered'}
70
+
71
+ @result = @base.to_s
72
+ end
73
+
74
+ it "should render the page with the options" do
75
+ @result.should == 'page_rendered'
76
+ end
77
+
78
+ it "should unregister the element" do
79
+ @base.formics.first.should_not == @base
80
+ end
81
+ end
82
+
83
+ context "#has_class" do
84
+ it "should return true for correct class" do
85
+ base = Formic::Base.new(@page, {:class => 'somestle'})
86
+ base.has_class?('somestle').should be_true
87
+ end
88
+ end
89
+
90
+ context "#merge_options" do
91
+ before(:each) do
92
+ @base = Formic::Base.new @page, @options.merge(:title => 'title'), &@block
93
+ @base.merge_options({:class => 'hello', :title => 'new title'})
94
+ end
95
+
96
+ it "should add class" do
97
+ @base.options[:class].should =~ ['base', 'hello']
98
+ end
99
+
100
+ it "should override other attribute" do
101
+ @base.options[:title].should == 'new title'
102
+ end
103
+ end
104
+
105
+ context "#options" do
106
+ it "should remove duplication in option[:class]" do
107
+ @base = Formic::Base.new @page, :class => 'same same different'
108
+ @base.options[:class].should == ['base', 'same', 'different']
109
+ end
110
+ end
111
+
112
+ context "#method_missing" do
113
+ before(:each) do
114
+ @base = Formic::Base.new(@page, {:class => 'class_in_option'})
115
+ @base.something @options, &@block
116
+ end
117
+
118
+ it "should add the class" do
119
+ @base.should have_class ('something')
120
+ end
121
+
122
+ it "should have options" do
123
+ @base.options[:key].should == 'value'
124
+ end
125
+
126
+ it "should have content" do
127
+ @base.content.should == @block
128
+ end
129
+
130
+ it "should add the class" do
131
+ @base.should have_class ('something')
132
+ end
133
+
134
+ context "order of classes" do
135
+ it "should have default type has first" do
136
+ @base.options[:class].first.should == 'base'
137
+ end
138
+
139
+ it "should have class in option as second priority" do
140
+ @base.options[:class].second.should == 'class_in_option'
141
+ end
142
+
143
+ it "should have class in missing method as the last" do
144
+ @base.options[:class].last.should == 'something'
145
+ end
146
+ end
147
+ end
148
+ end
@@ -0,0 +1,55 @@
1
+ require 'spec_helper'
2
+
3
+ describe Formic::Button do
4
+ before(:each) do
5
+ @model = Object.new
6
+ stub(@page = Object.new).url_for(@model) {'/models/1'}
7
+ stub(@page).render
8
+
9
+ @options = {}
10
+ @block = Proc.new {}
11
+ end
12
+
13
+ context "#initialize" do
14
+ before(:each) do
15
+ @button = Formic::Button.new @page, 'name', @options, &@block
16
+ end
17
+
18
+ it "should have template formic/default/button" do
19
+ @button.template.should == 'formic/default/button'
20
+ end
21
+
22
+ it "should have class button" do
23
+ @button.should have_class 'button'
24
+ end
25
+
26
+ it "should set field value" do
27
+ @button.label.should == 'name'
28
+ end
29
+ end
30
+
31
+ context "#method_missing" do
32
+ before(:each) do
33
+ @button = Formic::Button.new @page
34
+ @result = @button.go_ahead @options, &@block
35
+ end
36
+
37
+ it "should have class as action" do
38
+ @button.options[:class].last.should == 'go_ahead'
39
+ end
40
+
41
+ it "should generate label as class" do
42
+ @button.label.should == 'Go Ahead'
43
+ end
44
+
45
+ it "should return the object" do
46
+ @result.should == @button
47
+ end
48
+
49
+ it "should use label if label is given" do
50
+ button = Formic::Button.new @page
51
+ result = @button.go_ahead 'Button Label', @options, &@block
52
+ result.label.should == 'Button Label'
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,30 @@
1
+ require 'spec_helper'
2
+
3
+ describe Formic::Checkbox do
4
+ before(:each) do
5
+ @model = Object.new
6
+ stub(@page = Object.new).url_for(@model) {'/models/1'}
7
+ stub(@page).render
8
+
9
+ @options = {}
10
+ @block = Proc.new {}
11
+ end
12
+
13
+ context "#checkbox" do
14
+ before(:each) do
15
+ @input = Formic::Checkbox.new @page, 'name', @options, &@block
16
+ end
17
+
18
+ it "should have template formic/default/input" do
19
+ @input.template.should == 'formic/default/input'
20
+ end
21
+
22
+ it "should have type file" do
23
+ @input.options[:type].should == 'checkbox'
24
+ end
25
+
26
+ it "should set field value" do
27
+ @input.field.should == 'name'
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,30 @@
1
+ require 'spec_helper'
2
+
3
+ describe Formic::File do
4
+ before(:each) do
5
+ @model = Object.new
6
+ stub(@page = Object.new).url_for(@model) {'/models/1'}
7
+ stub(@page).render
8
+
9
+ @options = {}
10
+ @block = Proc.new {}
11
+ end
12
+
13
+ context "#file" do
14
+ before(:each) do
15
+ @input = Formic::File.new @page, 'name', @options, &@block
16
+ end
17
+
18
+ it "should have template formic/default/input" do
19
+ @input.template.should == 'formic/default/input'
20
+ end
21
+
22
+ it "should have type file" do
23
+ @input.options[:type].should == 'file'
24
+ end
25
+
26
+ it "should set field value" do
27
+ @input.field.should == 'name'
28
+ end
29
+ end
30
+ end