Posts Tagged ‘rails plugins’

search plugin for rails

# USAGE EXAMPLE
# -----------------------------------------------------------
# class PostsController < ApplicationController
#   restful_search do |s|
#     s.model = :foo
#     s.search_param_name = "search" # defaults to 'match'
#     s.columns = [ 'foo.name', 'bar.state', 'baz.comment' ],
#     s.joins = [ :foo => { :bar => :baz } ],
#   end
#
#   def index
#     @posts = restful_search_results
#   end
# end
# -----------------------------------------------------------

Read more »

my resource_controller rails plugin

# Specify not more than this to get a restful controller
# with the actions of your choice. You can always override
# the generated actions if the default implementation
# doesn't fit your needs. Of course, should you need more
# than the standard CRUD actions, you're free to add them
# to the class body. If your resource shall be audited
# (by referencing a creator) you must have either a
# current_user or current_account instance method defined
# on your controller (restful_authentication will provide
# one). If for any reason you choose to name your
# current_user getter different from the 2 examples above,
# simply override current_user_getters in your controller
# (most probably ApplicationController) to include your
# method name, and you're all set. By default, this plugin
# assumes that you have 'will_paginate' installed and will
# paginate your index actions. If you don't want this to
# happen, pass false to the will_paginate method in your
# resource spec.
# ------------------------------------------------------
#
# class PostsController < ApplicationController
#   resource :posts do |posts|
#     posts.actions = [ :index, :show, :edit ] # all if omitted
#     posts.audit_column = :creator # :created_by if omitted
#     posts.will_paginate = false # true if omitted
#     posts.index, :include => :creator # eager load this when calling find
#     posts.show,  :include => [ :creator, :comments ]
#   end
# end

Read more »

rails plugin that adds default includes to activerecord models

# USAGE
# class Foo < ActiveRecord::Base
#   default_includes = {
#       [ :bar => :baz ],
#       :bam
#     }
#   end
# end
module DefaultIncludeHelper

  def self.included(base)
    base.extend(ClassMethods)
  end

  class InvalidEagerLoadingDefaults < Exception; end

  module ClassMethods

    attr_reader :default_includes

    def find_with_default_includes(*args)
      options = args.extract_options!
      if @default_includes && !options[:include]
        options.merge!(:include => @default_includes)
      end
      find(*(args << options))
    end

    def eager_loading_defaults(defaults)
      unless valid_default_includes?(defaults)
        msg = 'includes must be either a Hash, an Array or a Symbol'
        raise InvalidEagerLoadingDefaults, msg
      end
      @default_includes = defaults
    end

    private 

    def valid_default_includes?(o)
      o.is_a?(Symbol) || o.is_a?(Array) || o.is_a?(Hash)
    end

  end

end

rails plugin to perform data migrations

# vendor/plugins/data_migration/init.rb

require 'snusnu/data_migration'
require 'active_record/fixtures'

class ActiveRecord::Migration
  include Snusnu::DataMigration
end

# vendor/plugins/data_migration/lib/snusnu/data_migration.rb

module DataMigration

  class DataMigrationException < Exception
  end

  class InvalidDataMigratorSpecException < DataMigrationException
  end

  def self.included(base)
    base.extend(ClassMethods)
  end

  class DataMigrator

    attr_reader :file_name, :folder_name, :format

    def initialize(file_name, folder_name, format)
      @file_name, @folder_name, @format = file_name, folder_name, format
      raise_if_invalid_data_migrator_spec!
    end

    def migrate
      Fixtures.create_fixtures(fixture_folder_path, fixture_file)
    end

    def valid?
      [ @file_name, @folder_name, @format ].all? do |v|
        v.is_a?(String) || v.is_a?(Symbol)
      end # raises if called from initialize
    end

    def fixture_path
      "#{fixture_folder_path}/#{fixture_file}"
    end

    def fixture_folder_path
      "#{RAILS_ROOT}/db/migrate/#{@folder_name}"
    end

    def fixture_file
      "#{@file_name.to_s.gsub(/.yml/, @format.to_s)}"
    end

    private

    def raise_if_invalid_data_migrator_spec!
      unless valid?
        msg = "Expected all args to fixtures method to be symbols or strings"
        raise InvalidDataMigratorSpec, msg
      end
    end
  end

  module ClassMethods

    def fixtures(file_name, options = { :folder => 'data', :format => :yml })
      @data_migrator =
        DataMigrator.new(file_name, options[:folder], options[:format])
      class << self
        # redefine rails migrate behaviour
        # only if a fixture file was specified
        alias_method_chain :migrate, :fixture_data
      end
      self.class.send :attr_reader, :data_migrator
    end

    def migrate_with_fixture_data(direction)
      migrate_without_fixture_data(direction) # perform migration
      if direction == :up && respond_to?(:data_migrator)
        data_migrator.migrate
        say "Loaded #{data_migrator.fixture_path}", true
      end
    rescue Errno::ENOENT => e
      say "Snusnu::DataMigration WARNING"
      say e.message, true
    end

  end

end

rails plugin that enables activerectord STI with referential integrity

module Snusnu #:nodoc:

  # -------------------------------
  # USAGE
  # -------------------------------
  #
  # class BasicInput < ActiveRecord::Base
  #   inheritance_table :input_types do |map|
  #     # uses 'input_type_id' for :type_id
  #     map.route :type_id => 1, :to_class => "TextInput"
  #     map.route :type_id => 2, :to_class => "NumericInput"
  #   end
  # end
  #
  # or
  #
  # class BasicInput < ActiveRecord::Base
  #   inheritance_table :input_types, :type_column => :type do |map|
  #     # uses 'type' as column for :type_id
  #     map.route :type_id => 1, :to_class => "TextInput"
  #     map.route :type_id => 2, :to_class => "NumericInput"
  #   end
  # end

Read more »