Skip to content

Commit

Permalink
Rename 'fillable' to 'permit_columns'
Browse files Browse the repository at this point in the history
Closes #77

Also renames `FillableAttribute` to `PermittedAttribute`
  • Loading branch information
paulcsmith committed Jun 15, 2019
1 parent d3503a1 commit b32b5a9
Show file tree
Hide file tree
Showing 11 changed files with 47 additions and 47 deletions.
6 changes: 3 additions & 3 deletions spec/nested_save_operation_spec.cr
@@ -1,16 +1,16 @@
require "./spec_helper"

private class SaveBusiness < Business::SaveOperation
fillable name
permit_columns name
has_one save_email_address : SaveEmailAddress
has_one save_tax_id : SaveTaxId

class SaveEmailAddress < EmailAddress::SaveOperation
fillable address
permit_columns address
end

class SaveTaxId < TaxId::SaveOperation
fillable number
permit_columns number
end
end

Expand Down
22 changes: 11 additions & 11 deletions spec/save_operation_spec.cr
@@ -1,15 +1,15 @@
require "./spec_helper"

private class SaveUser < User::SaveOperation
fillable :name, :nickname, :joined_at, :age
permit_columns :name, :nickname, :joined_at, :age

def prepare
validate_required name, joined_at, age
end
end

private class SaveLimitedUser < User::SaveOperation
fillable :name
permit_columns :name
end

private class SaveTask < Task::SaveOperation
Expand All @@ -22,7 +22,7 @@ private class ValidSaveOperationWithoutParams < Post::SaveOperation
end

private class SaveLineItem < LineItem::SaveOperation
fillable :name
permit_columns :name
end

private class ValueColumnModel < Avram::Model
Expand All @@ -32,7 +32,7 @@ private class ValueColumnModel < Avram::Model
end

private class ValueColumnModelSaveOperation < ValueColumnModel::SaveOperation
fillable value
permit_columns value
end

private class ParamKeySaveOperation < ValueColumnModel::SaveOperation
Expand Down Expand Up @@ -175,19 +175,19 @@ describe "Avram::SaveOperation" do
end
end

describe "fillable" do
it "ignores params that are not fillable" do
describe "permit_columns" do
it "ignores params that are not permitted" do
form = SaveLimitedUser.new({"name" => "someone", "nickname" => "nothing"})
form.changes.has_key?(:nickname).should be_false
form.changes[:name]?.should eq "someone"
end

it "returns a Avram::FillableAttribute" do
it "returns a Avram::PermittedAttribute" do
form = SaveLimitedUser.new({"name" => "someone", "nickname" => "nothing"})
form.nickname.value.should be_nil
form.nickname.is_a?(Avram::Attribute).should be_true
form.name.value.should eq "someone"
form.name.is_a?(Avram::FillableAttribute).should be_true
form.name.is_a?(Avram::PermittedAttribute).should be_true
end
end

Expand Down Expand Up @@ -215,7 +215,7 @@ describe "Avram::SaveOperation" do
end

describe "params" do
it "creates a param method for each of the fillable attributes" do
it "creates a param method for each of the permit_columns attributes" do
params = {"name" => "Paul", "nickname" => "Pablito"}

form = SaveUser.new(params)
Expand All @@ -234,7 +234,7 @@ describe "Avram::SaveOperation" do
end

describe "errors" do
it "creates an error method for each of the fillable attributes" do
it "creates an error method for each of the permit_columns attributes" do
params = {"name" => "Paul", "age" => "30", "joined_at" => now_as_string}
form = SaveUser.new(params)
form.valid?.should be_true
Expand All @@ -258,7 +258,7 @@ describe "Avram::SaveOperation" do
end

describe "attributes" do
it "creates a method for each of the fillable attributes" do
it "creates a method for each of the permit_columns attributes" do
params = {} of String => String
form = SaveLimitedUser.new(params)

Expand Down
4 changes: 2 additions & 2 deletions spec/transaction_spec.cr
@@ -1,7 +1,7 @@
require "./spec_helper"

private class PostTransactionSaveOperation < Post::SaveOperation
fillable title
permit_columns title
needs rollback_after_save : Bool

def after_save(_user)
Expand All @@ -12,7 +12,7 @@ private class PostTransactionSaveOperation < Post::SaveOperation
end

private class BadSaveOperation < Post::SaveOperation
fillable title
permit_columns title

def after_save(_user)
raise "Sad face"
Expand Down
2 changes: 1 addition & 1 deletion spec/type_extension_spec.cr
Expand Up @@ -4,7 +4,7 @@ class CompanyQuery < Company::BaseQuery
end

class SaveCompany < Company::SaveOperation
fillable :sales, :earnings
permit_columns :sales, :earnings

def prepare
validate_required sales
Expand Down
2 changes: 1 addition & 1 deletion spec/validations_spec.cr
Expand Up @@ -12,7 +12,7 @@ private class CallableMessage
end

class UniquenessWithDatabaseBackedSaveOperation < User::SaveOperation
fillable name
permit_columns name

def prepare
validate_uniqueness_of name
Expand Down
6 changes: 3 additions & 3 deletions spec/virtual_spec.cr
Expand Up @@ -16,13 +16,13 @@ private class VirtualOperation < Post::SaveOperation
end

describe "virtual in forms" do
it "is a FillableAttribute" do
form.password_confirmation.should be_a(Avram::FillableAttribute(String?))
it "is a PermittedAttribute" do
form.password_confirmation.should be_a(Avram::PermittedAttribute(String?))
form.password_confirmation.name.should eq(:password_confirmation)
form.password_confirmation.form_name.should eq("virtual")
end

it "generates a list of fillable_attributes" do
it "generates a list of virtual_attributes" do
form.virtual_attributes.map(&.name).should eq [:password_confirmation,
:terms_of_service,
:best_kind_of_bear,
Expand Down
8 changes: 4 additions & 4 deletions src/avram/attribute.cr
Expand Up @@ -8,11 +8,11 @@ class Avram::Attribute(T)
def initialize(@name : Symbol, @param : String?, @value : T, @form_name : String)
end

@_fillable : Avram::FillableAttribute(T)?
@_permitted : Avram::PermittedAttribute(T)?

def fillable
@_fillable ||= begin
Avram::FillableAttribute.new(name: @name, param: @param, value: @value, form_name: @form_name).tap do |attribute|
def permitted
@_permitted ||= begin
Avram::PermittedAttribute.new(name: @name, param: @param, value: @value, form_name: @form_name).tap do |attribute|
errors.each do |error|
attribute.add_error error
end
Expand Down
4 changes: 0 additions & 4 deletions src/avram/fillable_attribute.cr

This file was deleted.

4 changes: 4 additions & 0 deletions src/avram/permitted_attribute.cr
@@ -0,0 +1,4 @@
require "./attribute"

class Avram::PermittedAttribute(T) < Avram::Attribute(T)
end
32 changes: 16 additions & 16 deletions src/avram/save_operation.cr
Expand Up @@ -31,7 +31,7 @@ abstract class Avram::SaveOperation(T)
@valid : Bool = true
@save_status = SaveStatus::Unperformed

@@fillable_param_keys = [] of String
@@permitted_param_keys = [] of String
@@schema_class = T
end

Expand Down Expand Up @@ -76,7 +76,7 @@ abstract class Avram::SaveOperation(T)
{% end %}

private def extract_changes_from_params
fillable_params.each do |key, value|
permitted_params.each do |key, value|
{% for attribute in attributes %}
set_{{ attribute[:name] }}_from_param value if key == {{ attribute[:name].stringify }}
{% end %}
Expand Down Expand Up @@ -105,17 +105,17 @@ abstract class Avram::SaveOperation(T)
private def _{{ attribute[:name] }}
@_{{ attribute[:name] }} ||= Avram::Attribute({{ attribute[:type] }}?).new(
name: :{{ attribute[:name].id }},
param: fillable_params["{{ attribute[:name] }}"]?,
param: permitted_params["{{ attribute[:name] }}"]?,
value: @record.try(&.{{ attribute[:name] }}),
form_name: form_name)
end

def fillable_params
def permitted_params
new_params = {} of String => String
@params.nested(form_name).each do |key, value|
new_params[key] = value
end
new_params.select(@@fillable_param_keys)
new_params.select(@@permitted_param_keys)
end

def set_{{ attribute[:name] }}_from_param(_value)
Expand Down Expand Up @@ -183,44 +183,44 @@ abstract class Avram::SaveOperation(T)
save_status == SaveStatus::SaveFailed
end

macro allow(*args)
{% raise "'allow' has been renamed to 'fillable'" %}
macro fillable(*args)
{% raise "'fillable' has been renamed to 'permit_columns'" %}
end

macro fillable(*attribute_names)
macro permit_columns(*attribute_names)
{% for attribute_name in attribute_names %}
{% if attribute_name.is_a?(TypeDeclaration) %}
{% raise <<-ERROR
Must use a Symbol or a bare word in 'fillable'. Instead, got: #{attribute_name}
Must use a Symbol or a bare word in 'permit_columns'. Instead, got: #{attribute_name}
Try this...
fillable #{attribute_name.var}
permit_columns #{attribute_name.var}
ERROR
%}
{% end %}
{% unless attribute_name.is_a?(SymbolLiteral) || attribute_name.is_a?(Call) %}
{% raise <<-ERROR
Must use a Symbol or a bare word in 'fillable'. Instead, got: #{attribute_name}
Must use a Symbol or a bare word in 'permit_columns'. Instead, got: #{attribute_name}
Try this...
▸ Use a bare word (recommended): 'fillable name'
▸ Use a Symbol: 'fillable :name'
▸ Use a bare word (recommended): 'permit_columns name'
▸ Use a Symbol: 'permit_columns :name'
ERROR
%}
{% end %}
{% if ATTRIBUTES.any? { |attribute| attribute[:name].id == attribute_name.id } %}
def {{ attribute_name.id }}
_{{ attribute_name.id }}.fillable
_{{ attribute_name.id }}.permitted
end

@@fillable_param_keys << "{{ attribute_name.id }}"
@@permitted_param_keys << "{{ attribute_name.id }}"
{% else %}
{% raise <<-ERROR
Can't make '#{attribute_name}' fillable because the column has not been defined on the model.
Can't permit '#{attribute_name}' because the column has not been defined on the model.
Try this...
Expand Down
4 changes: 2 additions & 2 deletions src/avram/virtual.cr
Expand Up @@ -2,7 +2,7 @@ module Avram::Virtual
macro ensure_base_virtual_attributes_method_is_present
{% if !@type.methods.map(&.name).includes?(:virtual_attributes.id) %}
def virtual_attributes
[] of Avram::FillableAttribute(Nil)
[] of Avram::PermittedAttribute(Nil)
end
{% end %}
end
Expand Down Expand Up @@ -63,7 +63,7 @@ module Avram::Virtual
end

def {{ name }}
_{{ name }}.fillable
_{{ name }}.permitted
end

private def _{{ name }}
Expand Down

0 comments on commit b32b5a9

Please sign in to comment.