styx 0.0.4 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,2 @@
1
+ .idea/
2
+ .rspec
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source :rubygems
2
+
3
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,131 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ styx (0.0.4)
5
+
6
+ GEM
7
+ remote: http://rubygems.org/
8
+ specs:
9
+ actionmailer (3.1.3)
10
+ actionpack (= 3.1.3)
11
+ mail (~> 2.3.0)
12
+ actionpack (3.1.3)
13
+ activemodel (= 3.1.3)
14
+ activesupport (= 3.1.3)
15
+ builder (~> 3.0.0)
16
+ erubis (~> 2.7.0)
17
+ i18n (~> 0.6)
18
+ rack (~> 1.3.5)
19
+ rack-cache (~> 1.1)
20
+ rack-mount (~> 0.8.2)
21
+ rack-test (~> 0.6.1)
22
+ sprockets (~> 2.0.3)
23
+ activemodel (3.1.3)
24
+ activesupport (= 3.1.3)
25
+ builder (~> 3.0.0)
26
+ i18n (~> 0.6)
27
+ activerecord (3.1.3)
28
+ activemodel (= 3.1.3)
29
+ activesupport (= 3.1.3)
30
+ arel (~> 2.2.1)
31
+ tzinfo (~> 0.3.29)
32
+ activeresource (3.1.3)
33
+ activemodel (= 3.1.3)
34
+ activesupport (= 3.1.3)
35
+ activesupport (3.1.3)
36
+ multi_json (~> 1.0)
37
+ arel (2.2.1)
38
+ builder (3.0.0)
39
+ capybara (1.1.2)
40
+ mime-types (>= 1.16)
41
+ nokogiri (>= 1.3.3)
42
+ rack (>= 1.0.0)
43
+ rack-test (>= 0.5.4)
44
+ selenium-webdriver (~> 2.0)
45
+ xpath (~> 0.1.4)
46
+ childprocess (0.2.3)
47
+ ffi (~> 1.0.6)
48
+ combustion (0.3.1)
49
+ rails (>= 3.1.0)
50
+ thor (>= 0.14.6)
51
+ diff-lcs (1.1.3)
52
+ erubis (2.7.0)
53
+ ffi (1.0.11)
54
+ hike (1.2.1)
55
+ i18n (0.6.0)
56
+ json (1.6.5)
57
+ mail (2.3.0)
58
+ i18n (>= 0.4.0)
59
+ mime-types (~> 1.16)
60
+ treetop (~> 1.4.8)
61
+ mime-types (1.17.2)
62
+ multi_json (1.0.4)
63
+ nokogiri (1.5.0)
64
+ polyglot (0.3.3)
65
+ rack (1.3.6)
66
+ rack-cache (1.1)
67
+ rack (>= 0.4)
68
+ rack-mount (0.8.3)
69
+ rack (>= 1.0.0)
70
+ rack-ssl (1.3.2)
71
+ rack
72
+ rack-test (0.6.1)
73
+ rack (>= 1.0)
74
+ rails (3.1.3)
75
+ actionmailer (= 3.1.3)
76
+ actionpack (= 3.1.3)
77
+ activerecord (= 3.1.3)
78
+ activeresource (= 3.1.3)
79
+ activesupport (= 3.1.3)
80
+ bundler (~> 1.0)
81
+ railties (= 3.1.3)
82
+ railties (3.1.3)
83
+ actionpack (= 3.1.3)
84
+ activesupport (= 3.1.3)
85
+ rack-ssl (~> 1.3.2)
86
+ rake (>= 0.8.7)
87
+ rdoc (~> 3.4)
88
+ thor (~> 0.14.6)
89
+ rake (0.9.2.2)
90
+ rdoc (3.12)
91
+ json (~> 1.4)
92
+ rspec (2.8.0)
93
+ rspec-core (~> 2.8.0)
94
+ rspec-expectations (~> 2.8.0)
95
+ rspec-mocks (~> 2.8.0)
96
+ rspec-core (2.8.0)
97
+ rspec-expectations (2.8.0)
98
+ diff-lcs (~> 1.1.2)
99
+ rspec-mocks (2.8.0)
100
+ rspec-rails (2.8.1)
101
+ actionpack (>= 3.0)
102
+ activesupport (>= 3.0)
103
+ railties (>= 3.0)
104
+ rspec (~> 2.8.0)
105
+ rubyzip (0.9.5)
106
+ selenium-webdriver (2.15.0)
107
+ childprocess (>= 0.2.1)
108
+ ffi (~> 1.0.9)
109
+ multi_json (~> 1.0.4)
110
+ rubyzip
111
+ sprockets (2.0.3)
112
+ hike (~> 1.2)
113
+ rack (~> 1.0)
114
+ tilt (~> 1.1, != 1.3.0)
115
+ thor (0.14.6)
116
+ tilt (1.3.3)
117
+ treetop (1.4.10)
118
+ polyglot
119
+ polyglot (>= 0.3.1)
120
+ tzinfo (0.3.31)
121
+ xpath (0.1.4)
122
+ nokogiri (~> 1.3)
123
+
124
+ PLATFORMS
125
+ ruby
126
+
127
+ DEPENDENCIES
128
+ capybara (~> 1.1.2)
129
+ combustion (~> 0.3.1)
130
+ rspec-rails (~> 2.8.1)
131
+ styx!
data/README.md CHANGED
@@ -167,6 +167,7 @@ License
167
167
  -------
168
168
 
169
169
  Copyright (C) 2011 by Boris Staal <boris@roundlake.ru>
170
+ Alexander Pavlenko <a.pavlenko@roundlake.ru>
170
171
 
171
172
  Permission is hereby granted, free of charge, to any person obtaining a copy
172
173
  of this software and associated documentation files (the "Software"), to deal
data/Rakefile ADDED
@@ -0,0 +1,7 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ Bundler.require :development
4
+ require 'rake'
5
+ require 'rspec/core/rake_task'
6
+
7
+ task RSpec::Core::RakeTask.new(:spec)
data/lib/styx/forms.rb CHANGED
@@ -1,33 +1,27 @@
1
1
  module Styx
2
- module Forms
3
- def self.included base
4
- base.send(:include, InstanceMethods)
5
- end
6
-
7
- module InstanceMethods
8
- def styx_form_store_and_respond(entity, data=nil, &block)
9
- response.content_type = Mime::TEXT
2
+ module Forms
3
+ def styx_form_store_and_respond(entity, data=nil, &block)
4
+ response.content_type = Mime::TEXT
10
5
 
11
- if entity.save
12
- styx_form_respond_success(data, entity, &block)
13
- else
14
- styx_form_respond_failure(entity.class.name, entity.errors.messages)
15
- end
6
+ if entity.save
7
+ styx_form_respond_success(data, entity, &block)
8
+ else
9
+ styx_form_respond_failure(entity.class.name, entity.errors.messages)
16
10
  end
11
+ end
17
12
 
18
- def styx_form_respond_failure(entity, errors)
19
- response.content_type = Mime::TEXT
13
+ def styx_form_respond_failure(entity, errors)
14
+ response.content_type = Mime::TEXT
20
15
 
21
- errors = Hash[*errors.map {|x| [x, nil]}.flatten] if errors.is_a?(Array)
22
- render :text => {:entity => entity.to_s.underscore.gsub('/', '_'), :messages => errors}.to_json, :status => :unprocessable_entity
23
- end
16
+ errors = Hash[*errors.map {|x| [x, nil]}.flatten] if errors.is_a?(Array)
17
+ render :text => {:entity => entity.to_s.underscore.gsub('/', '_'), :messages => errors}.to_json, :status => :unprocessable_entity
18
+ end
24
19
 
25
- def styx_form_respond_success(data, entity=nil, &block)
26
- response.content_type = Mime::TEXT
20
+ def styx_form_respond_success(data, entity=nil, &block)
21
+ response.content_type = Mime::TEXT
27
22
 
28
- block.call(entity) if block_given?
29
- render :text => (data.is_a?(Proc) ? data.call(entity) : data)
30
- end
23
+ block.call(entity) if block_given?
24
+ render :text => (data.is_a?(Proc) ? data.call(entity) : data)
31
25
  end
32
26
  end
33
27
  end
data/lib/styx/helpers.rb CHANGED
@@ -1,40 +1,21 @@
1
1
  module Styx
2
2
  module Helpers
3
- def self.included base
4
- base.send(:include, InstanceMethods)
5
- base.class_eval do
6
- helper_method :this_page?, :this_namespace?
7
- end
3
+ extend ActiveSupport::Concern
4
+
5
+ included do
6
+ helper_method :this_page?, :this_namespace?
8
7
  end
9
8
 
10
- module InstanceMethods
11
- def this_page?(mask)
12
- mask = [mask] unless mask.is_a?(Array)
13
-
14
- flag = false
15
-
16
- mask.each do |m|
17
- m = m.to_s.split('#')
18
-
19
- c = m[0]
20
- a = m[1]
21
-
22
- f = true
23
-
24
- f = false if !c.blank? && c != controller_name
25
- f = false if !a.blank? && a != action_name
26
-
27
- flag ||= f
28
- end
29
-
30
- flag
9
+ def this_page?(mask)
10
+ [*mask].any? do |m|
11
+ c, a = m.to_s.split('#')
12
+ !(c.present? && c != controller_name || a.present? && a != action_name)
31
13
  end
14
+ end
32
15
 
33
- def this_namespace?(namespace)
34
- current = controller_path.split('/').first
35
-
36
- return namespace == current
37
- end
16
+ def this_namespace?(namespace)
17
+ # TODO: support nested namespaces?
18
+ namespace == controller_path.split('/').first
38
19
  end
39
20
  end
40
21
  end
@@ -1,27 +1,24 @@
1
1
  module Styx
2
2
  module Initializer
3
- def self.included base
4
- base.send(:include, InstanceMethods)
5
- base.class_eval do
6
- helper_method :styx_initialize, :styx_initialize_with
7
- before_filter { @styx_initialize_with = {} }
8
- end
3
+ extend ActiveSupport::Concern
4
+
5
+ included do
6
+ helper_method :styx_initialize, :styx_initialize_with
7
+ before_filter { @styx_initialize_with = {} }
8
+ end
9
+
10
+ def styx_initialize_with(data)
11
+ @styx_initialize_with.merge! data
9
12
  end
10
13
 
11
- module InstanceMethods
12
- def styx_initialize_with(data)
13
- @styx_initialize_with.merge! data
14
- end
15
-
16
- def styx_initialize()
17
- result = render_to_string :partial => 'styx/initializer', :locals => {
18
- :klass => controller_path.gsub('/', '_').camelize,
19
- :method => action_name,
20
- :data => @styx_initialize_with
21
- }
22
-
23
- result.html_safe
24
- end
14
+ def styx_initialize()
15
+ result = render_to_string :partial => 'styx/initializer', :locals => {
16
+ :klass => controller_path.gsub('/', '_').camelize,
17
+ :method => action_name,
18
+ :data => @styx_initialize_with
19
+ }
20
+
21
+ result.html_safe
25
22
  end
26
23
  end
27
24
  end
@@ -0,0 +1,11 @@
1
+ require 'spec_helper'
2
+
3
+ describe TestsController do
4
+ render_views
5
+
6
+ it 'index' do
7
+ get :index
8
+ response.body.should have_content('Styx.Initializers.Tests.initialize({"data":"test"})')
9
+ response.body.should have_content('Styx.Initializers.Tests.index({"data":"test"})')
10
+ end
11
+ end
@@ -0,0 +1,13 @@
1
+ require 'styx'
2
+
3
+ class TestsController < ActionController::Base
4
+
5
+ append_view_path Styx::Engine.paths['app/views']
6
+
7
+ include Styx::Initializer
8
+ include Styx::Forms
9
+
10
+ def index
11
+ styx_initialize_with :data => 'test'
12
+ end
13
+ end
@@ -0,0 +1 @@
1
+ <%= styx_initialize %>
@@ -0,0 +1,3 @@
1
+ Rails.application.routes.draw do
2
+ resources :tests
3
+ end
File without changes
@@ -0,0 +1 @@
1
+ *.log
@@ -0,0 +1,69 @@
1
+ require 'spec_helper'
2
+
3
+ describe Styx::Forms do
4
+
5
+ let :controller do
6
+ controller = double('controller')
7
+ controller.class_eval do
8
+ include Styx::Forms
9
+ end
10
+ controller.stub(:response => double('response'))
11
+ controller.response.should_receive(:content_type=).with(Mime::TEXT).at_least(:once)
12
+
13
+ controller
14
+ end
15
+
16
+ context '#styx_form_store_and_respond' do
17
+
18
+ it 'respond success' do
19
+ entity = double('model')
20
+ entity.stub(:save => true)
21
+ controller.should_receive(:styx_form_respond_success).with(data = Object.new, entity)
22
+ controller.styx_form_store_and_respond(entity, data) do |e|
23
+ e.should === entity
24
+ # whatever
25
+ end
26
+ end
27
+
28
+ it 'respond failure' do
29
+ entity = double('model')
30
+ entity.stub(:save => false)
31
+ entity.stub(:errors => double('errors'))
32
+ entity.errors.stub(:messages => {:field => ["can't this'", "can't that'"]})
33
+ controller.should_receive(:styx_form_respond_failure).with(entity.class.name, entity.errors.messages)
34
+ controller.styx_form_store_and_respond(entity)
35
+ end
36
+ end
37
+
38
+ it 'respond success' do
39
+ entity = Object.new
40
+ [
41
+ 'data',
42
+ lambda {|e| e.should === entity; 'data' }
43
+ ].each do |data|
44
+ controller.should_receive(:render).with(:text => 'data')
45
+ controller.styx_form_respond_success(data, entity) do |e|
46
+ e.should === entity
47
+ # whatever
48
+ end
49
+ end
50
+ end
51
+
52
+ it 'respond failure' do
53
+
54
+ errors_cases = [
55
+ {:field => ["can't this'", "can't that'"]},
56
+ ['field1', 'field2']
57
+ ]
58
+
59
+ def normalize(errors)
60
+ errors.is_a?(Array) && Hash[*errors.map {|x| [x, nil]}.flatten] || errors
61
+ end
62
+
63
+ errors_cases.each do |errors|
64
+ json = {:entity => 'm_cl_name', :messages => normalize(errors)}.to_json
65
+ controller.should_receive(:render).with(:text => json, :status => :unprocessable_entity)
66
+ controller.styx_form_respond_failure('M::ClName', errors)
67
+ end
68
+ end
69
+ end
@@ -0,0 +1,64 @@
1
+ require 'spec_helper'
2
+
3
+ describe Styx::Helpers do
4
+
5
+ let :controller do
6
+ controller = double('controller')
7
+ controller.class.should_receive(:helper_method).with(:this_page?, :this_namespace?)
8
+ controller.class_eval do
9
+ include Styx::Helpers
10
+ end
11
+
12
+ controller
13
+ end
14
+
15
+ it '#this_page?' do
16
+ controller.stub(:controller_name => 'tests')
17
+ controller.stub(:action_name => 'index')
18
+
19
+ cases = [
20
+ [
21
+ be_true, [
22
+ 'tests#index',
23
+ '#index'
24
+ ]
25
+ ], [
26
+ be_false, [
27
+ 'fails#index',
28
+ 'tests#show',
29
+ '#show'
30
+ ]
31
+ ]
32
+ ]
33
+
34
+ cases.each do |group|
35
+ be_ok, examples = group
36
+ examples.each do |example|
37
+ controller.this_page?(example).should be_ok
38
+ end
39
+ end
40
+ end
41
+
42
+ it '#this_namespace?' do
43
+ controller.stub(:controller_path => 'module/tests')
44
+
45
+ cases = [
46
+ [
47
+ be_true, [
48
+ 'module'
49
+ ]
50
+ ], [
51
+ be_false, [
52
+ 'whatever'
53
+ ]
54
+ ]
55
+ ]
56
+
57
+ cases.each do |group|
58
+ be_ok, examples = group
59
+ examples.each do |example|
60
+ controller.this_namespace?(example).should be_ok
61
+ end
62
+ end
63
+ end
64
+ end
@@ -0,0 +1,32 @@
1
+ require 'spec_helper'
2
+
3
+ describe Styx::Initializer do
4
+
5
+ let :controller do
6
+ controller = double('controller')
7
+ controller.class.should_receive(:helper_method).with(:styx_initialize, :styx_initialize_with)
8
+ controller.class.stub(:before_filter) do |block|
9
+ controller.instance_eval(&block)
10
+ end
11
+ controller.class_eval do
12
+ include Styx::Initializer
13
+ end
14
+ controller.instance_variable_get('@styx_initialize_with').should == {}
15
+
16
+ controller
17
+ end
18
+
19
+ it '#styx_initialize_with' do
20
+ controller.styx_initialize_with(:test => 'data')
21
+ controller.instance_variable_get('@styx_initialize_with').should == {:test => 'data'}
22
+ end
23
+
24
+ it '#styx_initialize' do
25
+ controller.stub(:controller_path => 'module/tests')
26
+ controller.stub(:action_name => 'index')
27
+ controller.should_receive(:render_to_string).and_return('rendered template')
28
+ result = controller.styx_initialize
29
+ result.should == 'rendered template'
30
+ result.html_safe?.should be_true
31
+ end
32
+ end
@@ -0,0 +1,12 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ Bundler.require :development
4
+ require 'capybara/rspec'
5
+ Combustion.initialize! :action_controller, :action_view, :sprockets
6
+ Bundler.require :default
7
+ require 'rspec/rails'
8
+ require 'capybara/rails'
9
+
10
+ RSpec.configure do |config|
11
+ config.mock_with :rspec
12
+ end
data/styx.gemspec CHANGED
@@ -1,14 +1,18 @@
1
- SPEC = Gem::Specification.new do |s|
1
+ Gem::Specification.new do |s|
2
2
  s.name = "styx"
3
- s.version = "0.0.4"
3
+ s.version = "0.1.0"
4
4
  s.platform = Gem::Platform::RUBY
5
5
  s.summary = "Set of helpers to maintain bridge between Server (Rails) side and Client (JS) side"
6
6
  s.email = "boris@roundlake.ru"
7
7
  s.homepage = "http://github.com/roundlake/styx"
8
8
  s.description = s.summary
9
- s.authors = ['Boris Staal']
9
+ s.authors = ['Boris Staal', 'Alexander Pavlenko']
10
10
 
11
11
  s.has_rdoc = false # disable rdoc generation until we've got more
12
12
  s.files = `git ls-files`.split("\n")
13
13
  s.require_paths = ["lib"]
14
+
15
+ s.add_development_dependency 'combustion', '~> 0.3.1'
16
+ s.add_development_dependency 'rspec-rails', '~> 2.8.1'
17
+ s.add_development_dependency 'capybara', '~> 1.1.2'
14
18
  end
metadata CHANGED
@@ -1,16 +1,50 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: styx
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
8
8
  - Boris Staal
9
+ - Alexander Pavlenko
9
10
  autorequire:
10
11
  bindir: bin
11
12
  cert_chain: []
12
- date: 2012-01-10 00:00:00.000000000 Z
13
- dependencies: []
13
+ date: 2012-01-18 00:00:00.000000000 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: combustion
17
+ requirement: &70186037571000 !ruby/object:Gem::Requirement
18
+ none: false
19
+ requirements:
20
+ - - ~>
21
+ - !ruby/object:Gem::Version
22
+ version: 0.3.1
23
+ type: :development
24
+ prerelease: false
25
+ version_requirements: *70186037571000
26
+ - !ruby/object:Gem::Dependency
27
+ name: rspec-rails
28
+ requirement: &70186037570300 !ruby/object:Gem::Requirement
29
+ none: false
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: 2.8.1
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: *70186037570300
37
+ - !ruby/object:Gem::Dependency
38
+ name: capybara
39
+ requirement: &70186037569600 !ruby/object:Gem::Requirement
40
+ none: false
41
+ requirements:
42
+ - - ~>
43
+ - !ruby/object:Gem::Version
44
+ version: 1.1.2
45
+ type: :development
46
+ prerelease: false
47
+ version_requirements: *70186037569600
14
48
  description: Set of helpers to maintain bridge between Server (Rails) side and Client
15
49
  (JS) side
16
50
  email: boris@roundlake.ru
@@ -18,7 +52,11 @@ executables: []
18
52
  extensions: []
19
53
  extra_rdoc_files: []
20
54
  files:
55
+ - .gitignore
56
+ - Gemfile
57
+ - Gemfile.lock
21
58
  - README.md
59
+ - Rakefile
22
60
  - app/assets/javascripts/styx.forms.js.coffee
23
61
  - app/assets/javascripts/styx.js.coffee
24
62
  - app/views/styx/_initializer.html.erb
@@ -28,6 +66,16 @@ files:
28
66
  - lib/styx/forms.rb
29
67
  - lib/styx/helpers.rb
30
68
  - lib/styx/initializer.rb
69
+ - spec/controllers/tests_controller_spec.rb
70
+ - spec/internal/app/controllers/tests_controller.rb
71
+ - spec/internal/app/views/tests/index.html.erb
72
+ - spec/internal/config/routes.rb
73
+ - spec/internal/db/schema.rb
74
+ - spec/internal/log/.gitignore
75
+ - spec/lib/styx_forms_spec.rb
76
+ - spec/lib/styx_helpers_spec.rb
77
+ - spec/lib/styx_initializer_spec.rb
78
+ - spec/spec_helper.rb
31
79
  - styx.gemspec
32
80
  homepage: http://github.com/roundlake/styx
33
81
  licenses: []