rails_hacks 0.0.2

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/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in rails_hacks.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 shivashankar
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,29 @@
1
+ # RailsHacks
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'rails_hacks'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install rails_hacks
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
@@ -0,0 +1,10 @@
1
+ class Exception
2
+
3
+ # Example:
4
+ # rescue MO::Handler::PreconditionError => e
5
+ # render :xml => e.to_xml, :status => :precondition_failed
6
+ # end
7
+ def to_xml
8
+ Builder::XmlMarkup.new.exception(message, :type => self.class.to_s.underscore)
9
+ end
10
+ end
@@ -0,0 +1,61 @@
1
+ # expects to be included by ActiveRecord::Base
2
+ module ExposeToApi
3
+
4
+ # Basically, this method sets up some defaults for ActiveRecord calls to .to_xml.
5
+ # used to only show certain fields to
6
+ # be sure to also use attr_accessible
7
+ #
8
+ # == Arguments:
9
+ #
10
+ # arguments to expose_to_api are a list of attributes to pass to to_xmls :only option, and an optional hash of options
11
+ #
12
+ # == Options:
13
+ # for:: this allows you to setup a scope, see examples below
14
+ # skip_id:: normally, expose_to_api automatically adds the :id attribute for you, this allows you to disable that feature
15
+ # all other options are passed on to to_xml
16
+ #
17
+ # == Example:
18
+ # # with these in your model
19
+ # expose_to_api :name, :username
20
+ # expose_to_api :name, :for => :feed, :skip_id => true
21
+ #
22
+ # instance.to_xml
23
+ # # is equivalent to
24
+ # instance.to_xml :only => [ :id, :name, :username ]
25
+ # # and
26
+ # instance.to_xml :for => :feed
27
+ # # is equivalent to
28
+ # instance.to_xml :only => [ :name ]
29
+ def expose_to_api(*args)
30
+ options = args.extract_options!
31
+ args |= [:id] unless options.delete(:skip_id)
32
+ options.merge!(:only => args)
33
+
34
+ if options[:for]
35
+ option_for = options.delete(:for)
36
+ class_variable_set "@@expose_to_api_for_#{option_for}", options
37
+ class_eval <<-EOS_FOR, __FILE__, __LINE__
38
+ def to_xml_with_expose_to_api_for_#{option_for}(options = {})
39
+ if options[:for] and options[:for].to_sym == :#{option_for}
40
+ method = respond_to?(:to_xml_without_expose_to_api) ? :to_xml_without_expose_to_api : :to_xml
41
+ send method, options.merge(@@expose_to_api_for_#{option_for})
42
+ else
43
+ to_xml_without_expose_to_api_for_#{option_for}(options)
44
+ end
45
+ end
46
+ alias_method_chain :to_xml, :expose_to_api_for_#{option_for}
47
+ EOS_FOR
48
+ else
49
+ class_variable_set "@@expose_to_api", options
50
+ class_eval <<-EOS, __FILE__, __LINE__
51
+ def to_xml_with_expose_to_api(options = {})
52
+ to_xml_without_expose_to_api(options.merge(@@expose_to_api))
53
+ end
54
+ alias_method_chain :to_xml, :expose_to_api
55
+ EOS
56
+ end
57
+ end
58
+
59
+ end
60
+
61
+ ActiveRecord::Base.send :extend, ExposeToApi
@@ -0,0 +1,42 @@
1
+
2
+ module ActionView
3
+ module Helpers
4
+ module TagHelper
5
+ def tag_with_type_class(name, options = nil, open = false, escape = true)
6
+ if options
7
+ options = options.with_indifferent_access
8
+ options.merge_dom_class! options[:type] if name.to_sym == :input and options[:type]
9
+ end
10
+
11
+ tag_without_type_class name, options, open, escape
12
+ end
13
+ alias_method_chain :tag, :type_class
14
+ end
15
+
16
+ # I don't know why these don't work with tag_with_type_class
17
+ # because to_input_field_tag calls tag, but for some reason
18
+ # tag_with_type_class does not get called
19
+ class InstanceTag
20
+
21
+ def to_input_field_tag_with_type_class(field_type, options = {})
22
+ options.merge_dom_class! field_type
23
+ to_input_field_tag_without_type_class field_type, options
24
+ end
25
+ alias_method_chain :to_input_field_tag, :type_class
26
+
27
+ def to_radio_button_tag_with_type_class(tag_value, options = {})
28
+ options.merge_dom_class! 'radio'
29
+ to_radio_button_tag_without_type_class tag_value, options
30
+ end
31
+ alias_method_chain :to_radio_button_tag, :type_class
32
+
33
+ def to_check_box_tag_with_type_class(options = {}, checked_value = "1", unchecked_value = "0")
34
+ options.merge_dom_class! 'checkbox'
35
+ to_check_box_tag_without_type_class options, checked_value, unchecked_value
36
+ end
37
+
38
+ alias_method_chain :to_check_box_tag, :type_class
39
+
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,20 @@
1
+ # allows you to merge the :class option without overwriting what classes were passed
2
+ #
3
+ # Example:
4
+ # def menu_item(text, options = {})
5
+ # options.merge_dom_class!('will-be-merged')
6
+ # content_tag :li, text, options
7
+ # end
8
+ #
9
+ # menu_item 'foo', :class => 'bar' # => <li class="bar will-be-merged">foo</li>
10
+ #
11
+ # NOTE: merge_dom_class! does not check for duplicates. try this: hash[:class] = hash.merge_dom_class!('foo bar')[:class].split.uniq.join ' '
12
+ module MergeDomClass
13
+ def merge_dom_class!(dom_class)
14
+ self.merge! :class => (self[:class] ? self[:class] + ' ' : '') + dom_class.to_s
15
+ end
16
+ end
17
+
18
+ Hash.send :include, MergeDomClass
19
+ HashWithIndifferentAccess.send :include, MergeDomClass
20
+
@@ -0,0 +1,15 @@
1
+ require 'uri'
2
+
3
+ # class ActionController::AbstractRequest
4
+ # def referer_host
5
+ # begin
6
+ # @referer_host ||= URI.parse(headers['HTTP_REFERER']).host if headers['HTTP_REFERER']
7
+ # rescue URI::InvalidURIError
8
+ # 'localhost'
9
+ # end
10
+ # end
11
+ #
12
+ # def referer_path
13
+ # URI.parse(headers['HTTP_REFERER']).path if headers['HTTP_REFERER']
14
+ # end
15
+ # end
@@ -0,0 +1,3 @@
1
+ module RailsHacks
2
+ VERSION = "0.0.2"
3
+ end
@@ -0,0 +1,10 @@
1
+ require "rails_hacks/version"
2
+
3
+ module RailsHacks
4
+ # Your code goes here...
5
+ require 'error_to_xml'
6
+ require 'form_helper'
7
+ require 'request_hacks'
8
+ require 'merge_dom_class'
9
+ require 'expose_to_api'
10
+ end
@@ -0,0 +1,18 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/rails_hacks/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["shivashankar"]
6
+ gem.email = ["shivashankar.it@gmail.com"]
7
+ gem.description = %q{Customized rails hacks}
8
+ gem.summary = %q{Rails hacks related to exception, custom tag builder}
9
+ gem.homepage = "https://github.com/shivaaqua/rails_hacks"
10
+
11
+ gem.files = `git ls-files`.split($\)
12
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
13
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
14
+ gem.name = "rails_hacks"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = RailsHacks::VERSION
17
+ gem.platform = Gem::Platform::RUBY
18
+ end
metadata ADDED
@@ -0,0 +1,58 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rails_hacks
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - shivashankar
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-04-18 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: Customized rails hacks
15
+ email:
16
+ - shivashankar.it@gmail.com
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - .gitignore
22
+ - Gemfile
23
+ - LICENSE
24
+ - README.md
25
+ - Rakefile
26
+ - lib/rails_hacks.rb
27
+ - lib/rails_hacks/error_to_xml.rb
28
+ - lib/rails_hacks/expose_to_api.rb
29
+ - lib/rails_hacks/form_helper.rb
30
+ - lib/rails_hacks/merge_dom_class.rb
31
+ - lib/rails_hacks/request_hacks.rb
32
+ - lib/rails_hacks/version.rb
33
+ - rails_hacks.gemspec
34
+ homepage: https://github.com/shivaaqua/rails_hacks
35
+ licenses: []
36
+ post_install_message:
37
+ rdoc_options: []
38
+ require_paths:
39
+ - lib
40
+ required_ruby_version: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ required_rubygems_version: !ruby/object:Gem::Requirement
47
+ none: false
48
+ requirements:
49
+ - - ! '>='
50
+ - !ruby/object:Gem::Version
51
+ version: '0'
52
+ requirements: []
53
+ rubyforge_project:
54
+ rubygems_version: 1.8.24
55
+ signing_key:
56
+ specification_version: 3
57
+ summary: Rails hacks related to exception, custom tag builder
58
+ test_files: []