Commit 69715aa6a451a1a387238066639e4a2d9554bd97

Authored by Lee Dykes
0 parents
Exists in master

initial commit

.gitignore 0 → 100644
  1 +++ a/.gitignore
... ... @@ -0,0 +1,9 @@
  1 +/.bundle/
  2 +/.yardoc
  3 +/Gemfile.lock
  4 +/_yardoc/
  5 +/coverage/
  6 +/doc/
  7 +/pkg/
  8 +/spec/reports/
  9 +/tmp/
... ...
CODE_OF_CONDUCT.md 0 → 100644
  1 +++ a/CODE_OF_CONDUCT.md
... ... @@ -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, ethnicity, 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/)
... ...
Gemfile 0 → 100644
  1 +++ a/Gemfile
... ... @@ -0,0 +1,4 @@
  1 +source 'https://rubygems.org'
  2 +
  3 +# Specify your gem's dependencies in surveyable.gemspec
  4 +gemspec
... ...
README.md 0 → 100644
  1 +++ a/README.md
... ... @@ -0,0 +1,36 @@
  1 +# Surveyable
  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/surveyable`. 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 'surveyable'
  13 +```
  14 +
  15 +And then execute:
  16 +
  17 + $ bundle
  18 +
  19 +Or install it yourself as:
  20 +
  21 + $ gem install surveyable
  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. You can also 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`, which will 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 +Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/surveyable. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](contributor-covenant.org) code of conduct.
  36 +
... ...
Rakefile 0 → 100644
  1 +++ a/Rakefile
... ... @@ -0,0 +1 @@
  1 +require "bundler/gem_tasks"
... ...
bin/console 0 → 100755
  1 +++ a/bin/console
... ... @@ -0,0 +1,14 @@
  1 +#!/usr/bin/env ruby
  2 +
  3 +require "bundler/setup"
  4 +require "surveyable"
  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
... ...
bin/setup 0 → 100755
  1 +++ a/bin/setup
... ... @@ -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
... ...
lib/surveyable.rb 0 → 100644
  1 +++ a/lib/surveyable.rb
... ... @@ -0,0 +1,6 @@
  1 +require "surveyable/version"
  2 +require "acts_as_survey"
  3 +
  4 +module Surveyable
  5 + # Your code goes here...
  6 +end
... ...
lib/surveyable/acts_as_response.rb 0 → 100644
  1 +++ a/lib/surveyable/acts_as_response.rb
... ... @@ -0,0 +1,16 @@
  1 +module Surveyable
  2 + module ActsAsResponse
  3 + extend ActiveSupport::Concern
  4 + included do
  5 + has_many :answers, as: :response
  6 + end
  7 + module ClassMethods
  8 + def acts_as_response(survey, options = {})
  9 + cattr_accessor :survey
  10 + self.survey = survey
  11 + end
  12 + end
  13 + end
  14 +end
  15 +
  16 +ActiveRecord::Base.send :include, Surveyable::ActsAsResponse
0 17 \ No newline at end of file
... ...
lib/surveyable/acts_as_survey.rb 0 → 100644
  1 +++ a/lib/surveyable/acts_as_survey.rb
... ... @@ -0,0 +1,17 @@
  1 +module Surveyable
  2 + module ActsAsSurvey
  3 + extend ActiveSupport::Concern
  4 + included do
  5 + has_many :questions, as: :survey
  6 +
  7 + end
  8 + module ClassMethods
  9 + def acts_as_survey(responses, options = {})
  10 + cattr_accessor :responses
  11 + self.responses = responses
  12 + end
  13 + end
  14 + end
  15 +end
  16 +
  17 +ActiveRecord::Base.send :include, Surveyable::ActsAsSurvey
0 18 \ No newline at end of file
... ...
lib/surveyable/answer.rb 0 → 100644
  1 +++ a/lib/surveyable/answer.rb
... ... @@ -0,0 +1,10 @@
  1 +module Surveyable
  2 + class Answer < ActiveRecord::Base
  3 + belongs_to :response, polymorphic: true
  4 + belongs_to :question
  5 + belongs_to :answer_choice
  6 +
  7 + scope :required, -> {joins(:question).where('questions.required'=>true)}
  8 + scope :answered, -> {where("(answers.text IS NOT NULL AND answers.text <> '') OR answers.answer_choice_id IS NOT NULL")}
  9 + end
  10 +end
... ...
lib/surveyable/answer_choice.rb 0 → 100644
  1 +++ a/lib/surveyable/answer_choice.rb
... ... @@ -0,0 +1,6 @@
  1 +module Surveyable
  2 + class AnswerChoice < ActiveRecord::Base
  3 + belongs_to :question
  4 + has_one :answer
  5 + end
  6 +end
... ...
lib/surveyable/question.rb 0 → 100644
  1 +++ a/lib/surveyable/question.rb
... ... @@ -0,0 +1,99 @@
  1 +module Surveyable
  2 + class Question < ActiveRecord::Base
  3 + belongs_to :survey, polymorphic: true
  4 + has_many :answers, :dependent => :restrict_with_error
  5 + has_many :answer_choices, dependent: :destroy
  6 + STI_TYPES = %w[InfoField HiddenField BooleanField DateField SingleSelectField MultiSelectField TextField StringField IntegerField MoneyAmountField SingleDocumentField MultiDocumentField TelephoneField RelationshipSelectField]
  7 + accepts_nested_attributes_for :answer_choices, :allow_destroy => true,
  8 + :reject_if => lambda { |a| a[:text].blank? }
  9 + validates_presence_of :text
  10 + validates_presence_of :type
  11 +
  12 + scope :required, -> { where(required:true)}
  13 + def field_type
  14 + 'text'
  15 + end
  16 + end
  17 + class InfoField < Question
  18 + def field_type
  19 + 'noAnswer'
  20 + end
  21 + end
  22 +
  23 +
  24 + class HiddenField < Question
  25 + def field_type
  26 + 'hidden'
  27 + end
  28 + end
  29 +
  30 + class BooleanField < Question
  31 + def field_type
  32 + 'boolean'
  33 + end
  34 + end
  35 +
  36 + class DateField < Question
  37 + def field_type
  38 + 'date'
  39 + end
  40 + end
  41 +
  42 + class SingleSelectField < Question
  43 + def field_type
  44 + 'select'
  45 + end
  46 + end
  47 +
  48 + class RelationshipSelectField < SingleSelectField
  49 +
  50 + end
  51 +
  52 + class MultiSelectField < Question
  53 + def field_type
  54 + 'select'
  55 + end
  56 + end
  57 +
  58 + class TextField < Question
  59 + def field_type
  60 + 'textarea'
  61 + end
  62 + end
  63 +
  64 + class StringField < Question
  65 + def field_type
  66 + 'text'
  67 + end
  68 + end
  69 +
  70 + class IntegerField < Question
  71 + def field_type
  72 + 'number'
  73 + end
  74 + end
  75 +
  76 + class MoneyAmountField < Question
  77 + def field_type
  78 + 'text'
  79 + end
  80 + end
  81 +
  82 + class SingleDocumentField < Question
  83 + def field_type
  84 + 'file'
  85 + end
  86 + end
  87 +
  88 + class MultiDocumentField < Question
  89 + def field_type
  90 + 'file'
  91 + end
  92 + end
  93 +
  94 + class TelephoneField < Question
  95 + def field_type
  96 + 'tel'
  97 + end
  98 + end
  99 +end
0 100 \ No newline at end of file
... ...
lib/surveyable/version.rb 0 → 100644
  1 +++ a/lib/surveyable/version.rb
... ... @@ -0,0 +1,3 @@
  1 +module Surveyable
  2 + VERSION = "0.1.0"
  3 +end
... ...
surveyable.gemspec 0 → 100644
  1 +++ a/surveyable.gemspec
... ... @@ -0,0 +1,32 @@
  1 +# coding: utf-8
  2 +lib = File.expand_path('../lib', __FILE__)
  3 +$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
  4 +require 'surveyable/version'
  5 +
  6 +Gem::Specification.new do |spec|
  7 + spec.name = "surveyable"
  8 + spec.version = Surveyable::VERSION
  9 + spec.authors = ["Lee Dykes"]
  10 + spec.email = ["lee.d.dykes@gmail.com"]
  11 +
  12 + spec.summary = %q{TODO: Write a short summary, because Rubygems requires one.}
  13 + spec.description = %q{TODO: Write a longer description or delete this line.}
  14 + spec.homepage = "TODO: Put your gem's website or public repo URL here."
  15 +
  16 + # Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or
  17 + # delete this section to allow pushing this gem to any host.
  18 + if spec.respond_to?(:metadata)
  19 + spec.metadata['allowed_push_host'] = "TODO: Set to 'http://mygemserver.com'"
  20 + else
  21 + raise "RubyGems 2.0 or newer is required to protect against public gem pushes."
  22 + end
  23 +
  24 + spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
  25 + spec.bindir = "exe"
  26 + spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
  27 + spec.require_paths = ["lib"]
  28 +
  29 + spec.add_development_dependency "bundler", "~> 1.10"
  30 + spec.add_development_dependency "rake", "~> 10.0"
  31 + spec.add_runtime_dependency "activerecord", "~> 4.0.0"
  32 +end
... ...
test/acts_as_survey_test.rb 0 → 100644
  1 +++ a/test/acts_as_survey_test.rb
... ... @@ -0,0 +1,23 @@
  1 +require 'test_helper'
  2 +
  3 +class ActsAsSurveyTest < ActiveSupport::TestCase
  4 +
  5 + # Called before every test method runs. Can be used
  6 + # to set up fixture information.
  7 + def setup
  8 + # Do nothing
  9 + end
  10 +
  11 + # Called after every test method runs. Can be used to tear
  12 + # down fixture information.
  13 +
  14 + def teardown
  15 + # Do nothing
  16 + end
  17 +
  18 + # Fake test
  19 + def test_fail
  20 +
  21 + fail('Not implemented')
  22 + end
  23 +end
0 24 \ No newline at end of file
... ...