toy-locomotive 0.0.4 → 0.0.6

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.
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- toy-locomotive (0.0.3)
4
+ toy-locomotive (0.0.4)
5
5
  rails
6
6
 
7
7
  GEM
@@ -0,0 +1,59 @@
1
+ module ToyLocomotive::Attributes
2
+ class AttributeChain
3
+
4
+ attr_accessor :column, :parent, :_as, :_helper
5
+
6
+ def initialize column, parent
7
+ @column = column
8
+ @parent = parent
9
+ @_as = :string
10
+ end
11
+
12
+ def as value
13
+ @_as = value
14
+ @parent.send @_as, @column if [:has_many, :has_and_belongs_to_many, :has_one, :belongs_to].include? @_as
15
+ self
16
+ end
17
+
18
+ def presence bool
19
+ parent.send :validates_presence_of, column
20
+ self
21
+ end
22
+
23
+ def uniqueness value
24
+ return parent.send :validates_uniqueness_of, column if value == true
25
+ parent.send :validates_uniqueness_of, column, {scope: value}
26
+ self
27
+ end
28
+
29
+ def helper value
30
+ @_helper = value
31
+ self
32
+ end
33
+
34
+ def to_table_column
35
+ return nil if skip_table_column?
36
+ return :"#{@column}_id" if @_as == :belongs_to
37
+ @column
38
+ end
39
+
40
+ def to_table_type
41
+ return nil if skip_table_column?
42
+ return :integer if @_as == :belongs_to
43
+ @_as
44
+ end
45
+
46
+ def skip_table_column?
47
+ [:has_many, :has_and_belongs_to_many, :has_one].include? @_as
48
+ end
49
+
50
+ def to_helper
51
+ return @_helper if @_helper
52
+ return :text_area if @_as == :text
53
+ return :hidden_field if @column == :id || @column.to_s[-3..-1] == '_id'
54
+ return :check_box if @column == :id || @_as == :boolean
55
+ :text_field
56
+ end
57
+
58
+ end
59
+ end
@@ -0,0 +1,19 @@
1
+ module ToyLocomotive::Attributes::Model
2
+ module ClassMethods
3
+
4
+ mattr_accessor :toy_attributes
5
+ self.toy_attributes = []
6
+
7
+ def attribute value
8
+ tattr = ToyLocomotive::Attributes::AttributeChain.new value, self
9
+ self.toy_attributes << tattr
10
+ tattr
11
+ end
12
+
13
+ def attributes
14
+ toy_attributes.select{|a| a.parent == self}
15
+ end
16
+
17
+ end
18
+ end
19
+ ActiveRecord::Base.extend ToyLocomotive::Attributes::Model::ClassMethods
@@ -0,0 +1,41 @@
1
+ module ToyLocomotive
2
+ class AttributeObserver
3
+
4
+ attr_accessor :attribute
5
+
6
+ def initialize model
7
+ @model = model
8
+ unless ActiveRecord::Base.connection.table_exists? @model.table_name
9
+ Class.new(ActiveRecord::Migration).create_table(@model.table_name.to_sym) do |t|
10
+ t.timestamps
11
+ end
12
+ end
13
+ @model.attributes.each do |attribute|
14
+ set_attribute(attribute)
15
+ @model.attr_accessible attribute.column
16
+ end
17
+ @model.reset_column_information
18
+ end
19
+
20
+ def set_attribute attribute
21
+ return if attribute.skip_table_column?
22
+ add_attribute attribute
23
+ update_attribute attribute
24
+ end
25
+
26
+ def add_attribute attribute
27
+ unless @model.column_names.include? attribute.to_table_column.to_s
28
+ Class.new(ActiveRecord::Migration).add_column @model.table_name.to_sym, attribute.to_table_column, attribute.to_table_type
29
+ end
30
+ end
31
+
32
+ def update_attribute attribute
33
+ column = @model.columns.select{|c| c.name == attribute.to_table_column.to_s}.first
34
+ if column && column.type != attribute.to_table_type
35
+ Class.new(ActiveRecord::Migration).change_column @model.table_name, attribute.to_table_column, attribute.to_table_type
36
+ end
37
+ end
38
+
39
+ end
40
+
41
+ end
@@ -1,14 +1,17 @@
1
1
  module ToyLocomotive
2
2
  class Engine < Rails::Engine
3
- initializer 'toy_locomotive.initialize', :after=> :disable_dependency_loading do |app|
4
- Dir["#{Rails.root}/app/models/*.rb"].each {|file| require file}
5
- Dir["#{Rails.root}/app/controllers/*.rb"].each do |file|
6
- require file
7
- const = file.split('/').last.split('.').first.classify.constantize.append_filters!
8
- end
9
- Rails.application.class.routes.draw do
10
- ToyLocomotive.routes.each {|route| match route[:path] => "#{route[:controller]}##{route[:action]}", as: route[:as], via: route[:method]}
11
- end
3
+ initializer 'toy_locomotive.initialize', :after=> :disable_dependency_loading do |app|
4
+ Dir["#{Rails.root}/app/models/*.rb"].each do |file|
5
+ require file
6
+ AttributeObserver.new file.split('/').last.split('.').first.classify.constantize
12
7
  end
8
+ Dir["#{Rails.root}/app/controllers/*.rb"].each do |file|
9
+ require file
10
+ const = file.split('/').last.split('.').first.classify.constantize.append_filters!
11
+ end
12
+ Rails.application.class.routes.draw do
13
+ ToyLocomotive.routes.each {|route| match route[:path] => "#{route[:controller]}##{route[:action]}", as: route[:as], via: route[:method]}
14
+ end
15
+ end
13
16
  end
14
17
  end
@@ -45,7 +45,7 @@ module ToyLocomotive::Resources::Controller
45
45
  post 'create' do
46
46
  vars = extract_parent_vars
47
47
  parent = instance_variable_get (model = self.class.extract_model).belongs_chain.reverse.pop.to_member_var
48
- member = (parent ? parent.send(model.to_s.underscore.pluralize) : model).new(params[model.to_params])
48
+ member = (parent ? parent.send(model.to_s.underscore.pluralize) : model).new(params[model.to_s.underscore.to_sym])
49
49
  instance_variable_set model.to_member_var, member
50
50
  vars = vars << member
51
51
  return redirect_to vars, notice: 'Burrito was successfully created.' if member.save
@@ -32,7 +32,7 @@ module ToyLocomotive::Router::Controller
32
32
 
33
33
  def extract_path path, opts={}
34
34
  return path if path[0] == '/'
35
- return "#{extract_model.route_chain}#{extract_model.to_route}/#{path.parameterize}" if opts[:on] == 'member' || ['edit','new'].include?(path)
35
+ return "#{extract_model.route_chain}#{extract_model.to_route}/#{path.parameterize}" if opts[:on] == 'member' || path == 'edit'
36
36
  return "#{extract_model.route_chain}#{extract_model.to_route}" if ['show','update','destroy'].include?(path)
37
37
  return "#{extract_model.route_chain}/#{extract_model.to_s.underscore.pluralize}" if ['create', 'index'].include?(path)
38
38
  "#{extract_model.route_chain}/#{extract_model.to_s.underscore.pluralize}/#{path.parameterize}"
@@ -94,7 +94,7 @@ module ToyLocomotive::Router::Controller
94
94
  def extract_member_var
95
95
  parent = (model = self.class.extract_model).belongs_chain.reverse.pop
96
96
  parent = parent ? instance_variable_get(parent.to_member_var) : nil
97
- instance_variable_set(model.to_collection_var, (parent ? parent.send(model.to_s.underscore.pluralize) : model).find(params[model.to_params]))
97
+ instance_variable_set(model.to_member_var, (parent ? parent.send(model.to_s.underscore.pluralize) : model).find(params[model.to_params]))
98
98
  end
99
99
 
100
100
  def extract_collection_var
@@ -1,3 +1,3 @@
1
1
  module ToyLocomotive
2
- VERSION = "0.0.4"
2
+ VERSION = "0.0.6"
3
3
  end
@@ -5,6 +5,7 @@ module ToyLocomotive
5
5
 
6
6
  module Router; end
7
7
  module Resources; end
8
+ module Attributes; end
8
9
  mattr_accessor :routes
9
10
 
10
11
  end
@@ -12,4 +13,7 @@ end
12
13
  require "toy-locomotive/router/model"
13
14
  require "toy-locomotive/router/controller"
14
15
  require "toy-locomotive/resources/controller"
16
+ require "toy-locomotive/attributes/chain"
17
+ require "toy-locomotive/attributes/model"
18
+ require "toy-locomotive/attributes/observer"
15
19
  require "toy-locomotive/initializer"
@@ -0,0 +1,40 @@
1
+ require 'spec_helper'
2
+
3
+ describe ToyLocomotive::Attributes::AttributeChain do
4
+
5
+ subject{ ToyLocomotive::Attributes::AttributeChain.new :title, Dog }
6
+
7
+ its(:parent){should == Dog}
8
+ its(:column){should == :title}
9
+
10
+ it "defaults as string" do
11
+ subject._as.should == :string
12
+ end
13
+
14
+ it "changes the type" do
15
+ subject.as(:text)._as == :text
16
+ end
17
+
18
+ it "use text_field as helper for strings as default" do
19
+ subject.as(:string).to_helper.should == :text_field
20
+ end
21
+
22
+ it "use text_area as helper for text as default" do
23
+ subject.as(:text).to_helper.should == :text_area
24
+ end
25
+
26
+ it "use hidden_field as helper for ids as default" do
27
+ attribute = ToyLocomotive::Attributes::AttributeChain.new :id, Dog
28
+ attribute.to_helper.should == :hidden_field
29
+ end
30
+
31
+ it "use hidden_field as helper for relational ids as default" do
32
+ attribute = ToyLocomotive::Attributes::AttributeChain.new :human_id, Dog
33
+ attribute.to_helper.should == :hidden_field
34
+ end
35
+
36
+ it "acepts new helper" do
37
+ subject.helper(:text_area).to_helper.should == :text_area
38
+ end
39
+
40
+ end
@@ -0,0 +1,10 @@
1
+ require 'spec_helper'
2
+
3
+ describe Dog do
4
+
5
+ it "acepts attributes" do
6
+ Dog.attribute(:title)
7
+ Dog.toy_attributes.first.column.should == :title
8
+ end
9
+
10
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: toy-locomotive
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.6
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-05-16 00:00:00.000000000 Z
12
+ date: 2012-06-08 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rails
16
- requirement: &26892444 !ruby/object:Gem::Requirement
16
+ requirement: &26057412 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *26892444
24
+ version_requirements: *26057412
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: rspec-rails
27
- requirement: &26891724 !ruby/object:Gem::Requirement
27
+ requirement: &26056980 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: '0'
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *26891724
35
+ version_requirements: *26056980
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: sqlite3
38
- requirement: &26890392 !ruby/object:Gem::Requirement
38
+ requirement: &26055636 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: '0'
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *26890392
46
+ version_requirements: *26055636
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: shoulda
49
- requirement: &26889996 !ruby/object:Gem::Requirement
49
+ requirement: &26054808 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ! '>='
@@ -54,7 +54,7 @@ dependencies:
54
54
  version: '0'
55
55
  type: :development
56
56
  prerelease: false
57
- version_requirements: *26889996
57
+ version_requirements: *26054808
58
58
  description: a Different aproach to Rails applications
59
59
  email:
60
60
  - mortaro@towsta.com
@@ -67,12 +67,17 @@ files:
67
67
  - Gemfile.lock
68
68
  - Rakefile
69
69
  - lib/toy-locomotive.rb
70
+ - lib/toy-locomotive/attributes/chain.rb
71
+ - lib/toy-locomotive/attributes/model.rb
72
+ - lib/toy-locomotive/attributes/observer.rb
70
73
  - lib/toy-locomotive/initializer.rb
71
74
  - lib/toy-locomotive/resources/controller.rb
72
75
  - lib/toy-locomotive/router/controller.rb
73
76
  - lib/toy-locomotive/router/model.rb
74
77
  - lib/toy-locomotive/version.rb
75
78
  - readme
79
+ - spec/lib/attribute_chain_spec.rb
80
+ - spec/lib/attribute_model_spec.rb
76
81
  - spec/lib/resources_controller_spec.rb
77
82
  - spec/lib/router_controller_spec.rb
78
83
  - spec/lib/router_model_spec.rb