Sha256: d35177c4bab5f4ded6785fc6a54e24187ceea9413a4b50f41a1a0a7aaead917c

Contents?: true

Size: 1.99 KB

Versions: 17

Compression:

Stored size: 1.99 KB

Contents

module Maestrano
  module Generators
    module OrmHelpers
      
      def model_contents
        
        if model_type == 'user'
          buffer = <<-CONTENT
  # Enable Maestrano for this user
  maestrano_user_via :provider, :uid do |user,maestrano|
    user.name = maestrano.first_name
    user.surname = maestrano.last_name
    user.email = maestrano.email
    #user.company = maestrano.company_name
    #user.country_alpha2 = maestrano.country
    #user.some_required_field = 'some-appropriate-default-value'
  end

CONTENT
        else
          buffer = <<-CONTENT
  # Enable Maestrano for this group
  maestrano_group_via :provider, :uid do |group, maestrano|
    group.name = (maestrano.company_name || "Default Group name")
    #group.country_alpha2 = maestrano.country
    #group.free_trial_end_at = maestrano.free_trial_end_at
    #group.some_required_field = 'some-appropriate-default-value'
  end

CONTENT
        end
        
        buffer += <<-CONTENT if needs_attr_accessible?
  # Setup protected attributes for your model
  attr_protected :provider, :uid

CONTENT
        buffer
      end
      
      def model_type
        self.class.name.split("::").last.gsub("Maestrano","").gsub("Generator","").downcase
      end
      
      def needs_attr_accessible?
        rails_3? && !strong_parameters_enabled?
      end

      def rails_3?
        ::Rails::VERSION::MAJOR == 3
      end

      def strong_parameters_enabled?
        defined?(ActionController::StrongParameters)
      end
      
      private
        def model_exists?
          File.exists?(File.join(destination_root, model_path))
        end

        def migration_exists?(table_name)
          Dir.glob("#{File.join(destination_root, migration_path)}/[0-9]*_*.rb").grep(/\d+_add_maestrano_to_#{table_name}.rb$/).first
        end

        def migration_path
          @migration_path ||= File.join("db", "migrate")
        end

        def model_path
          @model_path ||= File.join("app", "models", "#{file_path}.rb")
        end
    end
  end
end

Version data entries

17 entries across 17 versions & 2 rubygems

Version Path
maestrano-rails-0.10.0 lib/generators/maestrano/orm_helpers.rb
maestrano-rails-0.9.5 lib/generators/maestrano/orm_helpers.rb
maestrano-rails-0.9.4 lib/generators/maestrano/orm_helpers.rb
maestrano-rails-test-0.9.4 lib/generators/maestrano/orm_helpers.rb
maestrano-rails-test-0.9.3 lib/generators/maestrano/orm_helpers.rb
maestrano-rails-0.9.3 lib/generators/maestrano/orm_helpers.rb
maestrano-rails-0.9.2 lib/generators/maestrano/orm_helpers.rb
maestrano-rails-0.9.1 lib/generators/maestrano/orm_helpers.rb
maestrano-rails-0.9.0 lib/generators/maestrano/orm_helpers.rb
maestrano-rails-0.8.0 lib/generators/maestrano/orm_helpers.rb
maestrano-rails-0.7.0 lib/generators/maestrano/orm_helpers.rb
maestrano-rails-0.6.0 lib/generators/maestrano/orm_helpers.rb
maestrano-rails-0.5.0 lib/generators/maestrano/orm_helpers.rb
maestrano-rails-0.4.0 lib/generators/maestrano/orm_helpers.rb
maestrano-rails-0.3.0 lib/generators/maestrano/orm_helpers.rb
maestrano-rails-0.2.0 lib/generators/maestrano/orm_helpers.rb
maestrano-rails-0.1.0 lib/generators/maestrano/orm_helpers.rb