inline_forms 0.7.2 → 0.7.3

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.7.2
1
+ 0.7.3
@@ -1,5 +1,5 @@
1
1
  module InlineFormsHelper
2
- InlineForms::SPECIAL_MIGRATION_TYPES[:associated]=:references
2
+ InlineForms::SPECIAL_COLUMN_TYPES[:associated]=:references
3
3
  # associated
4
4
  def associated_show(object, attribute, values)
5
5
  #show a list of records
@@ -1,5 +1,5 @@
1
1
  module InlineFormsHelper
2
- InlineForms::SPECIAL_MIGRATION_TYPES[:check_box]=:boolean
2
+ InlineForms::SPECIAL_COLUMN_TYPES[:check_box]=:boolean
3
3
  # boolean, bit unaptly named check_box
4
4
  def check_box_show(object, attribute, values)
5
5
  values ||= { 'false' => 'no', 'true' => 'yes' }
@@ -1,5 +1,5 @@
1
1
  module InlineFormsHelper
2
- InlineForms::SPECIAL_MIGRATION_TYPES[:checklist]=:references
2
+ InlineForms::SPECIAL_COLUMN_TYPES[:checklist]=:references
3
3
  # checklist
4
4
  def checklist_show(object, attribute, values)
5
5
  out = '<ul class="checklist">'
@@ -1,5 +1,5 @@
1
1
  module InlineFormsHelper
2
- InlineForms::SPECIAL_MIGRATION_TYPES[:date_select]=:date
2
+ InlineForms::SPECIAL_COLUMN_TYPES[:date_select]=:date
3
3
 
4
4
  # date
5
5
  def date_select_show(object, attribute, values)
@@ -1,5 +1,5 @@
1
1
  module InlineFormsHelper
2
- InlineForms::SPECIAL_MIGRATION_TYPES[:dropdown]=:integer
2
+ InlineForms::SPECIAL_COLUMN_TYPES[:dropdown]=:belongs_to
3
3
  # dropdown
4
4
  def dropdown_show(object, attribute, values)
5
5
  attribute_value = object.send(attribute)._presentation rescue nil
@@ -1,5 +1,5 @@
1
1
  module InlineFormsHelper
2
- InlineForms::SPECIAL_MIGRATION_TYPES[:dropdown_with_integers]=:integer
2
+ InlineForms::SPECIAL_COLUMN_TYPES[:dropdown_with_integers]=:integer
3
3
  # dropdown_with_integers generates a dropdown menu
4
4
  # with the given list of integers as options
5
5
  #
@@ -1,5 +1,5 @@
1
1
  module InlineFormsHelper
2
- InlineForms::SPECIAL_MIGRATION_TYPES[:dropdown_with_values]=:integer
2
+ InlineForms::SPECIAL_COLUMN_TYPES[:dropdown_with_values]=:integer
3
3
  # dropdown_with_values
4
4
  def dropdown_with_values_show(object, attribute, values)
5
5
  unless values.is_a?(Hash)
@@ -1,5 +1,5 @@
1
1
  module InlineFormsHelper
2
- InlineForms::SPECIAL_MIGRATION_TYPES[:geo_code_curacao]=:string
2
+ InlineForms::SPECIAL_COLUMN_TYPES[:geo_code_curacao]=:string
3
3
  # geo_code_curacao
4
4
  def geo_code_curacao_show(object, attribute, values)
5
5
  attribute_value = object.send(attribute)._presentation rescue nil
@@ -1,5 +1,5 @@
1
1
  module InlineFormsHelper
2
- InlineForms::SPECIAL_MIGRATION_TYPES[:image]=:image
2
+ InlineForms::SPECIAL_COLUMN_TYPES[:image]=:image
3
3
  # image via paperclip
4
4
  def image_show(object, attribute, values)
5
5
  link_to_inline_image_edit object, attribute
@@ -1,5 +1,5 @@
1
1
  module InlineFormsHelper
2
- InlineForms::SPECIAL_MIGRATION_TYPES[:range]=:integer
2
+ InlineForms::SPECIAL_COLUMN_TYPES[:range]=:integer
3
3
  # range
4
4
  def range_show(object, attribute, values)
5
5
  link_to_inline_edit object, attribute, object.send(attribute), nil
@@ -1,5 +1,5 @@
1
1
  module InlineFormsHelper
2
- InlineForms::SPECIAL_MIGRATION_TYPES[:text_area]=:text
2
+ InlineForms::SPECIAL_COLUMN_TYPES[:text_area]=:text
3
3
  # text_area
4
4
  def text_area_show(object, attribute, values)
5
5
  link_to_inline_edit object, attribute, object.send(attribute), nil
@@ -1,5 +1,5 @@
1
1
  module InlineFormsHelper
2
- InlineForms::SPECIAL_MIGRATION_TYPES[:text_field]=:string
2
+ InlineForms::SPECIAL_COLUMN_TYPES[:text_field]=:string
3
3
  # text
4
4
  def text_field_show(object, attribute, values)
5
5
  link_to_inline_edit object, attribute, object.send(attribute), nil
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{inline_forms}
8
- s.version = "0.7.2"
8
+ s.version = "0.7.3"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Ace Suares"]
12
- s.date = %q{2011-03-07}
12
+ s.date = %q{2011-03-08}
13
13
  s.description = %q{Inline Forms aims to ease the setup of forms that provide inline editing. The field list can be specified in the model.}
14
14
  s.email = %q{ace@suares.an}
15
15
  s.extra_rdoc_files = [
@@ -1,44 +1,59 @@
1
1
  module InlineForms
2
+ # == Usage
3
+ # This generator generates a migration, a model and a controller.
4
+ #
5
+ # rails g inline_forms Model attribute:type [attribute:type ...] [options]
6
+ #
7
+ # Read more about the possible types below.
8
+ #
9
+ # = Overriding Rails::Generators::GeneratedAttribute
10
+ # When using a generator in the form
11
+ # rails g example_generator Modelname attribute:type attribute:type ...
12
+ # an array with attributes and types is created for use in the generator.
13
+ #
14
+ # Rails::Generators::GeneratedAttribute creates, among others, a field_type.
15
+ # This field_type maps column types to form field helpers like text_field.
16
+ # We override it here to make our own.
17
+ #
2
18
  class InlineFormsGenerator < Rails::Generators::NamedBase
3
-
4
- Rails::Generators::GeneratedAttribute.class_eval do
5
- # attributes are in the form name:type (f.i. price:integer)
6
- # we want to extend the types to our list of types
7
- # but some of the old types are still needed
8
- # f.i. price:integer turns into a text_field
19
+ Rails::Generators::GeneratedAttribute.class_eval do #:doc:
20
+ # Deducts the column_type for migrations from the type.
21
+ #
22
+ # We first merge the Special Column Types with the Default Column Types,
23
+ # which has the effect that the Default Column Types with the same key override
24
+ # the Special Column Types.
25
+ #
26
+ # If the type is not in the merged hash, then column_type defaults to :unknown
9
27
  #
10
- def migration_type
11
- # convert any type into a migration type
12
- # each helper adds to this list
13
- # be aware that the standard list overwrites the customized list if we merge like this!
14
- SPECIAL_MIGRATION_TYPES.merge(DEFAULT_MIGRATION_TYPES).merge(RELATION_TYPES).merge(SPECIAL_RELATION_TYPES)[type] || :unknown
28
+ # You are advised to check you migrations for the :unknown, because either you made a
29
+ # typo in the generator command line or you need to add a Form Element!
30
+ #
31
+ def column_type
32
+ SPECIAL_COLUMN_TYPES.merge(DEFAULT_COLUMN_TYPES).merge(RELATIONS)[type] || :unknown
15
33
  end
16
34
 
35
+ # Override the field_type to include our special column types.
36
+ #
37
+ # If a type is not in the Special Column Type hash, then the default
38
+ # column type hash is used, and if that fails, the field_type
39
+ # will be :unknown. Make sure to check your models for the :unknown.
40
+ #
17
41
  def field_type
18
- # convert standard types to one of ours
19
- SPECIAL_MIGRATION_TYPES.merge(RELATION_TYPES).merge(SPECIAL_RELATION_TYPES).has_key?(type) ? type : DEFAULT_FIELD_TYPES[type] || :unknown
42
+ SPECIAL_COLUMN_TYPES.merge(RELATIONS).has_key?(type) ? type : DEFAULT_COLUMN_TYPES[type] || :unknown
20
43
  end
21
-
22
- def migration_name
23
- # convert some to _id
24
- ( relation? || field_type == :dropdown) ? name + '_id' : name
44
+
45
+ def relation?
46
+ RELATIONS.has_key?(type) || special_relation?
25
47
  end
26
48
 
27
- def belongs_to?
28
- relation? || field_type == :dropdown
49
+ def special_relation?
50
+ SPECIAL_RELATIONS.has_key?(type)
29
51
  end
30
-
52
+
31
53
  def has_many?
32
54
  field_type == :associated
33
55
  end
34
56
 
35
- def relation?
36
- RELATION_TYPES.has_key?(field_type)
37
- end
38
-
39
- def special_relation?
40
- SPECIAL_RELATION_TYPES.has_key?(field_type)
41
- end
42
57
 
43
58
  end
44
59
  argument :attributes, :type => :array, :banner => "[name:form_element]..."
@@ -50,6 +65,48 @@ module InlineForms
50
65
  end
51
66
 
52
67
  def generate_model
68
+ @belongs_to = "\n"
69
+ @has_many = "\n"
70
+ @has_attached_files = "\n"
71
+ @presentation = "\n"
72
+ @inline_forms_field_list = String.new
73
+
74
+ for attribute in attributes
75
+ if attribute.column_type == :belongs_to || attribute.type == :belongs_to
76
+ @belongs_to << ' belongs_to :' + attribute.name + "\n"
77
+ end
78
+ if attribute.type == :has_many
79
+ @has_many << ' has_many :' + attribute.name + "\n"
80
+ end
81
+ if attribute.type == :image
82
+ @has_attached_files << " has_attached_file :#{attribute.name},
83
+ :styles => { :medium => \"300x300>\", :thumb => \"100x100>\" }\n"
84
+ end
85
+ if attribute.name == '_presentation'
86
+ @presentation << " def _presentation\n" +
87
+ " \"#{attribute.type.to_s}\"\n" +
88
+ " end\n" +
89
+ "\n"
90
+ end
91
+ unless attribute.name == '_presentation' || attribute.relation?
92
+ attribute.field_type == :unknown ? commenter = '#' : commenter = ' '
93
+ @inline_forms_field_list << commenter +
94
+ ' [ :' +
95
+ attribute.name +
96
+ ', "' + attribute.name +
97
+ '", :' + attribute.field_type.to_s +
98
+ " ], \n"
99
+ end
100
+ end
101
+ unless @inline_forms_field_list.empty?
102
+ @inline_forms_field_list = "\n" +
103
+ " def inline_forms_field_list\n" +
104
+ " [\n" +
105
+ @inline_forms_field_list +
106
+ " ]\n" +
107
+ " end\n" +
108
+ "\n"
109
+ end
53
110
  template "model.erb", "app/models/#{model_file_name}.rb"
54
111
  end
55
112
 
@@ -62,6 +119,26 @@ module InlineForms
62
119
  end
63
120
 
64
121
  def generate_migration
122
+ @columns = String.new
123
+
124
+ for attribute in attributes
125
+ if attribute.column_type == :image
126
+ @columns << ' t.string :' + attribute.name + "_file_name\n"
127
+ @columns << ' t.string :' + attribute.name + "_content_type\n"
128
+ @columns << ' t.integer :' + attribute.name + "_file_size\n"
129
+ @columns << ' t.datetime :' + attribute.name + "_updated_at\n"
130
+ else
131
+ unless attribute.name == '_presentation' || attribute.special_relation?
132
+ attribute.field_type == :unknown ? commenter = '#' : commenter = ' '
133
+ @columns << commenter +
134
+ ' t.' +
135
+ attribute.column_type.to_s +
136
+ " :" +
137
+ attribute.name +
138
+ " \n"
139
+ end
140
+ end
141
+ end
65
142
  template "migration.erb", "db/migrate/#{time_stamp}_inline_forms_create_#{table_name}.rb"
66
143
  end
67
144
 
@@ -2,17 +2,7 @@ class InlineFormsCreate<%= table_name.camelize %> < ActiveRecord::Migration
2
2
 
3
3
  def self.up
4
4
  create_table :<%= table_name %> do |t|
5
- <% for attribute in attributes -%>
6
- <% if attribute.migration_type == :image -%>
7
- <%= ' t.string :' + attribute.migration_name + "_file_name" %>
8
- <%= ' t.string :' + attribute.migration_name + "_content_type" %>
9
- <%= ' t.integer :' + attribute.migration_name + "_file_size" %>
10
- <%= ' t.datetime :' + attribute.migration_name + "_updated_at" %>
11
- <% else -%>
12
- <%= ( attribute.migration_type == :unknown || attribute.special_relation? ) ? '#' : ' ' -%>
13
- <%= ' t.' + attribute.migration_type.to_s %> :<%= attribute.migration_name %>
14
- <% end -%>
15
- <% end -%>
5
+ <%= @columns -%>
16
6
  t.timestamps
17
7
  end
18
8
  end
@@ -1,27 +1,7 @@
1
1
  class <%= name %> < ActiveRecord::Base
2
-
3
- <% for attribute in attributes -%>
4
- <%= ' belongs_to :' + attribute.name + "\n" if attribute.belongs_to? -%>
5
- <% end -%>
6
- <% for attribute in attributes -%>
7
- <%= ' has_many :' + attribute.name + "\n" if attribute.has_many? -%>
8
- <% end -%>
9
- <% for attribute in attributes -%>
10
- <%= " has_attached_file :#{attribute.name}, :styles => { :medium => \"300x300>\", :thumb => \"100x100>\" }\n" if attribute.type == :image -%>
11
- <% end -%>
12
-
13
- def _presentation
14
- <% for attribute in attributes -%>
15
- <%= ' "' + ( attribute.type.to_s || 'put your presentation here' ) + '"' + "\n" if attribute.name == '_presentation' -%>
16
- <% end -%>
17
- end
18
-
19
- def inline_forms_field_list
20
- [
21
- <% for attribute in attributes -%>
22
- <%= attribute.relation? ? '#' : ' ' -%>
23
- <%= ' [ :' + attribute.name + ', "' + attribute.name + '", :' + attribute.field_type.to_s + " ], \n" unless attribute.name == '_presentation' -%>
24
- <% end -%>
25
- ]
26
- end
2
+ <%= @belongs_to if @belongs_to.length > 1 -%>
3
+ <%= @has_many if @belongs_to.length > 1 -%>
4
+ <%= @has_attached_files if @belongs_to.length > 1 -%>
5
+ <%= @presentation if @belongs_to.length > 1 -%>
6
+ <%= @inline_forms_field_list -%>
27
7
  end
@@ -1,8 +1,29 @@
1
1
  puts 'loading inline_forms...'
2
2
 
3
3
  module InlineForms
4
- # convert type to migration_type
5
- DEFAULT_MIGRATION_TYPES = {
4
+ # ActiveRecord::Migration comes with a set of column types.
5
+ # They are listed here so they can be uses alongside our Special Column Types.
6
+ #
7
+ # These types will override the Special Column Types, so don't declare
8
+ # types with these names as Special Column Types!
9
+ #
10
+ # Example:
11
+ # rails g inline_forms Example name:string price:integer
12
+ # will result in:
13
+ # class InlineFormsCreateExamples < ActiveRecord::Migration
14
+ # def self.up
15
+ # create_table :examples do |t|
16
+ # t.string :name
17
+ # t.integer :price
18
+ # t.timestamps
19
+ # end
20
+ # end
21
+ # def self.down
22
+ # drop_table :examples
23
+ # end
24
+ # end
25
+ #
26
+ DEFAULT_COLUMN_TYPES = {
6
27
  :string => :string,
7
28
  :text => :text,
8
29
  :integer => :integer,
@@ -14,9 +35,26 @@ module InlineForms
14
35
  :date => :date,
15
36
  :binary => :binary,
16
37
  :boolean => :boolean,
38
+ # :references => :belongs_to,
39
+ # :belongs_to => :belongs_to,
17
40
  }
18
- # convert type to field_type
19
- DEFAULT_FIELD_TYPES = {
41
+ # For each Default Column Type, we need to specify a Form Element for use in form creation.
42
+ #
43
+ # Example:
44
+ # rails g inline_forms Example name:string price:integer
45
+ # will result in the following model:
46
+ #
47
+ # class Example < ActiveRecord::Base
48
+ # def inline_forms_field_list
49
+ # [
50
+ # [ :name, "name", :text_field ],
51
+ # [ :price, "price", :text_field ],
52
+ # ]
53
+ # end
54
+ # end
55
+ # as you see, both :string and :integer are mapped to a :text_field
56
+ #
57
+ DEFAULT_FORM_ELEMENTS = {
20
58
  :string => :text_field,
21
59
  :text => :text_area,
22
60
  :integer => :text_field,
@@ -28,22 +66,44 @@ module InlineForms
28
66
  :date => :date_select,
29
67
  :binary => :text_field,
30
68
  :boolean => :check_box,
31
-
32
69
  }
33
- # define this so the helpers can add to it
34
- SPECIAL_MIGRATION_TYPES = {}
70
+ # This Hash will be used to map our Special Column Types to
71
+ # ActiveRecord::Migration Column Types.
72
+ #
73
+ # The helpers in app/helpers/form_elements add to this Hash.
74
+ #
75
+ # Usage example: in app/helpers/form_elements/dropdown.rb
76
+ # InlineForms::SPECIAL_COLUMN_TYPES[:dropdown]=:belongs_to
77
+ # this maps the :dropdown form element to the :belongs_to column type.
78
+ #
79
+ # If you call the generator with country:dropdown, it will add
80
+ # t.belongs_to :country
81
+ # to the migration. (In fact AR will add t.integer :country_id). And
82
+ # it will add
83
+ # [ :country, "country", :dropdown ],
84
+ # to the model.
85
+ #
86
+ SPECIAL_COLUMN_TYPES = {}
35
87
  # experimental
36
- RELATION_TYPES = {
37
- :belongs_to => :integer,
88
+ RELATIONS = {
89
+ :belongs_to => :belongs_to,
90
+ :references => :belongs_to,
38
91
  }
39
- SPECIAL_RELATION_TYPES = {
92
+ # experimental
93
+ SPECIAL_RELATIONS = {
94
+ :has_many => :has_many,
40
95
  :associated => :associated,
41
96
  }
42
-
97
+ # has_and_belongs_to_many :clients
98
+ # def <=>(other)
99
+ # self.name <=> other.name
100
+ # end
101
+
102
+ # Declare as a Rails::Engine, see http://www.ruby-forum.com/topic/211017#927932
43
103
  class InlineFormsEngine < Rails::Engine
44
104
  initializer 'inline_forms.helper' do |app|
45
105
  ActionView::Base.send :include, InlineFormsHelper
46
106
  end
47
107
  end
48
108
  end
49
- # http://www.ruby-forum.com/topic/211017#927932
109
+
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: inline_forms
3
3
  version: !ruby/object:Gem::Version
4
- hash: 7
4
+ hash: 5
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 7
9
- - 2
10
- version: 0.7.2
9
+ - 3
10
+ version: 0.7.3
11
11
  platform: ruby
12
12
  authors:
13
13
  - Ace Suares
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-03-07 00:00:00 -04:00
18
+ date: 2011-03-08 00:00:00 -04:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency