destroy_all_view_assigns 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (42) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +9 -0
  3. data/.rspec +2 -0
  4. data/.ruby-gemset +1 -0
  5. data/.ruby-version +1 -0
  6. data/.travis.yml +6 -0
  7. data/CODE_OF_CONDUCT.md +13 -0
  8. data/Gemfile +4 -0
  9. data/LICENSE.txt +21 -0
  10. data/README.md +39 -0
  11. data/Rakefile +15 -0
  12. data/bin/console +14 -0
  13. data/bin/setup +7 -0
  14. data/destroy_all_view_assigns.gemspec +28 -0
  15. data/lib/destroy_all_view_assigns.rb +17 -0
  16. data/lib/destroy_all_view_assigns/abstract_controller_rendering.rb +23 -0
  17. data/lib/destroy_all_view_assigns/configuration.rb +49 -0
  18. data/lib/destroy_all_view_assigns/controller_object.rb +7 -0
  19. data/lib/destroy_all_view_assigns/dashboard.rb +55 -0
  20. data/lib/destroy_all_view_assigns/data_collector.rb +71 -0
  21. data/lib/destroy_all_view_assigns/data_presenter.rb +23 -0
  22. data/lib/destroy_all_view_assigns/railtie.rb +9 -0
  23. data/lib/destroy_all_view_assigns/static/css/bootstrap-theme.css +476 -0
  24. data/lib/destroy_all_view_assigns/static/css/bootstrap-theme.css.map +1 -0
  25. data/lib/destroy_all_view_assigns/static/css/bootstrap-theme.min.css +5 -0
  26. data/lib/destroy_all_view_assigns/static/css/bootstrap.css +6584 -0
  27. data/lib/destroy_all_view_assigns/static/css/bootstrap.css.map +1 -0
  28. data/lib/destroy_all_view_assigns/static/css/bootstrap.min.css +5 -0
  29. data/lib/destroy_all_view_assigns/static/css/destroy.css +39 -0
  30. data/lib/destroy_all_view_assigns/static/fonts/glyphicons-halflings-regular.eot +0 -0
  31. data/lib/destroy_all_view_assigns/static/fonts/glyphicons-halflings-regular.svg +288 -0
  32. data/lib/destroy_all_view_assigns/static/fonts/glyphicons-halflings-regular.ttf +0 -0
  33. data/lib/destroy_all_view_assigns/static/fonts/glyphicons-halflings-regular.woff +0 -0
  34. data/lib/destroy_all_view_assigns/static/fonts/glyphicons-halflings-regular.woff2 +0 -0
  35. data/lib/destroy_all_view_assigns/static/js/highcharts.js +324 -0
  36. data/lib/destroy_all_view_assigns/static/js/jquery.min.js +2 -0
  37. data/lib/destroy_all_view_assigns/version.rb +3 -0
  38. data/lib/destroy_all_view_assigns/views/controller.erb +82 -0
  39. data/lib/destroy_all_view_assigns/views/controllers.erb +90 -0
  40. data/lib/destroy_all_view_assigns/views/index.erb +92 -0
  41. data/lib/destroy_all_view_assigns/views/layout.erb +52 -0
  42. metadata +156 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: f41cd6d1dab1010b81ec228199e7b8c88c70931b
4
+ data.tar.gz: 6905fe071b9d817b042beb6ed31412fbd4368c7c
5
+ SHA512:
6
+ metadata.gz: 397e5862469ef06862fd03987f66c54e0024ff52f3039122b68c403b998b6252f6d0c9523e18b9e2e67a18b02ac64934ca9be53b4ceb42505bc4e0aa2301f20f
7
+ data.tar.gz: b63cb7d80cbe722440e2110fdb136d5b015531feb4a6339cedb224098e078c50f3a60adfb0b58d4c855032e7d26f67b9f67d9dfa916c4111f797c3d4c0bd3fc2
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.ruby-gemset ADDED
@@ -0,0 +1 @@
1
+ destroy
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ ruby-2.2.2
data/.travis.yml ADDED
@@ -0,0 +1,6 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.0
4
+ - 2.1.0
5
+ - 2.0.0
6
+ - 1.9.3
@@ -0,0 +1,13 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
4
+
5
+ We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, age, or religion.
6
+
7
+ Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
8
+
9
+ Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
10
+
11
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
12
+
13
+ This Code of Conduct is adapted from the [Contributor Covenant](http:contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in destroy_all_view_assigns.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Anton Shemerey
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,39 @@
1
+ # DestroyAllViewAssigns
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/destroy_all_view_assigns`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'destroy_all_view_assigns'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install destroy_all_view_assigns
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release` to create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ 1. Fork it ( https://github.com/[my-github-username]/destroy_all_view_assigns/fork )
36
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
37
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
38
+ 4. Push to the branch (`git push origin my-new-feature`)
39
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,15 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new
5
+
6
+ task :default => :spec
7
+ task :test => :spec
8
+
9
+
10
+ task :console do
11
+ require 'pry'
12
+ require 'destroy_all_view_assigns'
13
+ ARGV.clear
14
+ Pry.start
15
+ end
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "destroy_all_view_assigns"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,28 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'destroy_all_view_assigns/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "destroy_all_view_assigns"
8
+ spec.version = DestroyAllViewAssigns::VERSION
9
+ spec.authors = ["Anton Shemerey"]
10
+ spec.email = ["shemerey@gmail.com"]
11
+
12
+ spec.summary = %q{This gem will force you to avoid instance instance variables in you Rails controllers}
13
+ spec.description = %q{Some times instance variables can be bug producers when you exidently change data somewhere in the middle.}
14
+ spec.homepage = "https://github.com/shemerey/destroy_all_view_assigns"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = "exe"
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_dependency "sinatra"
23
+
24
+ spec.add_development_dependency "bundler"
25
+ spec.add_development_dependency "rake"
26
+ spec.add_development_dependency "rspec"
27
+ spec.add_development_dependency "pry"
28
+ end
@@ -0,0 +1,17 @@
1
+ require 'pstore'
2
+ require "destroy_all_view_assigns/version"
3
+ require "destroy_all_view_assigns/configuration"
4
+ require "destroy_all_view_assigns/data_presenter"
5
+ require "destroy_all_view_assigns/data_collector"
6
+ require "destroy_all_view_assigns/abstract_controller_rendering"
7
+ require "destroy_all_view_assigns/controller_object"
8
+
9
+ module DestroyAllViewAssigns
10
+ CollectedData = PStore.new("assigns.pstore")
11
+ extend Configuration
12
+ end
13
+
14
+ if defined? Rails
15
+ require "destroy_all_view_assigns/railtie"
16
+ require "destroy_all_view_assigns/dashboard"
17
+ end
@@ -0,0 +1,23 @@
1
+ module DestroyAllViewAssigns
2
+ module AbstractControllerRendering
3
+ def self.included(klass)
4
+ klass.class_eval do
5
+ alias_method :_old_view_assigns, :view_assigns
6
+
7
+ # replace
8
+ define_method :view_assigns do
9
+ hash = _old_view_assigns
10
+
11
+ DataCollector.new(self).tap do |obj|
12
+ hash.each do |variable_name, _|
13
+ obj.add(variable_name)
14
+ end
15
+ end.collect!
16
+
17
+ hash
18
+ end
19
+
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,49 @@
1
+ module DestroyAllViewAssigns
2
+ module Configuration
3
+ extend self
4
+
5
+ Error = Class.new(StandardError)
6
+
7
+ # Start a DestroyAllViewAssigns configuration block in an initializer.
8
+ #
9
+ # example: Provide a default currency for the application
10
+ # DestroyAllViewAssigns.configure do |config|
11
+ # config.default_currency = :eur
12
+ # end
13
+ def configure
14
+ yield self
15
+ prevent_black_and_white_list_at_the_same_time!
16
+ end
17
+
18
+ # apply default configuration
19
+ def self.extended(base)
20
+ base.configure do |config|
21
+ config.black_list = []
22
+ config.white_list = []
23
+ end
24
+ end
25
+
26
+ def white_list=(list_of_controllers)
27
+ @_white_list = list_of_controllers.to_set
28
+ end
29
+
30
+ def black_list=(list_of_controllers)
31
+ @_black_list = list_of_controllers.to_set
32
+ end
33
+
34
+ def white_list
35
+ @_white_list
36
+ end
37
+
38
+ def black_list
39
+ @_black_list
40
+ end
41
+
42
+ private
43
+
44
+ def prevent_black_and_white_list_at_the_same_time!
45
+ raise Error if [black_list, white_list].all? { |list| not list.empty? }
46
+ end
47
+ end
48
+ end
49
+
@@ -0,0 +1,7 @@
1
+ module DestroyAllViewAssigns
2
+ class ControllerObject
3
+ def initialize(controller)
4
+ @controller = controller
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,55 @@
1
+ require 'sinatra/base'
2
+ require_relative "abstract_controller_rendering"
3
+
4
+ module DestroyAllViewAssigns
5
+ class Dashboard < Sinatra::Base
6
+
7
+ helpers do
8
+ def active_tab?(url)
9
+ url == request.path_info
10
+ end
11
+
12
+ def controller_url(controller)
13
+ # "<a href=''>#{controller[:name]}</a>"
14
+ "<a href='#{url("/controller/" + underscore(controller[:klass]))}'>#{controller[:klass]}</a>"
15
+ end
16
+
17
+ def underscore(word)
18
+ word = word.dup
19
+ word.gsub!(/::/, '@')
20
+ word.gsub!(/([A-Z]+)([A-Z][a-z])/,'\1_\2')
21
+ word.gsub!(/([a-z\d])([A-Z])/,'\1_\2')
22
+ word.tr!("-", "_")
23
+ word.downcase!
24
+ word
25
+ end
26
+
27
+ def camelize(term)
28
+ string = term.to_s
29
+ string.split('@').map do |str|
30
+ str = str.sub(/^[a-z\d]*/) { $&.capitalize }
31
+ str.gsub!(/(?:_|(\/))([a-z\d]*)/i) { "#{$1}#{$2.capitalize}" }
32
+ str.gsub!(/@/, '::')
33
+ str
34
+ end.join('::')
35
+ end
36
+ end
37
+
38
+ set :public_folder, File.dirname(__FILE__) + '/static'
39
+
40
+ get "/" do
41
+ erb :index
42
+ end
43
+
44
+ get "/controller/:name" do
45
+ controller = DestroyAllViewAssigns::DataPresenter.new.find_controller_by_name(camelize(params[:name]))
46
+ erb :controller, locals: {controller: controller}
47
+ end
48
+
49
+ get "/controllers" do
50
+ erb :controllers
51
+ end
52
+
53
+ run! if __FILE__ == $PROGRAM_NAME
54
+ end
55
+ end
@@ -0,0 +1,71 @@
1
+ module DestroyAllViewAssigns
2
+ class DataCollector
3
+ attr_reader :controller
4
+
5
+ def initialize(controller)
6
+ @controller = controller
7
+ end
8
+
9
+ def add(variable_name)
10
+ action_data[:vars] << variable_name
11
+ controller_data[:vars] << variable_name
12
+ end
13
+
14
+ def collect!
15
+ controller_data[:actions][action_name] = action_data
16
+
17
+ CollectedData.transaction do
18
+ CollectedData[klass] = controller_data
19
+ end
20
+ end
21
+
22
+ private
23
+
24
+ def get_controller_data
25
+ CollectedData.transaction do
26
+ CollectedData[klass]
27
+ end
28
+ end
29
+
30
+ def default_controller_data
31
+ {
32
+ controller_name: controller_name,
33
+ klass: klass,
34
+ vars: Set.new,
35
+ actions: {}
36
+ }
37
+ end
38
+
39
+ def controller_data
40
+ @controller_data ||= (get_controller_data || default_controller_data)
41
+ end
42
+
43
+ def get_action_data
44
+ controller_data[:actions][action_name]
45
+ end
46
+
47
+ def default_action_data
48
+ {
49
+ name: action_name,
50
+ vars: Set.new
51
+ }
52
+ end
53
+
54
+ def action_data
55
+ @action_data ||= (get_action_data || default_action_data)
56
+ end
57
+
58
+ def klass
59
+ controller.class.to_s
60
+ end
61
+
62
+ def controller_name
63
+ controller.controller_name
64
+ end
65
+
66
+ def action_name
67
+ controller.action_name
68
+ end
69
+ end
70
+ end
71
+
@@ -0,0 +1,23 @@
1
+ module DestroyAllViewAssigns
2
+ class DataPresenter
3
+ attr_reader :data
4
+
5
+ def initialize(data = CollectedData)
6
+ @data = data
7
+ end
8
+
9
+ def find_controller_by_name(name)
10
+ data.transaction do
11
+ data.fetch(name) { raise Sinatra::NotFound }
12
+ end
13
+ end
14
+
15
+ def controllers
16
+ data.transaction do
17
+ data.roots.map do |controller|
18
+ data[controller]
19
+ end.sort_by { |c| -c[:vars].count }
20
+ end
21
+ end
22
+ end
23
+ end